从Bitmap中获取YUV数据的两种方式

从Bitmap中我们能获取到的是RGB颜色分量,当需要获取YUV数据的时候,则需要先提取R,G,B分量的值,然后将RGB转化为YUV(根据具体的YUV的排列格式做相应的Y,U,V分量的排列)

所以这篇文章的真正题目叫“从Bitmap中获取RGB数据的两种方式”,下面我们以从Bitmap中获取NV21数据为例进行说明

从Bitmap中获取RGB数据,Android SDK提供了两种方式供我们使用

第一种是getPixels接口:

public void getPixels(@ColorInt int[] pixels, 
                                int offset, 
                                int stride,
                                int x, 
                                int y, 
                                int width, 
                                int height)

Bitmap中的像素数据将copy到pixels数组中,数组中每一个pixel都是按ARGB四个分量8位排列压缩而成的一个int值

第二种是copyPixelsToBuffer接口:

public void copyPixelsToBuffer(Buffer dst)

Bitmap中的像素数据将copy到buffer中,buffer中每一个pixel都是按RGBA四个分量的顺序进行排列的

两种接口返回的颜色通道顺序不同,在取值的时候需要特别注意

拿到R,G,B分量的值后,就可以转化为Y,U,V分量了,转化算法:

y = ((66 * r + 129 * g + 25 * b + 128) >> 8) + 16;
u = ((-38 * r - 74 * g + 112 * b + 128) >> 8) + 128;
v = ((112 * r - 94 * g -18 * b + 128) >> 8) + 128;

使用getPixels接口从Bitmap中获取NV21数据的完整代码

 public static byte[] fetchNV21(@NonNull Bitmap bitmap) {
        int w = bitmap.getWidth();
        int h = bitmap.getHeight();
        int size = w * h;
        int[] pixels = new int[size];
        bitmap.getPixels(pixels, 0, w, 0, 0, w, h);


        byte[] nv21 = new byte[size * 3 / 2];
        
        // Make w and h are all even.
        w &= ~1;
        h &= ~1;


        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                int yIndex = i * w + j;
                
                int argb = pixels[yIndex];
                int a = (argb >> 24) & 0xff;  // unused
                int r = (argb >> 16) & 0xff;
                int g = (argb >> 8) & 0xff;
                int b = argb & 0xff;


                int y = ((66 * r + 129 * g + 25 * b + 128) >> 8) + 16;
                y = clamp(y, 16, 255);
                nv21[yIndex] = (byte)y;
                
                if (i % 2 == 0 && j % 2 == 0) {
                    int u = ((-38 * r - 74 * g + 112 * b + 128) >> 8) + 128;
                    int v = ((112 * r - 94 * g -18 * b + 128) >> 8) + 128;


                    u = clamp(u, 0, 255);
                    v = clamp(v, 0, 255);


                    nv21[size + i / 2 * w + j] = (byte) v;
                    nv21[size + i / 2 * w + j + 1] = (byte) u;
                }
            }
        }
        return nv21;
    }

拿到nv21数据后,我们怎么验证数据是正常的呢?

可以通过YuvImage接口转成jpeg,然后再将jpeg转化为Bitmap,使用ImageView显示出来看下是否和原图一致就可以验证了

// create test bitmap and fetch nv21 data
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.header);
int w = bitmap.getWidth();
int h = bitmap.getHeight();
byte[] nv21 = Util.fetchNV21(bitmap);
bitmap.recycle();


// nv21 -> jpeg -> bitmap
YuvImage yuvImage = new YuvImage(nv21, ImageFormat.NV21, w, h, null);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
yuvImage.compressToJpeg(new Rect(0, 0, w, h), 100, outputStream);
byte[] array = outputStream.toByteArray();
Bitmap tmp = BitmapFactory.decodeByteArray(array, 0, array.length);


// show
imageView.setImageBitmap(tmp);

在YuvImage的compressToJpeg接口的源码中,有个调整压缩rect的步骤

进入到adjustRectangle方法,可以发现压缩区域的宽高被调整为偶数了

为什么w,h必须要保证为偶数呢?这个是因为当w,h都不为偶数的时候,在计算到最后的V,U的索引时候算出来会和NV21的数组长度一致,这样就会导致ArrayIndexOutOfBoundsException了

