Android Camera Preview on Square Screen

I am creating an Android application, but the device has a square screen. The 320x320 screen and camera app use SurfaceView to show a preview as shown below:

mCameraView = (SurfaceView) findViewById(R.id.surface); LayoutParams params = mCameraView.getLayoutParams(); int camera_dimension = getResources().getInteger(R.integer.camera_dimension); params.height = camera_dimension; //320px params.width = camera_dimension; mCameraView.setLayoutParams(params); mCameraViewHolder = mCameraView.getHolder(); mCameraViewHolder.addCallback(this); mCameraViewHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 

The surface has changed, I do it and it works, but the preview is w480 x h320

 public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { int mWidth = w; int mHeight = h; int mFormat = format; try { mCameraAccess.mCamera.stopPreview(); } catch (Exception e){ // ignore: tried to stop a non-existent preview } Camera.Parameters param = mCameraAccess.mCamera.getParameters(); List<Camera.Size> SupportedPreview = param.getSupportedPreviewSizes(); int ScreenSize = getResources().getInteger(R.integer.camera_dimension); for (int i = 0; i < SupportedPreview.size(); i++) { if (SupportedPreview.get(i).height == ScreenSize) { param.setPreviewSize(SupportedPreview.get(i).width, SupportedPreview.get(i).height); break; } } mCameraAccess.mCamera.setParameters(param); mCameraAccess.mCamera.startPreview(); } 

How can I make sure that my preview inside the viewer is not compressed, but some kind of center. Since the preview is a rectangle, I just need a square centered on the image. I usually use scaleType but not supported in Surface view

Any idea?

+4
android android-camera surfaceview
source share
1 answer

The solution I found out is the following:

1) -See the full screen to full screen so that it does not stretch.

2) -Look at the surface finish with full opacity. Thus, the camera looks already squared.

3). After capturing an image or video, you have to crop them.

For video, you should use some video processing library like javacv. Using this library, you can extract video frames, convert them to bitmaps, crop the bitmap in a square, and then transcode to video.

To get accurate results, you will need various methods, such as zooming the camera during capture, etc. according to your needs.

Original Image:

enter image description here

Square image:

enter image description here

+3
source share

All Articles