private class MyOrientationEventListener extends OrientationEventListener {
public MyOrientationEventListener(Context context) {
super(context);
}
@Override
public void onOrientationChanged(int orientation) {
if (orientation == ORIENTATION_UNKNOWN)
return;
CameraInfo info = new android.hardware.Camera.CameraInfo();
Camera.getCameraInfo(mCamera.mCurrentCameraId, info);
orientation = (orientation + 45) / 90 * 90;
int rotation = 0;
if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
rotation = (info.orientation - orientation + 360) % 360;
} else { // back-facing camera
rotation = (info.orientation + orientation) % 360;
}
mParameters.setRotation(rotation);
}
public MyOrientationEventListener(Context context) {
super(context);
}
@Override
public void onOrientationChanged(int orientation) {
if (orientation == ORIENTATION_UNKNOWN)
return;
CameraInfo info = new android.hardware.Camera.CameraInfo();
Camera.getCameraInfo(mCamera.mCurrentCameraId, info);
orientation = (orientation + 45) / 90 * 90;
int rotation = 0;
if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
rotation = (info.orientation - orientation + 360) % 360;
} else { // back-facing camera
rotation = (info.orientation + orientation) % 360;
}
mParameters.setRotation(rotation);
}
}
OrientationEventListener myOrientationEventListener;
@Override
protected void onResume() {
super.onResume();
myOrientationEventListener.enable();
}
@Override
protected void onPause() {
myOrientationEventListener.disable();
super.onPause();
}