android - ImageAnalysis.Analyzer 只触发一次

标签 android android-camerax

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

由于种种原因我需要使用 TextureView所以我无法切换到 CameraView .

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

我的想法很新鲜。任何想法/帮助表示赞赏。

我已经配置了我的 Application类具有以下内容:

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

然后下面是我的MainActivity代码(布局只是 TextureView 内的单个 ConstraintLayout ):
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 ...")
    }
}

最佳答案

当我从另一个 channel 得到答案时,我决定在这里为后代回答我自己的问题。

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).



此更改为您如何进行帧分析(例如:多帧分析)提供了更大的灵 active ,因为您现在可以完全控制何时清除帧。

所以你的分析器代码应该是:
override fun analyze(image: ImageProxy) {
    Log.d("Sandbox", "### Would analyze the image here ...")
    image.close()
}

完整详情:https://developer.android.com/training/camerax/analyze .

关于android - ImageAnalysis.Analyzer 只触发一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59606400/

相关文章:

android - 打开新 fragment 时取消绑定(bind)/关闭所有cameraX组件

android - 以编程方式访问 <declare-styleable> 资源

android - 我如何告诉 ProGuard 保留用于 onClick 的函数?

java - oninfowindowclick 仅在有多个标记的情况下与标记信息一起使用

java - 设置 TextView 颜色的最有效方法

android - 如何在 CameraX 中使用 Camera2Config.Extender

java - 在 Android 应用程序中使用 Gmail/Facebook 凭据注册

android - 在附加的堆栈跟踪中获取了资源但从未释放 - kotlin

java - 相机X : How to call the analyze method with an image

android - 如何获取 CameraControl 和 CameraInfo