如何修复CameraX的旋转支持

5
我遇到了CameraX屏幕旋转支持的问题。
纵向: see picture 横向: see picure 变换代码:
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官方指南中获取了此代码。

非常感谢任何修复建议。祝您有一个愉快的一天!


我在你的代码中发现了一个错误。 - user6092898
@Roger,不是我... - Nikita Lapin
@Roger,能否回答我的问题?问题出在哪里?... - Nikita Lapin
请查看以下链接以获取有关如何在Python中检查文件是否存在的详细信息:https://dev59.com/OFsW5IYBdhLWcg3wFj5p#35431231 - user6092898
请您尝试一下并更新我好吗? - user6092898
@Roger,感谢您的帮助。我不像你建议给我的回答一样使用硬件包中的相机。我使用来自Android Jetpack的CameraX,所以很遗憾,我无法像那样操作相机... - Nikita Lapin
5个回答

4
基于 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
}

它在单侧工作正常,但如果我将设备旋转到另一侧,它会显示反向图像,你有什么想法吗? - Nidhi Desai
在viewCamera.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {中,我们将不会获得此参数: @Override public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) { // updateTransform(); } }); - Nidhi Desai

1

我曾经在使用CameraX Alpha 06时遇到了这个问题。后来我根据Marcin Mrugas的答案解决了它。 但是当我迁移到Alpha09时,我摆脱了这段代码,旋转也正常了。 因此看起来现在CameraX自己处理旋转。


1

添加以下代码

fun onCreate

viewFinder.addOnLayoutChangeListener { _, _, _, _, _, _, _, _, _ -> updateTransform(1920,1080) }

fun startCamera

preview.setOnPreviewOutputUpdateListener {

        val parent = viewFinder.parent as ViewGroup
        parent.removeView(viewFinder)
        parent.addView(viewFinder, 0)

        viewFinder.surfaceTexture = it.surfaceTexture
        updateTransform(1920,1080)
    }

更新变换(宽度:Int,高度: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中。


0

来自文档

override fun 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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接