为什么 CameraX 无法在 Android Studio 中将图片放大到我需要的尺寸?
Why can't CameraX zoom in a picture as larger as I need in Android Studio?
我正在学习CameraX,CameraXBasic是示例代码。
我写了一个基于 CameraFragment.kt 的 zoomX 函数。你可以看到代码A。我认为这个功能可以随时放大图片。
我发现当我用一个小值调用它时可以放大图片,例如zoomX(2f)
,zoomX(3f)
,但是当我使用a时图片不会再次放大大值,例如 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)
}
这是Android传统,缩放级别存在于0
和1
之间,例如许多其他值..
0
表示最低缩放级别。
1
表示最大缩放级别。
它是一个浮点值,因此您可以增加它,例如 0.1
、0.2
、0.9
等等。
首先,找出相机的最大变焦值——使用getMaxZoom()
方法。
https://developer.android.com/reference/android/hardware/Camera.Parameters.html#getMaxZoom()
如果您使用过相机预览,则可以使用 Rect
在相机预览中设置缩放。如下所示 -
PreviewConfig config = new PreviewConfig.Builder().build();
Preview preview = new Preview(config);
Rect rect = Rect(left, top, right, bottom);
preview.zoom(rect)
preview.setOnPreviewOutputUpdateListener(
new Preview.OnPreviewOutputUpdateListener() {
@Override
public void onUpdated(Preview.PreviewOutput previewOutput) {
// Your code here. For example, use previewOutput.getSurfaceTexture()
// and post to a GL renderer.
};
});
CameraX.bindToLifecycle((LifecycleOwner) this, preview);
您可以从此处获得更多详细信息 - Android Camera preview zoom using double finger touch
这不是传统的问题,而是Futures
的问题。更新 CameraXBasic example from 1.0.0-alpha06
to 1.0.0-alpha08
breaks a lot, but CameraX CameraControl 具有以下两种方法:
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).
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 and there's also CameraXExecutors
。
另请参阅 release notes or the commits ...for all the API changes, which break the CameraXBasic example. I won't explain any more of these API differences (since this wasn't the question), but have forked it; see 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
我正在学习CameraX,CameraXBasic是示例代码。
我写了一个基于 CameraFragment.kt 的 zoomX 函数。你可以看到代码A。我认为这个功能可以随时放大图片。
我发现当我用一个小值调用它时可以放大图片,例如zoomX(2f)
,zoomX(3f)
,但是当我使用a时图片不会再次放大大值,例如 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)
}
这是Android传统,缩放级别存在于0
和1
之间,例如许多其他值..
0
表示最低缩放级别。
1
表示最大缩放级别。
它是一个浮点值,因此您可以增加它,例如 0.1
、0.2
、0.9
等等。
首先,找出相机的最大变焦值——使用getMaxZoom()
方法。
https://developer.android.com/reference/android/hardware/Camera.Parameters.html#getMaxZoom()
如果您使用过相机预览,则可以使用 Rect
在相机预览中设置缩放。如下所示 -
PreviewConfig config = new PreviewConfig.Builder().build();
Preview preview = new Preview(config);
Rect rect = Rect(left, top, right, bottom);
preview.zoom(rect)
preview.setOnPreviewOutputUpdateListener(
new Preview.OnPreviewOutputUpdateListener() {
@Override
public void onUpdated(Preview.PreviewOutput previewOutput) {
// Your code here. For example, use previewOutput.getSurfaceTexture()
// and post to a GL renderer.
};
});
CameraX.bindToLifecycle((LifecycleOwner) this, preview);
您可以从此处获得更多详细信息 - Android Camera preview zoom using double finger touch
这不是传统的问题,而是Futures
的问题。更新 CameraXBasic example from 1.0.0-alpha06
to 1.0.0-alpha08
breaks a lot, but CameraX CameraControl 具有以下两种方法:
Sets current zoom by a linear zoom value ranging from
0f
to1.0f
.
linearZoom 0f
represents the minimum zoom whilelinearZoom 1.0f
represents the maximum zoom. The advantage oflinearZoom
is that it ensures the field of view (FOV) varies linearly with thelinearZoom
value, for use with slider UI elements (whilesetZoomRatio(float)
works well for pinch-zoom gestures).
Sets current zoom by ratio.
It modifies both current
zoomRatio
andlinearZoom
so if apps are observingzoomRatio
orlinearZoom
, they will get the update as well. If the ratio is smaller thanCameraInfo.getMinZoomRatio()
or larger thanCameraInfo.getMaxZoomRatio()
, the returnedListenableFuture
will fail withIllegalArgumentException
and it won't modify current zoom ratio. It is the applications' duty to clamp the ratio.
另见 Executor and there's also CameraXExecutors
。
另请参阅 release notes or the commits ...for all the API changes, which break the CameraXBasic example. I won't explain any more of these API differences (since this wasn't the question), but have forked it; see 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