android - 如何修复 CameraX 旋转支持
问题描述
我遇到了 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官方指南中获取此代码。
将非常感谢任何修复建议。祝你今天过得愉快!
解决方案
基于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
}
推荐阅读
- linkedin - Linkedin API:adAnalyticsV2 显示展示次数随着时间的推移而减少
- python - 从python中的两个字符串中一一删除字母
- react-native - 添加代码推送以响应本机 appcenter 应用程序
- javascript - 修复 windows.location.hostname 的 XSS 问题
- twilio - 在进行出站呼叫时将自定义呼叫者 ID 设置为 twilio 号码
- go - 如何使用接受 multipart/form-data 的 goDoc 记录我的服务端点
- java - 有没有办法在 Adapter 类之外检查 RecyclerView 类是否为空?
- redis - 如何获取 Redis 集群中每个客户端的连接数
- c++ - 我的 C++ hello world 程序无法使用 Build 脚本在 Atom IDE 中编译
- android - 将 Hilt @IntallIn 用于具有静态提供方法的 dagger-2 模块