使用copyPixelsToBuffer接口从Bitmap中获取NV21数据的完整代码

    public static byte[] fetchNV21(@NonNull Bitmap bitmap) {
        ByteBuffer byteBuffer = ByteBuffer
                .allocateDirect(bitmap.getByteCount())
                .order(ByteOrder.nativeOrder());
        bitmap.copyPixelsToBuffer(byteBuffer);
        byte[] array = byteBuffer.array();


        int w = bitmap.getWidth();
        int h = bitmap.getHeight();
        int area = w * h;
        int count = array.length / 4;
        if (count > area) {
            count = area;
        }
    
        int nv21Size = area * 3 / 2;
        byte[] nv21 = new byte[nv21Size];
        for (int i = 0; i < count; i++) {
            int row = i / w;
            int col = i - col * w;
            int vIndex = area + (row >> 1) * w + (col & ~1);
            int uIndex = area + (row >> 1) * w + (col & ~1) + 1;


            // case: w or h not even
            if (vIndex >= nv21Size) {
                break;
            }


            // RGBA 
            int r = ((int)array[i * 4]) & 0xff;
            int g = ((int)array[i * 4 + 1]) & 0xff;
            int b = ((int)array[i * 4 + 2]) & 0xff;
            int a = ((int)array[i * 4 + 3]) & 0xff; // unused


            int y = ((66 * r + 129 * g + 25 * b + 128) >> 8) + 16;
            int u = ((-38 * r - 74 * g + 112 * b + 128) >> 8) + 128;
            int v = ((112 * r - 94 * g -18 * b + 128) >> 8) + 128;


            y = clamp(y, 16, 255);
            u = clamp(u, 0, 255);
            v = clamp(v, 0, 255);


            nv21[i] = (byte)y;
            nv21[vIndex] = (byte)v;
            nv21[uIndex] = (byte)u;
        }


        return nv21;
    }

通过buffer拷贝的数据,有时候是会多那么一两个pixel。比如我测试的一张图片,Bitmap宽高为1200,获取到的byte数组长度为5760007,就多了7个字节,2个像素

fetchBitmapToNv21: w = 1200, h = 1200, array.length = 5760007, w * h = 1440000

从Bitmap中拿到RGB数据,再转化为YUV数据后,根据Y,U,V分量排列的不同可以任意组合为自己所需要的YUV格式~

推荐阅读:

音视频面试基础题

OpenGL 之 GPUImage 源码分析

OpenGL ES 实现实时音频的可视化

Shader 优化 | OpenGL 绘制网格效果

觉得不错,点个在看呗~

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的Android Camera2的ImageReader回调函数示例代码,用于将Image的YUV数据换为RGB格式的Bitmap并保存到本地。在这个示例,我们将使用JavaCV库来进行YUVRGB的操作,并使用Bitmap类来保存图像。 ```java private ImageReader.OnImageAvailableListener mOnImageAvailableListener = new ImageReader.OnImageAvailableListener() { @Override public void onImageAvailable(ImageReader reader) { Image image = reader.acquireNextImage(); if (image == null) { return; } // 获取图像的宽度和高度 int width = image.getWidth(); int height = image.getHeight(); // 将YUV数据换为RGB格式的Bitmap Mat yuvMat = new Mat(height + height / 2, width, CvType.CV_8UC1); ByteBuffer buffer = image.getPlanes()[0].getBuffer(); byte[] data = new byte[buffer.remaining()]; buffer.get(data); yuvMat.put(0, 0, data); Mat rgbMat = new Mat(height, width, CvType.CV_8UC3); Imgproc.cvtColor(yuvMat, rgbMat, Imgproc.COLOR_YUV2RGB_NV21); Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); Utils.matToBitmap(rgbMat, bitmap); // 保存Bitmap到本地 String fileName = "image_" + System.currentTimeMillis() + ".jpg"; String filePath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + File.separator + fileName; try { FileOutputStream outputStream = new FileOutputStream(filePath); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream); outputStream.close(); } catch (Exception e) { e.printStackTrace(); } image.close(); } }; ``` 请注意,这只是一个简单的示例代码,可能需要根据你的实际需求进行修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值