SoFunction
Updated on 2025-03-01

Android on the issue of stretching the custom camera preview interface

About custom camera preview interface stretching

1. The main deformation is caused by the difference in the rotation angle of the Camera preview interface and the angle of the camera mounted.
2. The direction of our Activity is set to be vertical screen, which is the natural direction of the phone, so the width ratio is shorter
3. Angle: The so-called angle between the screen and the camera refers to the angle that rotates relative to the natural direction. The current direction can be obtained based on the rotation angle.
4. If: when the phone is a vertical screen, the natural angle is 0, but the Camera rotates 90 degrees counterclockwise, then we set to rotate 90 degrees clockwise, which is normal. When the phone is horizontal, Camera returns to 0 degrees. If it is set to rotate 90 degrees clockwise, it will rotate back.

How to set up a method to keep the preview interface consistent with the real scene? The official documentation:

public static void setCameraDisplayOrientation(Activity activity,int cameraIo, Camera camera){

 info=new ();
(cameraIo,info);
int rotation=().getDefaultDisplay().getRotation();
int degress=0;
switch(rotation){
case Surface.ROTATION_0:
 degress=0;
 break;
case Surface.ROTATION_90:
 degress=90;
 break;
 case Surface.ROTATION_180:
 degress=180;
 break;
case Surface.ROTATION_270:
 degress=270;
 break;
}
int result;
if(=.CAMERA_FACING_FRONT){
 result = ( + degrees) % 360;
 > How many degrees do the camera need to turn clockwise to restore the natural direction?
 result = (360 - result) % 360;
 } else { // back-facing
 result = ( - degrees + 360) % 360;
 }
 (result);
switch (result) {
 case 0:
 case 180:
 setCameraSize((),   getScreenWidth(), getScreenHeight());
 break;
 case 90:
 case 270:
 setCameraSize((),   getScreenHeight(), getScreenWidth());
  break;
}
}

public static void setCameraSize( parameters, int width, int height) {
 Map<String, List<Size>> allSizes = new HashMap<>();
 String typePreview = "typePreview";
 String typePicture = "typePicture";
 (typePreview, ());
 (typePicture, ());
 Iterator iterator = ().iterator();
 while (()) {
  <String, List<Size>> entry = (<String, List<Size>>) ();
  List<Size> sizes = ();
  if (sizes == null || ()) continue;
  ArrayList<WrapCameraSize> wrapCameraSizes = new ArrayList<>(());
  for (Size size : sizes) {
  WrapCameraSize wrapCameraSize = new WrapCameraSize();
  ();
  ();
  ((( - width)) + (( - height)));
  if ( == width &&  == height) {
   if ((())) {
   (, );
   } else if ((())) {
   (, );
   }
   (TAG, "best size: width=" +  + ";height=" + );
   break;
  }
  (wrapCameraSize);
  }
  (TAG, "()=" + ());
  Size resultSize = null;
  if ((())) {
  resultSize = ();
  } else if ((())) {
  resultSize = ();
  }
  if (resultSize == null || ( != width &&  != height)) {
  //Find the best size for the camera Preview Size and Picture Size  if(()) continue;
  WrapCameraSize minCameraSize = (wrapCameraSizes);
  while (!(() >= width && () >= height)) {
   (minCameraSize);
   if(()) break;
   minCameraSize = null;
   minCameraSize = (wrapCameraSizes);
  }
  (TAG, "best min size: width=" + () + ";height=" + ());
  if ((())) {
   ((), ());
  } else if ((())) {
   ((), ());
  }
  }
  ();
 }
 }

First, you will get the size list that the phone supports preview and return it to a collection.
Make a judgment on the screen direction, because the preview sizes are w>h. If it is a vertical screen, the width and height need to be changed.
Compare the width and height of each element in the preview size list with the width and height of the SurfaceView, and set the current width and height to the preview size if there is a width and height of the SurfaceView, the same size.
If this step is not found, compare the scale of the size list with the SUrfaceView and find a one that is the same or similar.

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.