设置imageAnalysis.setAnalyzer()时发生编译错误 [英] Compile error while setting imageAnalysis.setAnalyzer()
问题描述
我正在创建一个工具,用于使用cameraX从预览中捕获每一帧(用于面部识别)
I'm creating a tool to capture every frame from preview using cameraX (for face recognition purpose)
我发现使用ImageAnalysis是必经之路.
I found that using ImageAnalysis was the way to go.
直到我尝试按照推荐的方式使用代码:
Until I tried using the code as recommended :
val imageAnalysisConfig = ImageAnalysisConfig.Builder()
.setTargetResolution(Size(1280, 720))
.setImageReaderMode(ImageAnalysis.ImageReaderMode.ACQUIRE_LATEST_IMAGE)
.build()
val imageAnalysis = ImageAnalysis(imageAnalysisConfig)
imageAnalysis.setAnalyzer({ image: ImageProxy, rotationDegrees: Int ->
viewModel.onAnalyzeImage(image)
})
我在setAnalyser方法上收到以下编译错误:
To which I get the following compile error on setAnalyser method :
None of the following function can be called with the arguments supplied
setAnalyser((command : Runnable!) -> Unit, (image: ImageProxy!, rotationDegrees: Int) -> Unit)
推荐答案
我今天也面临着相同的问题,因此,发现有一个缺少的参数是Executor
,我们需要传递该参数,否则会出现相同的编译错误
I was also facing the same issue today, so , found that there is one missing parameter which is Executor
which we need to pass otherwise same compilation error comes.
就像我过去使用AsyncTasks
一样,我认识到要在AsyncTasks
中的多个线程中执行任务,我们需要使用其静态方法executeOnExecutor()
,该方法将Executor
作为其参数,因此我使用了相同的参数,即在setAnalyser()
方法中将AsyncTask.THREAD_POOL_EXECUTOR
用作第一个参数.它就像一个魅力!将其作为第一个参数后,您需要在先前的代码中进行一些小的更改.
As as I worked with AsyncTasks
in my past, I recognized that for doing tasks in multiple threads in AsyncTasks
, we need to use its static method executeOnExecutor()
which takes an Executor
as its parameter, so I used the same parameter i.e. I used AsyncTask.THREAD_POOL_EXECUTOR
as first parameter in setAnalyser()
method. And it worked like a charm!! After putting this as first parameter you need to perform some minor changes in your previous code.
imageAnalysis.setAnalyzer(AsyncTask.THREAD_POOL_EXECUTOR,
object : ImageAnalysis.Analyzer { // changes to be done in this line
override fun analyze(imageProxy: ImageProxy, rotationDegrees: Int) {
val image = FirebaseVisionImage.fromMediaImage(
imageProxy.image!!, getFirebaseRotation(rotationDegrees)
)
if (processingBarcode.get() ||
!lifecycle.currentState.isAtLeast(Lifecycle.State.RESUMED)) {
return
}
..................
.............
.......BLA BLA BLA
}
尝试一下,并告诉我这种方法是否适用于您的用例.
Try it and tell me if this approach works with your use case.
如果您不喜欢AsyncTask
,那么我找到了一种无需使用AsyncTask.THREAD_POOL_EXECUTOR
即可获取Executor
实例的替代方法.
If you don't like AsyncTask
, then I have found an alternative for getting an Executor
instance without using AsyncTask.THREAD_POOL_EXECUTOR
.
您可以使用Executors.newFixedThreadPool(n)
来获取Executor
实例.
在这里,n代表您要在线程池中创建的线程数.它随您的用例而变化.
You can use Executors.newFixedThreadPool(n)
, to get an Executor
instance.
Here, n stands for the number of threads you want to create in the thread pool.It varies depending upon your use-case.
告诉我它是否对您有用.
Tell me if it worked for you.
这篇关于设置imageAnalysis.setAnalyzer()时发生编译错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!