我正在尝试将ProxyImage从cameraX分析器转换为位图,使用张量流光来分析图像。因此,我实现了cameraX分析回调,它将图像作为proxyImage。我需要转换成位图的proxyImage。如果我在UI线程上做这个对话,它会使相机预览滞后。所以我想用Coroutines来做。现在的问题是,每当我将proxyImage传递给coroutines以将其转换为后台线程上的位图时,它就会与“图像已经关闭”的IllegalStateException崩溃。
08-04 16:28:59.690 16185-16185/com.example.camerax E/libEGL: call to OpenGL ES API with no current context (logged once per thread)
08-04 16:29:00.849 16185-16308/com.example.camerax E/AndroidRuntime: FATAL EXCEPTION: DefaultDispatcher-worker-1
Process: com.example.camerax, PID: 16185
java.lang.IllegalStateException: Image is already closed
at android.media.Image.throwISEIfImageIsInvalid(Image.java:68)
at android.media.ImageReader$SurfaceImage$SurfacePlane.getBuffer(ImageReader.java:787)
at androidx.camera.core.AndroidImageProxy$PlaneProxy.getBuffer(AndroidImageProxy.java:141)
at com.example.camerax.MainActivity.getImageFromProxy(MainActivity.kt:216)
at com.example.camerax.MainActivity.convertProxyImageToBitmap(MainActivity.kt:150)
at com.example.camerax.MainActivity.access$convertProxyImageToBitmap(MainActivity.kt:38)
at com.example.camerax.MainActivity$startCamera$3$1.invokeSuspend(MainActivity.kt:136)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(Dispatched.kt:233)
at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:594)
at kotlinx.coroutines.scheduling.CoroutineScheduler.access$runSafely(CoroutineScheduler.kt:60)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:742)
我认为下一帧是取消引用上一帧,而应用程序正在将proxyImage转换为背景线程中的位图。我读到了那些文件,他们说
从该方法返回时,图像引用将被关闭。因此,该方法应完成分析或复制,而不是将图像引用传递到分析方法之外。
我在这里搞不懂,当我们把图像传递到分析方法之外时,复制图像意味着什么。我如何处理这种情况。下面是代码片段。
val imageAnalysisConfig = ImageAnalysisConfig.Builder()
.setTargetResolution(Size(1280, 720))
.build()
val imageAnalysis = ImageAnalysis(imageAnalysisConfig)
imageAnalysis.setAnalyzer { image: ImageProxy, _: Int ->
classifier = Classifier.create(this, Classifier.Model.FLOAT, Classifier.Device.CPU, 1)
CoroutineScope(Default).launch {
convertProxyImageToBitmap(image)
}
}
方法将proxyImage转换为位图。
private fun getImageFromProxy(image: ImageProxy): Bitmap {
val yBuffer = image.planes[0].buffer // Y
val uBuffer = image.planes[1].buffer // U
val vBuffer = image.planes[2].buffer // V
val ySize = yBuffer.remaining()
val uSize = uBuffer.remaining()
val vSize = vBuffer.remaining()
val nv21 = ByteArray(ySize + uSize + vSize)
//U and V are swapped
yBuffer.get(nv21, 0, ySize)
vBuffer.get(nv21, ySize, vSize)
uBuffer.get(nv21, ySize + vSize, uSize)
val yuvImage = YuvImage(nv21, ImageFormat.NV21, image.width, image.height, null)
val out = ByteArrayOutputStream()
yuvImage.compressToJpeg(Rect(0, 0, yuvImage.width, yuvImage.height), 100, out)
val imageBytes = out.toByteArray()
return BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.size)
}
提前谢谢你的帮助。
发布于 2021-01-21 16:26:16
我最近遇到了这个问题。如果我是对的,这意味着在image
已经被调用之后,您正在使用image.close()
做一些事情。在您的示例中,它可能会在setAnalyzer { }
中的lambda块的末尾自动调用,但是当发生这种情况时,您仍然在使用协同器执行一些异步工作。
您需要完全删除协同线,或者将其包装在runBlocking { }
块中等待其完成,因为否则错误不会消失(至少我最近就是这样解决的)。如果您已经将背压策略设置为STRATEGY_KEEP_ONLY_LATEST
,理论上您可以在执行图像分析的代码块中花费您想要的所有时间,因为当前由摄像机生成的帧将被丢弃,而不是不必要地等待。
同时,如果您已经更新了您的项目以使用最新的CameraX版本,并且您正在使用一个专用的类,请记住每次您从覆盖的analyze()
方法中返回时都要自己调用image.close()
,因为否则屏幕上的摄像机预览将永远冻结。
我在这里搞不懂,当我们把图像传递到分析方法之外时,复制图像意味着什么。我如何处理这种情况。
在本例中,我认为您需要对image
进行深度复制,这意味着您创建了一个新实例,并将其所有内容和内部状态设置为原始实例的内容和内部状态,而不仅仅是简单地分配引用。
https://stackoverflow.com/questions/57346611
复制