【问题标题】:How to set a correct aspect ratio using CameraX?如何使用 CameraX 设置正确的纵横比?
【发布时间】:2019-05-22 23:36:40
【问题描述】:

我正在关注 CameraX codelab,即使使用 setTargetAspectRatio 和 setTargetResolution 方法,我在预览中也得到了错误的纵横比。

private fun startCamera() {
    // Create configuration object for the viewfinder use case
    val previewConfig = PreviewConfig.Builder().apply {
        setTargetAspectRatio(Rational(1, 1))
        setTargetResolution(Size(640, 640))
    }.build()
    ...

并且布局使用的是 codelab 中提供的硬编码大小。

<TextureView
    android:id="@+id/view_finder"
    android:layout_width="640px"
    android:layout_height="640px"
    ...

如果库有CameraTextureView 和属性android:scaleType(类似于ImageView 的现有属性)来将预览调整为预览大小,那就太好了。

【问题讨论】:

    标签: android android-jetpack android-camerax


    【解决方案1】:

    你试过了吗?

        val metrics = DisplayMetrics().also { viewFinder.display.getRealMetrics(it) }
        val screenSize = Size(metrics.widthPixels, metrics.heightPixels)
        val screenAspectRatio = Rational(metrics.widthPixels, metrics.heightPixels)
    
        val viewFinderConfig = PreviewConfig.Builder().apply {
                //...
                setTargetResolution(screenSize)
                setTargetAspectRatio(screenAspectRatio)
                setTargetRotation(viewFinder.display.rotation)
            }.build()
    
        val preview = AutoFitPreviewBuilder.build(viewFinderConfig, viewFinder)
    

    您可以在这里找到 AutoFitPreviewBuilder: https://gist.github.com/yevhenRoman/90681822adef43350844464be95d23f1

    我建议您使用 dp 或约束来设置 TextureView 的宽度和高度。 让我知道它是否适合您,谢谢!

    【讨论】:

    • 对不起。也没有工作:( 预览剧照拉伸...关于视图大小,我知道 dp 是推荐的方法,我只是按照代码实验室。
    • 奇怪的事情。它是 alpha 版本,也许它现在不适用于某些设备。希望你能解决这个问题,祝你好运!
    • @nglauber 您是否尝试过使用示例代码?如果你克隆并按原样运行会发生什么:github.com/android/camera/tree/master/CameraXBasic
    • @OscarWahltinez,示例代码运行良好。在比较两者之后,我注意到当我删除这个调用preview.setOnPreviewOutputUpdateListener 时,预览不再被拉伸。谢谢你的提示。但是,整个预览(一个矩形)位于视图(一个矩形)内部,类似于 ImageView 的 CENTER_INSIDE 比例类型,我想要类似于 CENTER_CROP 的东西。这带来了我的问题的第二点:如果有一个 CameraTextureView,我们可以在其中设置相机预览的比例类型,那就太好了。
    • @nglauber 很好,我花了 5 个小时试图理解为什么会被拉长。删除 setOnPreview 成功了
    【解决方案2】:

    https://stackoverflow.com/a/49449986/9397052 有类似问题的解决方案。决定解决方案后,不要使用setTargetAspectRatio 函数;相反,您应该只使用setTargetResolution。然后它应该工作相同。

    【讨论】:

    • 问题是关于cameraX而不是camera 2。而且它对我没有用。
    【解决方案3】:

    根据安卓官方documentation: "不允许在同一个用例上同时设置目标纵横比和目标分辨率。"

    尽管如果您尝试这样做编译器不会抛出错误,但可能会出现意外结果。

    【讨论】:

      【解决方案4】:

      您可以像这样设置TextureView 和Matrix 的纵横比:

      Matrix txform = new Matrix();
      textureView.getTransform(txform);
      txform.setScale((float) = videoWidth / viewWidth, (float) videoHeight / viewHeight);// aspect ratio
      textureView.setTransform(txform); // apply matrix
      

      【讨论】:

      • 你看过我提到的codelab吗?好吧,如果你看一下第 6 步,updateTransform 正在更新 TextureView。我使用matrix.setScale(960f / 640, 720f / 640)(960x720 是有效的相机分辨率,纹理大小为 640x640)将您的建议应用于此方法,但没有奏效。预览剧照被拉长。
      • 我认为拉伸的预览图像可能是FixedSizeSurfaceTexture 的结果,它由Preview.PreviewOutput 返回。它的纵横比与其附加的预览不同。所以我尝试按照您已经提到的方式对其进行缩放:matrix.setScale((float) = videoWidth / viewWidth, (float) videoHeight / viewHeight),这并没有解决拉伸效果
      猜你喜欢
      • 2021-10-29
      • 2021-05-24
      • 2020-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-15
      • 2015-05-02
      相关资源
      最近更新 更多