Compile error while setting imageAnalysis.setAnalyzer()

前端 未结 2 474
梦如初夏
梦如初夏 2021-01-18 07:18

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

相关标签:
2条回答
  • 2021-01-18 08:14

    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.

    Like this

     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.

    EDIT

    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.

    0 讨论(0)
  • 2021-01-18 08:14

    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.

    0 讨论(0)
提交回复
热议问题