camera2输出到位图 [英] camera2 output to Bitmap

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

问题描述

我正在尝试将 Google Mobile Vision API 与camera2模块一起使用,并且我很麻烦.

I'm trying to use Google Mobile Vision API with the camera2 module and I'm having a lot of trouble.

我使用Google的 android-Camera2Video 示例代码作为基础.我已经对其进行了修改,以包括以下回调:

I'm using Google's android-Camera2Video example code as a base. I've modified it to include the following callback:

Camera2VideoFragment.java

OnCameraImageAvailable mCameraImageCallback;

public interface OnCameraImageAvailable {
    void onCameraImageAvailable(Image image);
}


ImageReader.OnImageAvailableListener mImageAvailable = new ImageReader.OnImageAvailableListener() {
    @Override
    public void onImageAvailable(ImageReader reader) {

        Image image = reader.acquireLatestImage();
        if (image == null)
            return;

        mCameraImageCallback.onCameraImageAvailable(image);

        image.close();
    }
};

这样,任何包含Camera2VideoFragment.java的片段都可以访问其图像.

That way any fragment including Camera2VideoFragment.java can get access to its images.

现在,条形码API仅接受Bitmap图像,但是我无法将YUV_420_888转换为位图.相反,我将imageReader的文件格式更改为JPEG并运行了以下转换代码:

Now, The Barcode API only accepts Bitmap images, but I'm unable to convert YUV_420_888 to Bitmap. Instead, I changed the imageReader's file format to JPEG and ran the following conversion code:

    Image.Plane[] planes = image.getPlanes();
    ByteBuffer buffer = planes[0].getBuffer();
    buffer.rewind();
    byte[] data = new byte[buffer.capacity()];
    buffer.get(data);
    Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);

这有效,但是将JPEG数据馈送到imageReader的帧速率下降很大.我想知道以前是否有人解决过这个问题.

This worked but the framerate drop of feeding JPEG data to the imageReader was significant. I'm wondering if anyone has worked around this issue before.

推荐答案

答案较晚,但希望对您有所帮助.

A late answer but hopefully still helpful.

Ezequiel Adrian 在他的示例中已经解释了YUV_420_888的转换转换为一种受支持的格式(在他的情况下为NV21),您可以执行类似的操作来获取位图输出:

As Ezequiel Adrian on his Example has explained the conversion of YUV_420_888 into one of the supported formats (In his case NV21), you can do the similar thing to get your Bitmap output:

private byte[] convertYUV420888ToNV21(Image imgYUV420) {
// Converting YUV_420_888 data to YUV_420_SP (NV21).
byte[] data;
ByteBuffer buffer0 = imgYUV420.getPlanes()[0].getBuffer();
ByteBuffer buffer2 = imgYUV420.getPlanes()[2].getBuffer();
int buffer0_size = buffer0.remaining();
int buffer2_size = buffer2.remaining();
data = new byte[buffer0_size + buffer2_size];
buffer0.get(data, 0, buffer0_size);
buffer2.get(data, buffer0_size, buffer2_size);
return data;}

然后您可以将结果转换为位图:

Then you can convert the result into Bitmap:

Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);

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

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