首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将cameraX转换为位图时,imageProxy图像已关闭

将cameraX转换为位图时,imageProxy图像已关闭
EN

Stack Overflow用户
提问于 2020-11-18 22:50:36
回答 1查看 302关注 0票数 0

我正在尝试使用下面的函数将cameraX的take photo方法捕获的图像转换为位图

代码语言:javascript
运行
复制
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)
}

但是它给出了一个错误

代码语言:javascript
运行
复制
java.lang.IllegalStateException: Image is already closed

我使用的是最新版本的cameraX库"1.0.0-beta11"

如何解决此问题?

EN

回答 1

Stack Overflow用户

发布于 2020-11-18 23:39:28

这个运行得很好。

代码语言:javascript
运行
复制
override fun onCaptureSuccess(image: ImageProxy) {
        var capturedImageBitmap = AppUtils().imageProxyToBitmap(image)
 super.onCaptureSuccess(image)

修复方法是,必须在转换后调用super.onCaptureSuccess(image)

原因可能是这样的

代码语言:javascript
运行
复制
/**
     * 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()
     */
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64895456

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档