gpt4 book ai didi

android - ImageAnalysis.Analyzer 只触发一次

转载 作者:行者123 更新时间:2023-12-05 00:09:53 25 4
gpt4 key购买 nike

在最新的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com