Problems viewing the Android camera Distortion / calibration

I have an application where the photo preview size is smaller than the phone screen. I read a lot of posts / ideas on how to fix this, but my preview is still distorted.

Ideally, I could save the preview area as it is, and “stretch and re-center” the preview so that the edges of the photo would not appear on the screen.

From what I can say, I get all the correct values ​​for the height and width of the photo and the height and width of the photo, but I'm not sure where and how I can adjust the stretch factor. Right now, a 640x480 photo (or any other size) is added to the smaller preview area.

Here is the code I'm using:

class Preview extends SurfaceView implements SurfaceHolder.Callback { SurfaceHolder mHolder; public Camera camera; boolean writingFile = false; Size theBiggest=null; Size screenSize=null; Float cameraRatio=null; Preview(Context context) { super(context); // Install a SurfaceHolder.Callback so we get notified when the // underlying surface is created and destroyed. mHolder = getHolder(); mHolder.addCallback(this); mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); } // // surfaceCreate is called the first time the Camera Tab is loaded // public void surfaceCreated(SurfaceHolder holder) { // The Surface has been created, acquire the camera and tell it where to draw. camera = Camera.open(); camera.setDisplayOrientation(90); // Get For Photo Size Parameters camparams = camera.getParameters(); // Find the Largest Possible Photo Size List<Size> sizes = camparams.getSupportedPictureSizes(); int maxWidth = 0; int maxHeight = 0; for (Size s : sizes) { if (s.width > maxWidth || s.height > maxHeight) { maxWidth = s.width; maxHeight = s.height; theBiggest = s; } } // Set Photo Size camparams.setPictureSize(theBiggest.width, theBiggest.height); camera.setParameters(camparams); } // end surfaceCreate() public void surfaceDestroyed(SurfaceHolder holder) { // Surface will be destroyed when we return, so stop the preview. // Because the CameraDevice object is not a shared resource, it very // important to release it when the activity is paused. camera.setPreviewCallback(null); camera.stopPreview(); camera.release(); camera = null; } public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { // Now that the size is known, set up the camera parameters and begin // the preview. camera.setDisplayOrientation(90); Camera.Parameters parameters = camera.getParameters(); Camera.Size size = getBestPreviewSize(w, h); parameters.setPreviewSize(size.width, size.height); // preview size camera.setParameters(parameters); camera.startPreview(); } private Camera.Size getBestPreviewSize(int width, int height) { // Get For Photo Size Camera.Parameters camparams = camera.getParameters(); // Find the Largest Possible Preview Sizes List<Size> sizes = camparams.getSupportedPreviewSizes(); Camera.Size result=null; for (Size s : sizes) { if (s.width <= width && s.height <= height) { if (result == null) { result = s; } else { int resultArea=result.width*result.height; int newArea=s.width*s.height; if (newArea>resultArea) { result=s; } } // end else (result=null) } // end if (width<width&&height<height) } // end for return result; } // end function } 

Here is my layout xml file

  <FrameLayout android:layout_weight="1" android:id="@+id/preview" android:fitsSystemWindows="true" android:layout_height="0dp" android:layout_width="wrap_content"> </FrameLayout> <Button android:text="Take Photo" android:id="@+id/buttonClick" android:layout_height="wrap_content" android:layout_width="200dp" android:layout_gravity="center" android:textSize="12sp"> </Button> 

+6
source share

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


All Articles