将位图数组转换为YUV(YCbCr NV21)

Abh*_*hak 27 android image-processing yuv android-image android-camera

如何将返回的Bitmap转换BitmapFactory.decodeFile()为YUV格式(simillar到相机的onPreviewFrame()返回字节数组)?

Fra*_*oid 48

这是一些实际工作的代码:

    // untested function
    byte [] getNV21(int inputWidth, int inputHeight, Bitmap scaled) {

        int [] argb = new int[inputWidth * inputHeight];

        scaled.getPixels(argb, 0, inputWidth, 0, 0, inputWidth, inputHeight);

        byte [] yuv = new byte[inputWidth*inputHeight*3/2];
        encodeYUV420SP(yuv, argb, inputWidth, inputHeight);

        scaled.recycle();

        return yuv;
    }

    void encodeYUV420SP(byte[] yuv420sp, int[] argb, int width, int height) {
        final int frameSize = width * height;

        int yIndex = 0;
        int uvIndex = frameSize;

        int a, R, G, B, Y, U, V;
        int index = 0;
        for (int j = 0; j < height; j++) {
            for (int i = 0; i < width; i++) {

                a = (argb[index] & 0xff000000) >> 24; // a is not used obviously
                R = (argb[index] & 0xff0000) >> 16;
                G = (argb[index] & 0xff00) >> 8;
                B = (argb[index] & 0xff) >> 0;

                // well known RGB to YUV algorithm
                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;

                // NV21 has a plane of Y and interleaved planes of VU each sampled by a factor of 2
                //    meaning for every 4 Y pixels there are 1 V and 1 U.  Note the sampling is every other
                //    pixel AND every other scanline.
                yuv420sp[yIndex++] = (byte) ((Y < 0) ? 0 : ((Y > 255) ? 255 : Y));
                if (j % 2 == 0 && index % 2 == 0) { 
                    yuv420sp[uvIndex++] = (byte)((V<0) ? 0 : ((V > 255) ? 255 : V));
                    yuv420sp[uvIndex++] = (byte)((U<0) ? 0 : ((U > 255) ? 255 : U));
                }

                index ++;
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

  • 如果有人需要YV12而不是NV21,可以稍微修改这个答案,以产生每个人最喜欢的三平面格式:https://gist.github.com/wobbals/5725412 (6认同)
  • encodeYUV420SP抛出ArrayIndexOutOfRange当inputWidth或inputHeight是奇数时的异常.将yuv数组声明为`byte [] yuv = new byte [inputHeight*inputWidth + 2*(int)Math.ceil(inputHeight/2.0)*(int)Math.ceil(inputWidth/2.0)];`解决了问题. (5认同)
  • 根据yuv420sp规范,U值在缓冲区的最后3个中存储在V之前,因此我必须交换if-case中的行以防止蓝色/红色被翻转. (4认同)
  • 我在来自`BitmapFactory.decodeResource()`的位图上使用这个方法时遇到了问题,但是当我从`BitmapFactory.decodeStream(getAssets().open("myasset.bmp"))`加载位图时它工作正常。 (2认同)
  • 永远不要使用 Java 进行此类对话,请使用 RenderScript 或 Lubyuv 库。Java 清晰的对话很慢 (2认同)

blu*_*con -1

bmp 文件将为 RGB888 格式,因此您需要将其转换为 YUV。我在 Android 中还没有遇到过任何可以为您执行此操作的 api。

但您可以自己执行此操作,请参阅此链接以了解如何..