我遇到了CameraX屏幕旋转支持的问题。
肖像:
风景:
转换代码:
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);
}
因此,如您在图片中所见,相机支持屏幕旋转不正确...屏幕旋转时我调用updateTransform
方法...
从Android开发人员网站上的cameraX官方指南中获取了此代码。
非常感谢您提出修复建议。祝你有美好的一天!
答案 0 :(得分:0)
从官方的Android Camera X示例项目尝试以下转换方法: https://github.com/android/camera/blob/master/CameraXBasic/app/src/main/java/com/android/example/cameraxbasic/utils/AutoFitPreviewBuilder.kt#L132
我希望这会有所帮助。
答案 1 :(得分:0)
基于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
}
答案 2 :(得分:0)
添加此代码
有趣的onCreate
viewFinder.addOnLayoutChangeListener { _, _, _, _, _, _, _, _, _ ->
updateTransform(1920,1080)
}
有趣的startCamera
preview.setOnPreviewOutputUpdateListener {
val parent = viewFinder.parent as ViewGroup
parent.removeView(viewFinder)
parent.addView(viewFinder, 0)
viewFinder.surfaceTexture = it.surfaceTexture
updateTransform(1920,1080)
}
有趣的updateTransform(width:Int,height:Int)
val metrics = DisplayMetrics().also { viewFinder.display.getRealMetrics(it) }
val previewWidth = metrics.widthPixels
val previewHeight = metrics.heightPixels
matrix.postScale(previewWidth.toFloat() / height,previewHeight.toFloat() /width,centerX,centerY)
我的设备的分辨率为1920x1080
完整的代码在我的存储库CameraX
中答案 3 :(得分:0)
我在CameraX Alpha 06上遇到了这个问题。然后我用Marcin Mrugas的答案解决了这个问题。 但是,当迁移到Alpha09时,我摆脱了这段代码,轮换可以了。 因此,似乎CameraX现在可以自行处理旋转。
答案 4 :(得分:0)
来自文档
重写乐趣onCreate(){ val imageCapture = ImageCapture.Builder()。build()
val orientationEventListener = object : OrientationEventListener(this as Context) {
override fun onOrientationChanged(orientation : Int) {
// Monitors orientation values to determine the target rotation value
val rotation : Int = when (orientation) {
in 45..134 -> Surface.ROTATION_270
in 135..224 -> Surface.ROTATION_180
in 225..314 -> Surface.ROTATION_90
else -> Surface.ROTATION_0
}
imageCapture.targetRotation = rotation
}
}
orientationEventListener.enable()
}
这里是链接 https://developer.android.com/training/camerax/configuration