The orientation of the preview depends on the orientation of the device and the orientation of the camera.
Basically, you need to calculate the orientation of the cameraโs preview based on these conditions.
We need two help methods:
1 - Calculate the orientation of the device:
public int getDeviceOrientation(Context context) {
int degrees = 0;
WindowManager windowManager =
(WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
int rotation = windowManager.getDefaultDisplay().getRotation();
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;
}
return degrees;
}
2 - :
public static int getPreviewOrientation(Context context, int cameraId) {
int temp = 0;
int previewOrientation = 0;
Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
Camera.getCameraInfo(cameraId, cameraInfo);
int deviceOrientation = getDeviceOrientation(context);
temp = cameraInfo.orientation - deviceOrientation + 360;
previewOrientation = temp % 360;
return previewOrientation;
}
mediaRecorder.prepare();
int rotation = getPreviewOrientation(context, cameraId);
mediaRecorder.setOrientationHint(rotation);
.