Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android how to fix camera orientation

Notice how the view of the camera (NOT THE CAPTURED IMAGE) was flipped to left (image above), the orientation of the Activity is correct, but the camera view is messed up, please help me guys :) thank you.

Here is the XML layout file:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:orientation="vertical" >      <LinearLayout         android:layout_width="match_parent"         android:layout_height="match_parent"         android:orientation="vertical" >          <LinearLayout             android:layout_width="match_parent"             android:layout_height="match_parent"             android:gravity="center|top"             android:orientation="vertical" >              <SurfaceView                 android:id="@+id/camerapreview"                 android:layout_margin="10dp"                 android:layout_width="300dp"                 android:layout_height="300dp" />         </LinearLayout>     </LinearLayout>  </LinearLayout> 

And here is the code for the activity:

public class CustomCameraActivity extends Activity implements SurfaceHolder.Callback {      Camera camera;     SurfaceView surfaceView;     SurfaceHolder surfaceHolder;     boolean previewing = false;      @Override     public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         requestWindowFeature(Window.FEATURE_NO_TITLE);         setContentView(R.layout.camera);          surfaceView = (SurfaceView)findViewById(R.id.camerapreview);         surfaceHolder = surfaceView.getHolder();         surfaceHolder.addCallback(this);      }      @Override     public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {         if(previewing){             camera.stopPreview();             previewing = false;         }          if (camera != null){             try {                 camera.setPreviewDisplay(surfaceHolder);                 camera.startPreview();                 previewing = true;             } catch (IOException e) {                 e.printStackTrace();             }         }     }      @Override     public void surfaceCreated(SurfaceHolder holder) {         camera = Camera.open();     }      @Override     public void surfaceDestroyed(SurfaceHolder holder) {         camera.stopPreview();         camera.release();         camera = null;         previewing = false;     } } 
like image 811
CENT1PEDE Avatar asked Nov 19 '13 06:11

CENT1PEDE


People also ask

How do I fix the camera orientation on my Android?

Find and turn on the "Auto-rotate" tile in the quick-setting panel. You can also go to Settings > Display > Auto-rotate screen to turn it on. Your phone screen should rotate automatically now if nothing is wrong with the sensors.

Why is my camera app sideways?

The reason your photo would appear this way is because the photo was taken that way (either with the phone sideways or upside down) and the image file itself is in this orientation. For example, if you hold your phone upright and take a photo, the photo is saved in portrait mode or "sideways".


2 Answers

I found the solution here. Answer by @Ed Jellard.

i just have to add camera.setDisplayOrientation(90); on surfaceCreated(SurfaceHolder holder) method, now the display is on the right angle.

see the happy T-REX :)

like image 176
CENT1PEDE Avatar answered Sep 23 '22 06:09

CENT1PEDE


This problem was solved a long time ago but I encountered some difficulties to put all pieces together so here is my final solution, I hope this will help others :

public void startPreview() {         try {             Log.i(TAG, "starting preview: " + started);              // ....             Camera.CameraInfo camInfo = new Camera.CameraInfo();             Camera.getCameraInfo(cameraIndex, camInfo);             int cameraRotationOffset = camInfo.orientation;             // ...              Camera.Parameters parameters = camera.getParameters();             List<Camera.Size> previewSizes = parameters.getSupportedPreviewSizes();             Camera.Size previewSize = null;             float closestRatio = Float.MAX_VALUE;              int targetPreviewWidth = isLandscape() ? getWidth() : getHeight();             int targetPreviewHeight = isLandscape() ? getHeight() : getWidth();             float targetRatio = targetPreviewWidth / (float) targetPreviewHeight;              Log.v(TAG, "target size: " + targetPreviewWidth + " / " + targetPreviewHeight + " ratio:" + targetRatio);             for (Camera.Size candidateSize : previewSizes) {                 float whRatio = candidateSize.width / (float) candidateSize.height;                 if (previewSize == null || Math.abs(targetRatio - whRatio) < Math.abs(targetRatio - closestRatio)) {                     closestRatio = whRatio;                     previewSize = candidateSize;                 }             }              int rotation = getWindowManager().getDefaultDisplay().getRotation();             int degrees = 0;             switch (rotation) {             case Surface.ROTATION_0:                 degrees = 0;                 break; // Natural orientation             case Surface.ROTATION_90:                 degrees = 90;                 break; // Landscape left             case Surface.ROTATION_180:                 degrees = 180;                 break;// Upside down             case Surface.ROTATION_270:                 degrees = 270;                 break;// Landscape right             }             int displayRotation;             if (isFrontFacingCam) {                 displayRotation = (cameraRotationOffset + degrees) % 360;                 displayRotation = (360 - displayRotation) % 360; // compensate                                                                     // the                                                                     // mirror             } else { // back-facing                 displayRotation = (cameraRotationOffset - degrees + 360) % 360;             }              Log.v(TAG, "rotation cam / phone = displayRotation: " + cameraRotationOffset + " / " + degrees + " = "                     + displayRotation);              this.camera.setDisplayOrientation(displayRotation);              int rotate;             if (isFrontFacingCam) {                 rotate = (360 + cameraRotationOffset + degrees) % 360;             } else {                 rotate = (360 + cameraRotationOffset - degrees) % 360;             }              Log.v(TAG, "screenshot rotation: " + cameraRotationOffset + " / " + degrees + " = " + rotate);              Log.v(TAG, "preview size: " + previewSize.width + " / " + previewSize.height);             parameters.setPreviewSize(previewSize.width, previewSize.height);             parameters.setRotation(rotate);             camera.setParameters(parameters);             camera.setPreviewDisplay(mHolder);             camera.startPreview();              Log.d(TAG, "preview started");              started = true;         } catch (IOException e) {             Log.d(TAG, "Error setting camera preview: " + e.getMessage());         }     } 
like image 45
Louis GRIGNON Avatar answered Sep 19 '22 06:09

Louis GRIGNON