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
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:
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With