Mur*_*iel 5 android kotlin android-camerax
对不起我的英语,我说得不好。
我有一个通过闪烁闪光灯与 arduino 设备通信的应用程序。我注意到 Camera1 和 Camera2 在所有 Android 设备上都存在问题,因此我构建了一个设置屏幕,用户可以测试两者并选择运行良好的一个。
我现在正在尝试与 CameraX 建立相同的通信,希望它适用于更多设备,但我找不到仅切换闪光灯的示例。我是android开发的新手,我找到的材料只是关于拍照之类的,但我什至不想打开相机屏幕,只需打开和关闭闪光灯,就像灯笼一样。
有人可以帮忙解决这个问题,或者发送一份有帮助的文档吗?
编辑1
我在 onCreate 中做了这个,我看到日志进入 logcat 但 flash 不切换。也许我需要创建案例?
lateinit var cameraControl: CameraControl
val cameraProcessFuture = ProcessCameraProvider.getInstance(this)
cameraProcessFuture.addListener(Runnable {
val cameraProvider = cameraProcessFuture.get()
val lifecycleOwner = this
val cameraSelector = CameraSelector.DEFAULT_BACK_CAMERA
val camera = cameraProvider.bindToLifecycle(lifecycleOwner, cameraSelector)
cameraControl = camera.cameraControl
val listenableFuture = cameraControl!!.enableTorch(true)
// cameraControl.enableTorch(false)
Log.d("MurilloTesteCamera", "listener")
listenableFuture.addListener(Runnable {
Log.d("MurilloTesteCamera", "listener 2")
}, ContextCompat.getMainExecutor(this))
}, ContextCompat.getMainExecutor(this))
Log.d("MurilloTesteCamera", "oncreate")
Run Code Online (Sandbox Code Playgroud)
编辑2
这段代码我试图创建一个案例,但没有解决我的问题并且闪光灯仍然没有打开(我的活动实现了 CameraXConfig.Provider:
val context = this
Log.d("MurilloTesteCamera", "before initialize")
CameraX.initialize(context, cameraXConfig).addListener(Runnable {
Log.d("MurilloTesteCamera", "inside initialize")
CameraX.unbindAll()
val preview = Preview.Builder()
.apply {
setTargetResolution(Size(640, 480))
}
.build()
lateinit var cameraControl: CameraControl
val cameraProcessFuture = ProcessCameraProvider.getInstance(context)
cameraProcessFuture.addListener(Runnable {
val cameraProvider = cameraProcessFuture.get()
val lifecycleOwner = context
val cameraSelector = CameraSelector.DEFAULT_BACK_CAMERA
val camera = cameraProvider.bindToLifecycle(lifecycleOwner, cameraSelector)
cameraControl = camera.cameraControl
camera.cameraInfo.hasFlashUnit()
Log.d("MurilloTesteCamera", "info before -> " + camera.cameraInfo.torchState)
Log.d("MurilloTesteCamera", "has flash -> " + camera.cameraInfo.hasFlashUnit())
val listenableFuture = cameraControl.enableTorch(true)
Log.d("MurilloTesteCamera", "listener")
listenableFuture.addListener(Runnable {
Log.d("MurilloTesteCamera", "info after -> " + camera.cameraInfo.torchState)
Log.d("MurilloTesteCamera", "listener 2")
}, ContextCompat.getMainExecutor(context))
CameraX.bindToLifecycle(context, cameraSelector, preview)
}, ContextCompat.getMainExecutor(context))
}, ContextCompat.getMainExecutor(context))
Log.d("MurilloTesteCamera", "after initialize")
while (!CameraX.isInitialized()){}
Log.d("MurilloTesteCamera", "after while")
Run Code Online (Sandbox Code Playgroud)
在 CameraX 中,用于打开/关闭相机手电筒的 API 是CameraControl.enableTorch(boolean). 要获取CameraControl实例,您可以按照文档进行操作:
应用程序可以通过 Camera.getCameraControl() 检索 CameraControl 实例。CameraControl 准备好在检索到 Camera 并将 UseCases 绑定到该相机后立即开始操作
这意味着您需要首先将一个用例(或多个用例)绑定到一个生命周期。你提到你不想open a camera screen,所以我假设你的意思是你不想绑定任何用例,在这种情况下你可以bindToLifecycle()用零用例调用。这可能适用于最新版本的 CameraX,也可能不适用。
总而言之,你必须写这样的东西:
val cameraProcessFuture = ProcessCameraProvider.getInstance(context)
cameraProcessFuture.addListener(Runnable {
val cameraProvider = cameraProcessFuture.get()
// Choose the lifecycle to which the camera will be attached to
val lifecycleOwner = /* Can be an Activity, Fragment, or a custom lifecycleOwner */
// Choose a valid camera the device has
val cameraSelector = CameraSelector.DEFAULT_BACK_CAMERA
// You might need to create a use case to start capture requests
// with the camera
val imageAnalysis = ImageAnalysis.Builder()
.build()
.apply {
val executor = /* Define an executor */
setAnalyzer(executor, ImageAnalysis.Analyzer {
it.close()
})
}
// Get a camera instance
val camera = cameraProvider.bindToLifecycle(lifecycleOwner, cameraSelector)
// Get a cameraControl instance
val cameraControl = camera.cameraControl
// Call enableTorch(), you can listen to the result to check whether it was successful
cameraControl.enableTorch(true) // enable torch
cameraControl.enableTorch(false) // disbale torch
}, ContextCompat.getMainExecutor(context))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1630 次 |
| 最近记录: |