13

我已经构建了一个类似于 vine app 视频录制的模块。但我无法将视频尺寸设为 480x480 px 。有没有办法做到这一点。谢谢

4

2 回答 2

1

Android 相机的可用尺寸列表有限。所以我们需要选择最佳的相机尺寸并从原始相机图像中选择子图像(480x480)。例如,在我的 HTC one m8 上,我的相机尺寸如下:

  1. 1920x1088
  2. 1920x1080
  3. 1808x1080
  4. ……
  5. 720x480
  6. 640x360
  7. 640x480
  8. 576x432
  9. 480x320
  10. 384x288
  11. 352x288
  12. 320x240
  13. 240x160
  14. 176x144

您可以使用getSupportedPreviewSizes()方法检索可用大小列表。

public Camera mCamera;//Your camera instance
public List<Camera.Size> cameraSizes;
private final int CAMERA_IMAGE_WIDTH = 480;
private final int CAMERA_IMAGE_HEIGHT = 480;
...

cameraSizes = mCamera.getParameters().getSupportedPreviewSizes()

之后,您需要找到最合适的相机尺寸并为相机设置预览尺寸。

Camera.Size findBestCameraSize(int width, int height){
  Camera.Size bestSize = cameraSizes.get(0);
  int minimalArea = bestSize.height * bestSize.width;
  for(int i = 1;i < cameraSizes.size();i++){
    Camera.Size size = cameraSizes.get(i);

    int area = size.height * size.width;
    if(size.width < width || size.height < height){
      continue;
    }

    if(area < minimalArea){
      bestSize = size;
      minimalArea = area;
    }
  }
  return bestSize;
}

...
SurfaceHolder.Callback surfaceCallback = new SurfaceHolder.Callback() {
  public void surfaceCreated(SurfaceHolder holder) {
    //Do something
  }

  public void surfaceChanged(SurfaceHolder holder,
                             int format, int width,
                             int height) {
    Camera.Parameters params = mCamera.getParameters();
    Camera.Size size = findBestCameraSize(CAMERA_IMAGE_WIDTH, CAMERA_IMAGE_HEIGHT);
    params.setPreviewSize(size.width, size.height);
    camera.setParameters(params);
    if(mCamera != null){
      mCamera.startPreview();
    }
  }

  public void surfaceDestroyed(SurfaceHolder holder) {
    // Do something
  }
  };    

设置好相机尺寸后,我们需要从相机的结果位图中获取子图像。您需要将此代码放在收到位图图片的位置(通常我使用 OpenCV 库和矩阵以获得更好的性能)。

Bitmap imageFromCamera = //here ve receive image from camera.
Camera.Size size = mCamera.getParameters().getPreviewSize();
int x = (size.width - CAMERA_IMAGE_WIDTH)/2;
int y = (size.height - CAMERA_IMAGE_HEIGHT)/2;
Bitmap resultBitmap = null;
if(x < 0 || y < 0){
   resultBitmap = imageFromCamera;
}else{
   resultBitmap = Bitmap.createBitmap(imageFromCamera, x, y, CAMERA_IMAGE_WIDTH, CAMERA_IMAGE_HEIGHT);
}
于 2017-08-09T11:54:38.783 回答
0

The video capture resolution for android are limited to the native resolutions supported by the camera.

You can try using a 3rd party library for video post processing. So you can crop or re-scale the video captured by the camera.

I am using this one

android-gpuimage-videorecording

and it works quite well.

于 2017-08-02T16:05:25.597 回答