Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set Android camera orientation properly?

I want to set the camera orientation according to the device orientation in Android but nothing seems to be working. I tried to rotate the Surface as well as the camera parameters but the camera preview in portrait mode always comes upside down. I would need to rotate it by 90 degree clockwise for it to be correct. Here is the code I am using right now which works in landscape mode only.

    SurfaceHolder.Callback surfaceCallback = new SurfaceHolder.Callback() {      @Override     public void surfaceDestroyed(SurfaceHolder holder) {         camera.stopPreview();         camera.release();         camera = null;     }      @Override     public void surfaceCreated(SurfaceHolder holder) {                   initCamera();                }      private Size getOptimalPreviewSize(List<Size> sizes, int w, int h) {         final double ASPECT_TOLERANCE = 0.2;         double targetRatio = (double) w / h;         if (sizes == null)             return null;          Size optimalSize = null;         double minDiff = Double.MAX_VALUE;          int targetHeight = h;          // Try to find an size match aspect ratio and size         for (Size size : sizes) {             Log.d(TAG, "Checking size " + size.width + "w " + size.height                     + "h");             double ratio = (double) size.width / size.height;             if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE)                 continue;             if (Math.abs(size.height - targetHeight) < minDiff) {                 optimalSize = size;                 minDiff = Math.abs(size.height - targetHeight);             }         }          // Cannot find the one match the aspect ratio, ignore the         // requirement         if (optimalSize == null) {             minDiff = Double.MAX_VALUE;             for (Size size : sizes) {                 if (Math.abs(size.height - targetHeight) < minDiff) {                     optimalSize = size;                     minDiff = Math.abs(size.height - targetHeight);                 }             }         }         return optimalSize;     }      @Override     public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {         Camera.Parameters parameters = camera.getParameters();          List<Size> sizes = parameters.getSupportedPreviewSizes();         Size optimalSize = getOptimalPreviewSize(sizes, width, height);                  Log.d(TAG, "Surface size is " + width + "w " + height + "h");         Log.d(TAG, "Optimal size is " + optimalSize.width + "w " + optimalSize.height + "h");                    parameters.setPreviewSize(optimalSize.width, optimalSize.height);                    // parameters.setPreviewSize(width, height);                     camera.setParameters(parameters);         camera.startPreview();     } };   
like image 718
Abhinav Avatar asked Jan 10 '11 10:01

Abhinav


People also ask

How do I change the camera orientation on my android?

The expression deviceOrientationDegrees * sign + 360 converts device rotation from counterclockwise to clockwise for back-facing cameras (for example, converting 270 degrees counterclockwise to 90 degrees clockwise).

How do I fix the orientation on my android phone?

To do this, swipe down from the right side of the top panel. Hold the device in the orientation in which you want it locked. On the drop-down menu, touch the “Auto Rotate” button. The “Auto Rotate” button becomes the “Rotation Locked” button.


2 Answers

From other member and my problem:

Camera Rotation issue depend on different Devices and certain Version.

Version 1.6: to fix the Rotation Issue, and it is good for most of devices

if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)         {                p.set("orientation", "portrait");             p.set("rotation",90);         }         if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE)         {                                            p.set("orientation", "landscape");                       p.set("rotation", 90);         } 

Version 2.1: depend on kind of devices, for example, Cannt fix the issue with XPeria X10, but it is good for X8, and Mini

Camera.Parameters parameters = camera.getParameters(); parameters.set("orientation", "portrait"); camera.setParameters(parameters); 

Version 2.2: not for all devices

camera.setDisplayOrientation(90); 

http://code.google.com/p/android/issues/detail?id=1193#c42

like image 182
LTEHUB Avatar answered Oct 06 '22 21:10

LTEHUB


From the Javadocs for setDisplayOrientation(int) (Requires API level 9):

 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);  } 
like image 22
Jason Robinson Avatar answered Oct 06 '22 21:10

Jason Robinson