[英]Why can't CameraX zoom in a picture as larger as I need in Android Studio?
I'm learning CameraX, and CameraXBasic is a sample code.我正在学习CameraX, CameraXBasic是一个示例代码。
I write a zoomX function based CameraFragment.kt .我写了一个基于 ZoomX function 的CameraFragment.kt 。 you can see the Code A. I think the function can zoom in a picture any time.
你可以看到代码A。我认为function可以随时放大图片。
I find that a picture can be zoom in when I invoke it with sa small value, such as zoomX(2f)
, zoomX(3f)
, but the picture will not be zoom in again when I use a big value such as zoomX(6.0f)
, zoomX(7.0f)
... why?我发现当我用 sa 小值调用它时可以放大图片,例如
zoomX(2f)
, zoomX(3f)
,但是当我使用大值例如zoomX(6.0f)
) 时,图片不会再次放大zoomX(6.0f)
, zoomX(7.0f)
... 为什么?
Code A代码 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)
}
This is not a matter of tradition, but of Futures
.这不是传统的问题,而是
Futures
的问题。 Updating that CameraXBasic example from 1.0.0-alpha06
to 1.0.0-alpha08
breaks a lot, but CameraX CameraControl features these two methods:将CameraXBasic示例从
1.0.0-alpha06
更新到1.0.0-alpha08
会破坏很多,但 CameraX CameraControl具有以下两种方法:
Sets current zoom by a linear zoom value ranging from
0f
to1.0f
.通过范围从
0f
到1.0f
的线性缩放值设置当前缩放。
linearZoom 0f
represents the minimum zoom whilelinearZoom 1.0f
represents the maximum zoom.linearZoom 0f
代表最小缩放,而linearZoom 1.0f
代表最大缩放。 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).linearZoom
的优势在于它确保视野 (FOV) 随linearZoom
值线性变化,适用于 slider UI 元素(而setZoomRatio(float)
适用于捏缩放手势)。
Sets current zoom by ratio.
按比例设置当前缩放。
It modifies both current
zoomRatio
andlinearZoom
so if apps are observingzoomRatio
orlinearZoom
, they will get the update as well.它同时修改了当前的
zoomRatio
和linearZoom
,因此如果应用程序正在观察zoomRatio
或linearZoom
,它们也会得到更新。 If the ratio is smaller thanCameraInfo.getMinZoomRatio()
or larger thanCameraInfo.getMaxZoomRatio()
, the returnedListenableFuture
will fail withIllegalArgumentException
and it won't modify current zoom ratio.如果比率小于
CameraInfo.getMinZoomRatio()
或大于CameraInfo.getMaxZoomRatio()
,则返回的ListenableFuture
将失败并出现IllegalArgumentException
并且不会修改当前缩放比率。 It is the applications' duty to clamp the ratio.限制比率是应用程序的职责。
Also see Executor and there's also CameraXExecutors
.另请参阅Executor并且还有
CameraXExecutors
。
Also see the release notes or the commits ...for all the API changes, which break the CameraXBasic example.另请参阅 发行说明或提交...对于所有 API 更改,这些更改破坏了 CameraXBasic 示例。 I won't explain any more of these API differences (since this wasn't the question), but have forked it;
我不会再解释这些 API 差异(因为这不是问题),但已经分叉了; see issues #131 (so far, at least the preview works there).
请参阅问题 #131 (到目前为止,至少可以在那里进行预览)。
This is how it actually works:这就是它的实际工作方式:
val camera: Camera = cameraProvider.bindToLifecycle(this as LifecycleOwner, cameraSelector, preview)
val control: CameraControl = camera.cameraControl
control.setZoomRatio(5.0f)
Zoom ratios from 1.0f
until 8.0f
work on my Motorola XT1900:从
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
It's a Android tradition, zoom level exist between 0
and 1
like many other values..这是 Android 的传统,缩放级别像许多其他值一样存在于
0
和1
之间。
0
mean the lowest zoom level. 0
表示最低缩放级别。
1
mean the maximum zoom level. 1
表示最大缩放级别。
It's a float value so you increase it like 0.1
, 0.2
, 0.9
and so on.这是一个浮点值,因此您可以像
0.1
、 0.2
、 0.9
等一样增加它。
First of all, find out the max zoom value of the camera - using getMaxZoom()
method.首先,找出相机的最大缩放值 - 使用
getMaxZoom()
方法。
https://developer.android.com/reference/android/hardware/Camera.Parameters.html#getMaxZoom() https://developer.android.com/reference/android/hardware/Camera.Parameters.html#getMaxZoom()
If you have used Camera Preview then you can set zoom in Camera preview using Rect
.如果您使用过 Camera Preview,那么您可以使用
Rect
在 Camera preview 中设置缩放。 Like below -如下图——
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);
You may get more details from here - Android Camera preview zoom using double finger touch您可以从这里获得更多详细信息 - Android 使用双指触摸的相机预览缩放
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.