Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Record video with a size of 480 * 480 px in android?

I have built a module similar to vine app video recording. But I am not able to make the video size to 480x480 px . Is there any way to do that. Thanks

like image 648
stack5 Avatar asked Feb 04 '14 11:02

stack5


1 Answers

The Android camera has a limited list of available sizes for the camera. So we need to select best camera size and select sub image(480x480) from the original camera image. For example on my HTC one m8 I have this sizes for the camera:

  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

You can retrieve list of available size by use getSupportedPreviewSizes() method.

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()

After that, you need to find most suitable camera size and set preview size for the camera.

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
  }
  };    

After setup camera size, we need to get sub image from result bitmap, from the camera. You need put this code where you receive a bitmap picture(Usually I use an OpenCV library and matrixes for better performance).

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);
}
like image 82
Albert Avatar answered Nov 03 '22 11:11

Albert