I faced with the issue of CameraX screen rotation support.
Portrait:
Landscape:
Transformation code:
private void updateTransform() {
Log.d(TAG, "updateTransform: ");
Matrix matrix = new Matrix();
float centerX = cameraViewTextureV.getWidth() / 2f;
float centerY = cameraViewTextureV.getHeight() / 2f;
switch (cameraViewTextureV.getDisplay().getRotation()) {
case Surface.ROTATION_0:
rotation = 0;
break;
case Surface.ROTATION_90:
rotation = 90;
break;
case Surface.ROTATION_180:
rotation = 180;
break;
case Surface.ROTATION_270:
rotation = 270;
break;
default:
break;
}
matrix.postRotate((float) -rotation, centerX, centerY);
cameraViewTextureV.setTransform(matrix);
}
So, as you can see in the pictures, camera support screen rotation not correctly... I calling updateTransform
method when screen rotating...
Took this code from the official guide for cameraX from Android developers site.
Will be very grateful for any proposes for fixing. Have a nice day!
There is a property in class Camera. CameraInfo named as orientation . It returns the integer. You can get the current orientation and then changed accordingly.
Display orientation. This refers to which side of the device is in the upward position, and can be one of four values: portrait, landscape, reverse portrait, or reverse landscape.
Most phone cameras are landscape, meaning if you take the photo in portrait, the resulting photos will be rotated 90 degrees. In this case, the camera software should populate the Exif data with the orientation that the photo should be viewed in.
The orientation of a Camera Station defines the position of and the pointing direction of the camera at the time of exposure of a photograph. Orientation can also refer to the process of determining the orientation of one or more Camera Stations.
Solution based on AutoFitPreviewBuilder:
preview.onPreviewOutputUpdateListener = Preview.OnPreviewOutputUpdateListener { output ->
// Get all dimensions
val metrics = DisplayMetrics().also { camera_texture_view.display.getRealMetrics(it) }
val previewWidth = metrics.widthPixels
val previewHeight = metrics.heightPixels
val width = output.textureSize.width
val height = output.textureSize.height
val centerX = camera_texture_view.width.toFloat() / 2
val centerY = camera_texture_view.height.toFloat() / 2
// Get rotation
val rotation = when (camera_texture_view.display.rotation) {
Surface.ROTATION_0 -> 0
Surface.ROTATION_90 -> 90
Surface.ROTATION_180 -> 180
Surface.ROTATION_270 -> 270
else -> throw IllegalStateException()
}
val matrix = Matrix()
// Rotate matrix
matrix.postRotate(-rotation.toFloat(), centerX, centerY)
// Scale matrix
matrix.postScale(
previewWidth.toFloat() / height,
previewHeight.toFloat() / width,
centerX,
centerY
)
// Assign transformation to view
camera_texture_view.setTransform(matrix)
camera_texture_view.surfaceTexture = output.surfaceTexture
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With