android - 如何使用 Android CameraX 自动对焦
问题描述
最近几个月,Android 发布了一个新的 API camerax。我试图了解如何让相机自动对焦。
https://groups.google.com/a/android.com/forum/#!searchin/camerax-developers/auto $20focus|sort:date/camerax-developers/IQ3KZd8iOIY/LIbrRIqEBgAJ
这是关于该主题的讨论,但几乎没有具体的文档。
这也是基本的camerax应用程序,但我找不到任何处理自动对焦的文件。
任何有关文档的提示或要点都是有帮助的。此外,我对 android 还很陌生,所以很可能我遗漏了一些使上述链接更有用的东西。
解决方案
使用当前的 CameraX 1.0.0
,您可以通过以下两种方式进行操作:
每 X 秒自动对焦:
previewView.afterMeasured { val autoFocusPoint = SurfaceOrientedMeteringPointFactory(1f, 1f) .createPoint(.5f, .5f) try { val autoFocusAction = FocusMeteringAction.Builder( autoFocusPoint, FocusMeteringAction.FLAG_AF ).apply { //start auto-focusing after 2 seconds setAutoCancelDuration(2, TimeUnit.SECONDS) }.build() camera.cameraControl.startFocusAndMetering(autoFocusAction) } catch (e: CameraInfoUnavailableException) { Log.d("ERROR", "cannot access camera", e) } }
专注于点击:
previewView.afterMeasured { previewView.setOnTouchListener { _, event -> return@setOnTouchListener when (event.action) { MotionEvent.ACTION_DOWN -> { true } MotionEvent.ACTION_UP -> { val factory: MeteringPointFactory = SurfaceOrientedMeteringPointFactory( previewView.width.toFloat(), previewView.height.toFloat() ) val autoFocusPoint = factory.createPoint(event.x, event.y) try { camera.cameraControl.startFocusAndMetering( FocusMeteringAction.Builder( autoFocusPoint, FocusMeteringAction.FLAG_AF ).apply { //focus only when the user tap the preview disableAutoCancel() }.build() ) } catch (e: CameraInfoUnavailableException) { Log.d("ERROR", "cannot access camera", e) } true } else -> false // Unhandled event. } } }
afterMeasured扩展功能是一个简单的实用程序:(感谢 ch271828n改进它)
inline fun View.afterMeasured(crossinline block: () -> Unit) {
if (measuredWidth > 0 && measuredHeight > 0) {
block()
} else {
viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.GlobalLayoutListener {
override fun onGlobalLayout() {
if (measuredWidth > 0 && measuredHeight > 0) {
viewTreeObserver.removeOnGlobalLayoutListener(this)
block()
}
}
})
}
}
Camera
可以通过以下方式获得对象
val camera = cameraProvider.bindToLifecycle(
this@Activity, cameraSelector, previewView //this is a PreviewView
)
推荐阅读
- authentication - 使用 Git Bash 连接到 Google Cloud (gcloud) 时出现身份验证错误
- arrays - 使用 FiddlerScript 的 OnBeforeResponse 更改数组中元素的属性
- ios - 在加载 WKWebView 之前尝试获取 CLLocation 坐标
- ios - 使用 swift 4 在 WKWebView 中请求库的权限
- outlook - Outlook 2016 - 加载项对话框关闭时崩溃
- nightwatch.js - 如何通过黄瓜集成在 nightwatch.js 中运行特定功能
- python - 连接单列多个不同形状的文件
- android - 尝试在 Android Studio 中更改字体时出错
- kubernetes - 私有子网中的本地 Kubernetes 集群
- api - 如何从 Google Photos API 播放视频