Why can't CameraX zoom in a picture as larger as I need in Android Studio?
This is not a matter of tradition, but of 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:
ListenableFuture<Void> setLinearZoom (float linearZoom)
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).
ListenableFuture<Void> setZoomRatio (float ratio)
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.
Also see Executor and there's also CameraXExecutors
.
Also see the 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 (so far, at least the preview works there).
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:
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