View full screen image without distortion / stretch

I have a CameraPreview class (see below) that runs fullscreen and landscape ... but the image gets stretched / distorted. Is there a way to make this preview remain in full screen mode but not distort it?

camLayer:

public class CamLayer extends SurfaceView implements SurfaceHolder.Callback { Camera camera; SurfaceHolder previewHolder; String camID; private static final String TAG = "Cam Preview"; public CamLayer(Context context, String facing) { super(context); camID = facing; previewHolder = this.getHolder(); previewHolder.addCallback(this); previewHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); } public void surfaceCreated(SurfaceHolder holder) { startCamera(); } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { Parameters params = camera.getParameters(); //params.setPreviewSize(width, height); //params.setPictureFormat(PixelFormat.JPEG); camera.setParameters(params); camera.startPreview(); } public void surfaceDestroyed(SurfaceHolder arg0) { stopCamera(); } public void onResume() { startCamera(); } public void onPause() { stopCamera(); } public void stopCamera(){ System.out.println("stopCamera method"); if (camera != null){ camera.stopPreview(); camera.setPreviewCallback(null); camera.release(); camera = null; previewHolder.removeCallback(this); previewHolder = null; } } private void startCamera(){ if(camID.equals("front")){ camera=Camera.open(Camera.CameraInfo.CAMERA_FACING_FRONT); }else{ camera=Camera.open(Camera.CameraInfo.CAMERA_FACING_BACK); } try { camera.setPreviewDisplay(previewHolder); } catch (Throwable e){ Log.w("TAG,", "failed create surface !?!?"); } } public void draw(Canvas canvas) { super.draw(canvas); Paint p = new Paint(Color.RED); Log.d(TAG, "draw"); canvas.drawText("PREVIEW", canvas.getWidth() / 2, canvas.getHeight() / 2, p); } } 
+6
source share
2 answers

You need to run params.getSupportedPreviewSizes();

and from this find the best preview for your view

Have a look at this project: https://github.com/commonsguy/cw-advandroid/blob/master/Camera/Picture/src/com/commonsware/android/picture/PictureDemo.java

  private Camera.Size getBestPreviewSize(int width, int height, Camera.Parameters parameters) { Camera.Size result=null; for (Camera.Size size : parameters.getSupportedPreviewSizes()) { if (size.width <= width && size.height <= height) { if (result == null) { result=size; } else { int resultArea=result.width * result.height; int newArea=size.width * size.height; if (newArea > resultArea) { result=size; } } } } return(result); } 
+1
source

In addition to what Lena Bru says about finding a good preview size if there is no exact ratio of the proportions between your CamLayer sizes and the camera preview size, the only option to avoid distortion is to customize the layout of your CamLayer.

The camera’s preview is simply stretched to cover the whole view, regardless of aspect ratio, so for an undistorted view, you need to customize the layout of the view in accordance with the selected preview format. See the Android Developer site for user component documentation ; especially the part about onMeasure.

You want to tell the width and height based on the proportion of the preview, and you may need to trigger a relay of your user interface when setting the size of the camera’s preview to update the view size.

0
source

Source: https://habr.com/ru/post/911897/


All Articles