将ImageProxy转换为位图 [英] Converting ImageProxy to Bitmap

查看:140
本文介绍了将ImageProxy转换为位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我想探索新的Google相机API- CameraX
我想做的是每秒从相机提要中获取图像,然后将其传递到接受位图的功能中,以进行机器学习。

So, I wanted to explore new Google's Camera API - CameraX. What I want to do, is take an image from camera feed every second and then pass it into a function that accepts bitmap for machine learning purposes.

我阅读了 Camera X Image Analyzer上的文档:

I read the documentation on Camera X Image Analyzer:


图像分析用例为您的应用提供了CPU可访问的
图像,以执行图像处理,计算机视觉或机器
的学习推理。该应用程序实现了在每个帧上运行的分析器方法

The image analysis use case provides your app with a CPU-accessible image to perform image processing, computer vision, or machine learning inference on. The application implements an Analyzer method that is run on each frame.

..这基本上就是我所需要的。因此,我像这样实现了此图像分析器:

..which basically is what I need. So, I implemented this image analyzer like this:

imageAnalysis.setAnalyzer { image: ImageProxy, _: Int ->
    viewModel.onAnalyzeImage(image)
}

我得到的是图片:ImageProxy 。如何将这个 ImageProxy 传输到 Bitmap

What I get is image: ImageProxy. How can I transfer this ImageProxy to Bitmap?

我试图这样解决它:

fun decodeBitmap(image: ImageProxy): Bitmap? {
    val buffer = image.planes[0].buffer
    val bytes = ByteArray(buffer.capacity()).also { buffer.get(it) }
    return BitmapFactory.decodeByteArray(bytes, 0, bytes.size)
}

但返回 null -因为 decodeByteArray 没有收到有效的(?)位图字节。有想法吗?

But it returns null - because decodeByteArray does not receive valid (?) bitmap bytes. Any ideas?

推荐答案

您需要检查 image.format 查看是否为 ImageFormat.YUV_420_888 。如果是这样,则可以使用此扩展程序将图像转换为位图:

You will need to check the image.format to see if it is ImageFormat.YUV_420_888. If so , then you can you use this extension to convert image to bitmap:

fun Image.toBitmap(): Bitmap {
    val yBuffer = planes[0].buffer // Y
    val uBuffer = planes[1].buffer // U
    val vBuffer = 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, this.width, this.height, null)
    val out = ByteArrayOutputStream()
    yuvImage.compressToJpeg(Rect(0, 0, yuvImage.width, yuvImage.height), 50, out)
    val imageBytes = out.toByteArray()
    return BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.size)
}

这对我有用。

这篇关于将ImageProxy转换为位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