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

所以这篇文章的真正题目叫“从Bitmap中获取RGB数据的两种方式”

Android按像素获取图片rgb值_sed

,下面我们以从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的步骤

Android按像素获取图片rgb值_宽高_02

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

Android按像素获取图片rgb值_宽高_03

为什么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格式~

Android按像素获取图片rgb值_sed_04