我正在尝试使用下面的函数将cameraX的take photo方法捕获的图像转换为位图
fun imageProxyToBitmap(image: ImageProxy): Bitmap {
val buffer: ByteBuffer = image.planes[0].buffer // :- This line is where error was occurring
buffer.rewind()
val bytes = ByteArray(buffer.capacity())
buffer.get(bytes)
return BitmapFactory.decodeByteArray(bytes, 0, bytes.size)
}
但是它给出了一个错误
java.lang.IllegalStateException: Image is already closed
我使用的是最新版本的cameraX库"1.0.0-beta11"
如何解决此问题?
发布于 2020-11-18 23:39:28
这个运行得很好。
override fun onCaptureSuccess(image: ImageProxy) {
var capturedImageBitmap = AppUtils().imageProxyToBitmap(image)
super.onCaptureSuccess(image)
修复方法是,必须在转换后调用super.onCaptureSuccess(image)
。
原因可能是这样的
/**
* Returns the android {@link Image}.
*
* <p>If the ImageProxy is a wrapper for an android {@link Image}, it will return the
* {@link Image}. It is possible for an ImageProxy to wrap something that isn't an
* {@link Image}. If that's the case then it will return null.
*
* <p>The returned image should not be closed by the application. Instead it should be closed by
* the ImageProxy, which happens, for example, on return from the {@link ImageAnalysis.Analyzer}
* function. Destroying the {@link ImageAnalysis} will close the underlying
* {@link android.media.ImageReader}. So an {@link Image} obtained with this method will behave
* as such.
*
* @return the android image.
* @see android.media.Image#close()
*/
https://stackoverflow.com/questions/64895456
复制相似问题