The camera is stretched in portrait mode

I am trying to put the camera in a surfaceView in portrait orientation on an Android phone. I used http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/graphics/CameraPreview.html to put the camera in a surfaceView. However, the camera rotates 90 degrees, so I tried to do setDisplayOrientation (90) to fix it, but it goes through the image (does this probably change the View surface incorrectly?). How can I do this so that the camera does not squish?

+6
source share
4 answers

You need to resize the image to fit the rotation. See here for an example.

+6
source

If you used the Android API demos, you need to change the OnLayout() function.

Basically, the demo version of the Android API sets the preview size according to the aspect ratio, so that the preview image is compressed and displayed in the center of the screen with a small size in portrait mode.

Even if we adjust the screen orientation in portrait mode, the width and height of the preview will not be changed. This is why the preview image was compressed.

+2
source

The only thing that worked for me is

In code mCamera.setDisplayOrientation(90);

Layout has fill_parent for height and width .

And the manifest file has android:screenOrientation="portrait"

0
source

use this function to resize the image, which will be useful to you

 public Bitmap resize(Bitmap img,int Width,int Height){ int width = img.getWidth(); int height = img.getHeight(); int newWidth = (int) Width; int newHeight = (int) Height; // calculate the scale - in this case = 0.4f float scaleWidth = ((float) newWidth) / width; float scaleHeight = ((float) newHeight) / height; // createa matrix for the manipulation Matrix matrix = new Matrix(); // resize the bit map matrix.postScale(scaleWidth, scaleHeight); // rotate the Bitmap //matrix.postRotate(45); // recreate the new Bitmap Bitmap resizedBitmap = Bitmap.createBitmap(img, 0, 0, width, height, matrix, true); return resizedBitmap; } 
-2
source

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


All Articles