ImageAnalysis.Analyzer 只触发一次

ImageAnalysis.Analyzer only fires once

在最新的 alpha (alpha08) 中,我似乎无法弄清楚如何正确配置所有内容,以便我的 Analyzer 正常运行。我可以看到它工作一次,然后就再也不会运行了。

出于各种原因,我需要使用TextureView,所以我不能换成CameraView,等等

我几乎可以肯定这是由于与期货有关的事情,但我似乎无法确定。

我没有想法。任何 thoughts/help 表示赞赏。

我已经配置了我的 Application class 如下:

override fun getCameraXConfig(): CameraXConfig {
    return Camera2Config.defaultConfig()
}

然后下面是我的MainActivity代码(布局只是ConstraintLayout里面的一个TextureView):

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.util.Size
import android.view.Surface
import android.view.TextureView
import androidx.camera.core.*
import androidx.camera.lifecycle.ProcessCameraProvider
import androidx.concurrent.futures.CallbackToFutureAdapter
import androidx.core.content.ContextCompat
import androidx.lifecycle.LifecycleOwner
import com.google.common.util.concurrent.ListenableFuture
import java.util.concurrent.Executors

class MainActivity : AppCompatActivity() {

    private lateinit var viewFinder: TextureView
    private lateinit var cameraProviderFuture : ListenableFuture<ProcessCameraProvider>
    private val executor = Executors.newSingleThreadExecutor()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        cameraProviderFuture = ProcessCameraProvider.getInstance(this)
        viewFinder = findViewById(R.id.view_finder)     // TextureView
        startCamera()
    }

    private fun startCamera() {
        val preview = Preview.Builder().apply {
            setTargetResolution(Size(640, 480))
        }.build()
        preview.setPreviewSurfaceProvider { resolution, surfaceReleaseFuture ->
            viewFinder.surfaceTexture.setDefaultBufferSize(resolution.width, resolution.height)
            val surface = Surface(viewFinder.surfaceTexture)
            surfaceReleaseFuture.addListener(
                Runnable {
                    surface.release()
                    viewFinder.surfaceTexture.release()
                },
                ContextCompat.getMainExecutor(this)
            )
            CallbackToFutureAdapter.getFuture<Surface> { completer -> completer.set(surface) }
        }

        val analyzer = ImageAnalysis.Builder().build()
        val analyzerUseCase = analyzer.apply {
            setAnalyzer(executor, MyTestAnalyzer())
        }

        val cameraSelector = CameraSelector.Builder().requireLensFacing(CameraSelector.LENS_FACING_BACK).build()
        cameraProviderFuture.addListener(Runnable {
            val cameraProvider = cameraProviderFuture.get()
            cameraProvider.bindToLifecycle(this as LifecycleOwner, cameraSelector, analyzerUseCase, preview)
        }, ContextCompat.getMainExecutor(this))
    }
}

private class MyTestAnalyzer : ImageAnalysis.Analyzer {
    override fun analyze(image: ImageProxy) {
        Log.d("Sandbox", "### Would analyze the image here ...")
    }
}

我决定在这里为后人回答我自己的问题,因为我从另一个渠道得到了答案。

最近对 API 进行了更改,这意味着您需要手动调用 ImageProxy#close。 CameraX 过去常常自动调用它。来自文档:

It is the responsibility of the application to close the image once done with it. If the images are not closed then it may block further images from being produced (causing the preview to stall) or drop images as determined by the configured backpressure strategy. The exact behavior is configurable via ImageAnalysis.Builder.setBackpressureStrategy(int).

此更改使您可以更灵活地进行帧分析(例如:多帧分析),因为您现在可以完全控制何时清除帧。

所以你的分析器代码应该是:

override fun analyze(image: ImageProxy) {
    Log.d("Sandbox", "### Would analyze the image here ...")
    image.close()
}

这里有完整的细节:https://developer.android.com/training/camerax/analyze