对不起,我的英语说得不好。
我有一个应用程序,通过闪烁闪光灯与arduino设备进行通信。我注意到Camera1和Camera2在所有安卓设备上都有问题,所以我构建了一个设置屏幕,用户可以测试这两种设置,并选择一个工作正常的。
我现在正试图与CameraX建立同样的通信,希望它能在更多的设备中正常工作,但我找不到仅仅切换闪存的例子。我是android开发部的新手,我发现的材料只是关于拍照之类的,但我甚至不想打开相机屏幕,只要打开或关闭闪光灯,就像灯笼一样。
有人能帮上忙吗,或者发送一份帮助你的文档?
edit1
我在onCreate中这样做,我看到日志进入logcat,但闪存不切换。也许我需要造个案子?
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")
edit2
这段代码我试图创建一个案例,但没有解决我的问题,闪存也没有打开(我的活动实现了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")
发布于 2020-05-13 08:02:31
在CameraX中,打开/关闭相机火炬的API是CameraControl.enableTorch(boolean)
。要获得一个CameraControl
实例,您可以遵循以下文档:
应用程序可以通过Camera.getCameraControl()检索CameraControl实例。CameraControl已准备好在获取摄像机并将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))
https://stackoverflow.com/questions/61777177
复制相似问题