[英]How can I freeze picture of camera with CameraX API in Android Studio?
[英]Why can't CameraX zoom in a picture as larger as I need in Android Studio?
我正在學習CameraX, CameraXBasic是一個示例代碼。
我寫了一個基於 ZoomX function 的CameraFragment.kt 。 你可以看到代碼A。我認為function可以隨時放大圖片。
我發現當我用 sa 小值調用它時可以放大圖片,例如zoomX(2f)
, zoomX(3f)
,但是當我使用大值例如zoomX(6.0f)
) 時,圖片不會再次放大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具有以下兩種方法:
通過范圍從
0f
到1.0f
的線性縮放值設置當前縮放。
linearZoom 0f
代表最小縮放,而linearZoom 1.0f
代表最大縮放。linearZoom
的優勢在於它確保視野 (FOV) 隨linearZoom
值線性變化,適用於 slider UI 元素(而setZoomRatio(float)
適用於捏縮放手勢)。
按比例設置當前縮放。
它同時修改了當前的
zoomRatio
和linearZoom
,因此如果應用程序正在觀察zoomRatio
或linearZoom
,它們也會得到更新。 如果比率小於CameraInfo.getMinZoomRatio()
或大於CameraInfo.getMaxZoomRatio()
,則返回的ListenableFuture
將失敗並出現IllegalArgumentException
並且不會修改當前縮放比率。 限制比率是應用程序的職責。
另請參閱Executor並且還有CameraXExecutors
。
另請參閱 發行說明或提交...對於所有 API 更改,這些更改破壞了 CameraXBasic 示例。 我不會再解釋這些 API 差異(因為這不是問題),但已經分叉了; 請參閱問題 #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 的傳統,縮放級別像許多其他值一樣存在於0
和1
之間。
0
表示最低縮放級別。
1
表示最大縮放級別。
這是一個浮點值,因此您可以像0.1
、 0.2
、 0.9
等一樣增加它。
首先,找出相機的最大縮放值 - 使用getMaxZoom()
方法。
https://developer.android.com/reference/android/hardware/Camera.Parameters.html#getMaxZoom()
如果您使用過 Camera Preview,那么您可以使用Rect
在 Camera preview 中設置縮放。 如下圖——
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 使用雙指觸摸的相機預覽縮放
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.