android - 将NV21字节数组转换为位图可读格式

标签 android bitmap android-camera yuv

<分区>

嘿,我正在创建小型相机应用程序,我已经实现了所有功能,但我有一个问题,即将 NV21 字节数组转换为 jpeg 格式
我找到了很多方法,但所有方法甚至都无法工作或无法在某些设备上工作
首先我尝试了这个 fragment ,它在 Xperia z2 5.2 上工作,但在 galaxy s4 4.4.4 上工作

bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);

同样这种方式在同一台设备上工作而在另一台设备上失败

 int pich = camera.getParameters().getPreviewSize().height;
        int picw = camera.getParameters().getPreviewSize().width;
        int[] pix = new int[picw * pich];
        bitmap.getPixels(pix, 0, picw, 0, 0, picw, pich);
                    //  int R, G, B, Y;
                    for (int y = 0; y < pich; y++) {
                        for (int x = 0; x < picw; x++) {
                            int index = y * picw + x;
                          int R = (pix[index] >> 16) & 0xff; 
                         int G = (pix[index] >> 8) & 0xff;     
                    int B = pix[index] & 0xff;
                   pix[index] = 0xff000000 | (R << 16) | (G << 8) | B;
                        }
                                  }

其次 我尝试了很多解决方案来转换解码 NV21 第一个renderscript代码

    public Bitmap convertYUV420_NV21toRGB8888_RenderScript(byte [] data,int W, int H, Fragment fragment) {
    // http://stackoverflow.com/questions/20358803/how-to-use-scriptintrinsicyuvtorgb-converting-byte-yuv-to-byte-rgba
    RenderScript rs;
    ScriptIntrinsicYuvToRGB yuvToRgbIntrinsic;
    rs = RenderScript.create(fragment.getActivity());
    yuvToRgbIntrinsic = ScriptIntrinsicYuvToRGB.create(rs, Element.U8_4(rs)); //Create an intrinsic for converting YUV to RGB.

    Type.Builder yuvType = new Type.Builder(rs, Element.U8(rs)).setX(data.length);
    Allocation in = Allocation.createTyped(rs, yuvType.create(), Allocation.USAGE_SCRIPT); //an Allocation will be populated with empty data when it is first created

    Type.Builder rgbaType = new Type.Builder(rs, Element.RGBA_8888(rs)).setX(W).setY(H);
    Allocation out = Allocation.createTyped(rs, rgbaType.create(), Allocation.USAGE_SCRIPT); //an Allocation will be populated with empty data when it is first created

    in.copyFrom(data);//Populate Allocations with data.

    yuvToRgbIntrinsic.setInput(in); //Set the input yuv allocation, must be U8(RenderScript).
    yuvToRgbIntrinsic.forEach(out); //Launch the appropriate kernels,Convert the image to RGB.


    Bitmap bmpout = Bitmap.createBitmap(W, H, Bitmap.Config.ARGB_8888);
    out.copyTo(bmpout); //Copy data out of Allocation objects.
    return bmpout;

}

还有这段代码

 void decodeYUV420SP(int[] rgb, byte[] yuv420sp, int width, int height) {

    final int frameSize = width * height;

    for (int j = 0, yp = 0; j < height; j++) {
        int uvp = frameSize + (j >> 1) * width, u = 0, v = 0;

        for (int i = 0; i < width; i++, yp++) {

            int y = (0xff & ((int) yuv420sp[yp])) - 16;

            if (y < 0)

                y = 0;

            if ((i & 1) == 0) {

                v = (0xff & yuv420sp[uvp++]) - 128;

                u = (0xff & yuv420sp[uvp++]) - 128;

            }

            int y1192 = 1192 * y;

            int r = (y1192 + 1634 * v);
            int g = (y1192 - 833 * v - 400 * u);
            int b = (y1192 + 2066 * u);

            if (r < 0)
                r = 0;
            else if (r > 262143)

                r = 262143;
            if (g < 0)
                g = 0;
            else if (g > 262143)

                g = 262143;

            if (b < 0)

                b = 0;

            else if (b > 262143)

                b = 262143;

            rgb[yp] = 0xff000000 | ((r << 6) & 0xff0000) | ((g >> 2) & 0xff00) | ((b >> 10) & 0xff);
        }

    }
}

最后我尝试将图像保存到 sd 卡上,然后重新打开它 但它也失败了

File pictureFile = new File(filename);
            int pich = camera.getParameters().getPreviewSize().height;
            int picw = camera.getParameters().getPreviewSize().width;
            Rect rect = new Rect(0, 0,picw, pich);
            YuvImage img = new YuvImage(data, ImageFormat.NV21, picw, picw, null);

            try {
                FileOutputStream fos = new FileOutputStream(pictureFile);
                img.compressToJpeg(rect, 100, fos);
                fos.write(data);
                fos.close();

这是我遵循的最后 3 种方法的结果 enter image description here

最佳答案

有几种方法可以保存来自相机的 NV21 帧,最简单的方法是将其转换为 YuvImage,然后将其保存为 Jpeg 文件:

FileOutputStream fos = new FileOutputStream(Environment.getExternalStorageDirectory() + "/imagename.jpg");
YuvImage yuvImage = new YuvImage(nv21bytearray, ImageFormat.NV21, width, height, null);
yuvImage.compressToJpeg(new Rect(0, 0, width, height), 100, fos);
fos.close();

或者,您也可以将其转换为 Android Bitmap 对象并将其保存为 PNG 或其他格式:

YuvImage yuvImage = new YuvImage(nv21bytearray, ImageFormat.NV21, width, height, null);
ByteArrayOutputStream os = new ByteArrayOutputStream();
yuvImage.compressToJpeg(new Rect(0, 0, width, height), 100, os);
byte[] jpegByteArray = os.toByteArray();
Bitmap bitmap = BitmapFactory.decodeByteArray(jpegByteArray, 0, jpegByteArray.length);
FileOutputStream fos = new FileOutputStream(Environment.getExternalStorageDirectory() + "/imagename.png");
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.close();

请注意,最后一个选项执行 NV21 -> JPEG -> Bitmap Object -> PNG file 过程,因此请记住,这不是一种非常有效的方式来保存来自相机的预览图像,如果您需要高性能。

更新:我厌倦了这种转换需要多长时间才能工作,所以我围绕 RenderScript 编写了一个库 ( easyRS ),以便在一行代码中轻松完成此操作:

Bitmap outputBitmap = Nv21Image.nv21ToBitmap(rs, nv21ByteArray, width, height);

在 Moto G 2nd 上处理 2000x2000 图像的 JPEG 处理速度大约快五倍。

关于android - 将NV21字节数组转换为位图可读格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32276522/

相关文章:

java - 需要帮助调试我的 android-java 代码

android - 使用 PhoneNumberFormattingTextWatcher 无需输入国家代码

c# - 在 Unity 中读取二维码

android - 相机没有反应

android - 使用相机 Intent 拍照以纵向模式android旋转图片

Android 相机 android.hardware.Camera 已弃用

android - 从 assets 文件夹加载大于 1M 的文件

android - 服务停止时 Widget 的 PendingIntent 丢失

android - 使用异步任务显示外部图像

android - GLUtils.texImage2D 和纹理中的 Alpha 问题