I\'m creating a tool to capture every frame from preview using cameraX (for face recognition purpose)
I found that using ImageAnalysis was the way to go.
Unt
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.
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.
If you don't like AsyncTask
, then I have found an alternative for getting an Executor
instance without using AsyncTask.THREAD_POOL_EXECUTOR
.
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.
You can also find the implementation in the official CameraX sample app: CameraFragment.kt.
The part which you need is this:
// Executor field
private lateinit var analysisExecutor: Executor
// in onCreate()
analysisExecutor = Executors.newSingleThreadExecutor()
// after initializing imageAnalysis
imageAnalysis.setAnalyzer(analysisExecutor, ImageAnalysis.Analyzer {
// TODO analyze
})
If you're wondering whether to use Executors.newSingleThreadExecutor()
, Executors.newFixedThreadPool(n)
or something else, take a look at the Executors documentation.