Camera review turned upside down in my Android app for Nexus 5X?

I am developing an Android application that uses a camera preview in the background. It works great on different devices.

But when testing on the Nexus 5X, the camera preview is upside down. I use this permission in the manifest

<uses-permission android:name="android.permission.CAMERA" />

My camera code is as follows

public void startCamera(int myTexture)
  {
    surface = new SurfaceTexture(myTexture);

    try {
      camera = Camera.open();


    } catch (Exception e) {
      Log.e("MainActivity", "failed to open Camera");
      e.printStackTrace();
    }
    try
    {
      camera1.setPreviewTexture(surface);
      camera1.startPreview();
    }
    catch (IOException ioe)
    {
      Log.w("MainActivity","Camera launch failed");
    }
  }
Run code

Test Camera .setDisplayOrientation (180); But it didn’t work out. Please, help!

+5
source share
2 answers

, setDisplayOrientation(), .

( Nexus 5X):

public static void setCameraDisplayOrientation(Activity activity,
     int cameraId, android.hardware.Camera camera) {
   android.hardware.Camera.CameraInfo info =
       new android.hardware.Camera.CameraInfo();
   android.hardware.Camera.getCameraInfo(cameraId, info);
   int rotation = activity.getWindowManager().getDefaultDisplay()
       .getRotation();
   int degrees = 0;
   switch (rotation) {
     case Surface.ROTATION_0: degrees = 0; break;
     case Surface.ROTATION_90: degrees = 90; break;
     case Surface.ROTATION_180: degrees = 180; break;
     case Surface.ROTATION_270: degrees = 270; break;
   }

   int result;
   if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
     result = (info.orientation + degrees) % 360;
     result = (360 - result) % 360;  // compensate the mirror
   } else {  // back-facing
     result = (info.orientation - degrees + 360) % 360;
   }
   camera.setDisplayOrientation(result);
}
+4

SurfaceTexture, . , :

, onPreviewFrame ( [], ), JPEG .

, GPU, OpenGL Texture. , 180 ° , OpenGL , :

  • 180 ° , .
  • -1 x y , .
  • UV
+1

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


All Articles