gpt4 book ai didi

android - 为什么 CameraX 不能在 Android Studio 中将图片放大到我需要的大小?

转载 作者:行者123 更新时间:2023-12-02 11:49:25 28 4
gpt4 key购买 nike

我正在学习 CameraX 和 CameraXBasic是一个示例代码。

我写了一个基于 CameraFragment.kt 的 zoomX 函数.你可以看到代码A。我认为该功能可以随时放大图片。

我发现当我用小值调用它时可以放大图片,例如zoomX(2f) , zoomX(3f) ,但是当我使用zoomX(6.0f)这样的大值时,图片不会再次放大, zoomX(7.0f) ... 为什么?

代码 A

   private lateinit var viewFinder: TextureView 
private var preview: Preview? = null

fun zoomX(orign: Float ){
val x=orign+1

val singleWidth=viewFinder.width/x
val singleHeight=viewFinder.height/x

val left=viewFinder.width/2f-singleWidth/2f
val right=left+singleWidth
val top=viewFinder.height/2f-singleHeight/2f
val bottom=top+singleHeight

val my= Rect(left.toInt(), top.toInt(), right.toInt(), bottom.toInt())
preview?.zoom(my)
}

最佳答案

这不是传统问题,而是Futures .更新 CameraXBasic来自 1.0.0-alpha06 的示例至1.0.0-alpha08坏了很多,但是 CameraX CameraControl具有以下两种方法:

  • ListenableFuture<Void> setLinearZoom (float linearZoom)

  • Sets current zoom by a linear zoom value ranging from 0f to 1.0f.

    linearZoom 0f represents the minimum zoom while linearZoom 1.0f represents the maximum zoom. The advantage of linearZoom is that it ensures the field of view (FOV) varies linearly with the linearZoom value, for use with slider UI elements (while setZoomRatio(float) works well for pinch-zoom gestures).


  • ListenableFuture<Void> setZoomRatio (float ratio)

  • Sets current zoom by ratio.

    It modifies both current zoomRatio and linearZoom so if apps are observing zoomRatio or linearZoom, they will get the update as well. If the ratio is smaller than CameraInfo.getMinZoomRatio() or larger than CameraInfo.getMaxZoomRatio(), the returned ListenableFuture will fail with IllegalArgumentException and it won't modify current zoom ratio. It is the applications' duty to clamp the ratio.


    另见 Executor还有 CameraXExecutors .

    另见 release notescommits ...对于所有破坏 CameraXBasic 示例的 API 更改。我不会再解释这些 API 差异(因为这不是问题),但是 have forked它;见 issues #131 (到目前为止,至少预览在那里工作)。

    这就是它的实际工作方式:
    val camera: Camera = cameraProvider.bindToLifecycle(this as LifecycleOwner, cameraSelector, preview)
    val control: CameraControl = camera.cameraControl
    control.setZoomRatio(5.0f)
    缩放比例来自 1.0f直到 8.0f在我的摩托罗拉 XT1900 上工作:
    val info: CameraInfo = camera.cameraInfo
    val cameraId = (info as Camera2CameraInfoImpl).cameraId
    val zoomRatio = info.getZoomRatio().value
    val maxZoomRatio = info.getMaxZoomRatio().value
    val minZoomRatio = info.getMinZoomRatio().value
    val linearZoom = info.getLinearZoom().value

    关于android - 为什么 CameraX 不能在 Android Studio 中将图片放大到我需要的大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58872770/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com