Rotating phone quickly 180 degrees, camera preview turns upside down

You can use OrientationEventListener to trigger recalculation of camera rotation.

Add to your activity:

private OrientationEventListener orientationListener = null;

to onCreate():

orientationListener = new OrientationEventListener(this) {
    public void onOrientationChanged(int orientation) {
        setCameraDisplayOrientation(CustomCameraActivity.this, cameraId, camera);
    }
};

to surfaceCreated():

orientationListener.enable();

to surfaceDestroyed():

orientationListener.disable();

Now, it almost works. To make setCameraDisplayOrientation() more robust,

  1. add check for camera != null
  2. only call camera.setDisplayOrientation(result) (or perform any heavy-lifting) if result changed since last time the function was called.

Leave a Comment