我正在尝试将从cameraX分析器中的ProxyImage转换为位图,以使用Tensor Flow Lite分析图像。
因此,我实现了cameraX Analyze回调,该回调提供了proxyImage作为图像。我需要将该proxyImage转换为位图。
如果我在UI线程上进行此对话,它会使相机预览延迟。因此,我想使用协程来完成它。
现在的问题是,每当我将proxyImage传递给协程在后台线程上将其转换为位图时,它会崩溃,并显示“Image is already closed.”的非法状态异常。
我认为下一帧正在将代理图像转换为位图时,引用上一个帧。 我阅读了文档,他们在那里说:
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)
我认为下一帧正在将代理图像转换为位图时,引用上一个帧。 我阅读了文档,他们在那里说:
我感到困惑的是,当我们将图像传递到分析方法之外时,它是什么意思。 我该如何处理这种情况。 以下是代码片段。从此方法返回后,图像引用将被关闭。 因此,该方法应完成分析或制作副本,而不是将图像引用传递到分析方法之外。
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)转换为位图(Bitmap)的方法。
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)
}
感谢提前帮助。