【问题标题】:Displaying YUV Image in Android在 Android 中显示 YUV 图像
【发布时间】:2012-02-29 20:50:16
【问题描述】:

在我的应用程序中,我们需要显示从服务器接收到的视频帧到我们的 android 应用程序,
服务器正在以每秒 50 帧的速度发送视频数据,已在 WebM 中编码,即使用 libvpx 对图像进行编码和解码,

现在从 libvpx 解码后,它得到 YUV 数据,我们可以显示在图像布局上,

目前的实现是这样的,

在 JNI / Native C++ 代码中,我们将 YUV 数据转换为 RGB 数据 在Android框架中,调用

public Bitmap createImgae(byte[] bits, int width, int height, int scan) {
    Bitmap bitmap=null;
    System.out.println("video: creating bitmap");
    //try{

            bitmap = Bitmap.createBitmap(width, height,
                    Bitmap.Config.ARGB_8888);
            bitmap.copyPixelsFromBuffer(ByteBuffer.wrap(bits));     

    //}catch(OutOfMemoryError ex){

    //}
            System.out.println("video: bitmap created");
    return bitmap;
}  

要创建位图图像,

使用以下代码在 imageView 上显示图像,

               img = createImgae(imgRaw, imgInfo[0], imgInfo[1], 1);
               if(img!=null && !img.isRecycled()){

                    iv.setImageBitmap(img);
                    //img.recycle();
                    img=null;
                    System.out.println("video: image displayed");
                }

我的查询是,总体而言,此功能大约需要 40 毫秒,有什么办法可以优化它,
1 - 有没有办法将 YUV 数据显示到 imageView ?

2 -- 有没有其他方法可以从 RGB 数据创建图像(位图图像​​),

3 -- 我相信我总是在创建图像,但我想我应该只创建一次位图,并且总是在我们收到时执行/提供新缓冲区。
请分享您的观点。

【问题讨论】:

  • bitmap.copyPixelsFromBuffer(ByteBuffer.wrap(bits)); 是否将 YUV 转换为 RGB?或者你是说这就是你在本地做的事情?
  • 这是一种方法。查看stackoverflow.com/questions/9192982/…

标签: android image-processing imageview yuv


【解决方案1】:

以下代码解决了您的问题,并且由于 Android-SDK 提供了 YuvImage 类,因此可能需要更少的时间来处理 Yuv 格式数据。

你可以试试这个,

ByteArrayOutputStream out = new ByteArrayOutputStream();
YuvImage yuvImage = new YuvImage(data, ImageFormat.NV21, width, height, null);
yuvImage.compressToJpeg(new Rect(0, 0, width, height), 50, out);
byte[] imageBytes = out.toByteArray();
Bitmap image = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);
iv.setImageBitmap(image);

void yourFunction(byte[] data, int mWidth, int mHeight)
{

int[] mIntArray = new int[mWidth*mHeight];

// Decode Yuv data to integer array
decodeYUV420SP(mIntArray, data, mWidth, mHeight);

//Initialize the bitmap, with the replaced color  
Bitmap bmp = Bitmap.createBitmap(mIntArray, mWidth, mHeight, Bitmap.Config.ARGB_8888);  

// Draw the bitmap with the replaced color  
iv.setImageBitmap(bmp);  

}

static public void decodeYUV420SP(int[] rgba, 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);
        // rgba, divide 2^10 ( >> 10)
        rgba[yp] = ((r << 14) & 0xff000000) | ((g << 6) & 0xff0000)
                | ((b >> 2) | 0xff00);
    }
}
}

【讨论】:

  • 我试过你的方法 decodeYUV420SP() 但它创建的图像不好。它充满了黄色和绿色的波浪。我做了这个,它的工作原理:stackoverflow.com/questions/5272388/…
  • 我不明白为什么 rgba[yp]=... 被移动了 8 位。注释掉的行更正确。我得到一个旋转的图像。
  • 有没有办法通过无损压缩(即非 JPEG)做到这一点?
  • YuvImage 根本没用,转成 JPG 再转回 RGB 每一帧播放视频太慢了,不是说 JPG 是有损格式,所以视频看起来会比原来的差。
【解决方案2】:

在onCreate中获取Width和height后创建位图。

editedBitmap = Bitmap.createBitmap(widthPreview, heightPreview,
                android.graphics.Bitmap.Config.ARGB_8888);

onPreviewFrame.

int[] rgbData = decodeGreyscale(aNv21Byte,widthPreview,heightPreview);
editedBitmap.setPixels(rgbData, 0, widthPreview, 0, 0, widthPreview, heightPreview);

private int[] decodeGreyscale(byte[] nv21, int width, int height) {
    int pixelCount = width * height;
    int[] out = new int[pixelCount];
    for (int i = 0; i < pixelCount; ++i) {
        int luminance = nv21[i] & 0xFF;
       // out[i] = Color.argb(0xFF, luminance, luminance, luminance);
        out[i] = 0xff000000 | luminance <<16 | luminance <<8 | luminance;//No need to create Color object for each.
    }
    return out;
}

还有奖金。

if(cameraId==CameraInfo.CAMERA_FACING_FRONT)
{   
    matrix.setRotate(270F);
}

finalBitmap = Bitmap.createBitmap(editedBitmap, 0, 0, widthPreview, heightPreview, matrix, true);

【讨论】:

  • 其实这回答了一个不同的question
【解决方案3】:

另一种方法是使用ScriptIntrinsicYuvToRGB,这比每次 JPEG 编码(和解码)效率更高

fun yuvByteArrayToBitmap(bytes: ByteArray, width: Int, height: Int): Bitmap {
    val rs = RenderScript.create(this)

    val yuvToRgbIntrinsic = ScriptIntrinsicYuvToRGB.create(rs, Element.U8_4(rs));
    val yuvType = Type.Builder(rs, Element.U8(rs)).setX(bytes.size);
    val input = Allocation.createTyped(rs, yuvType.create(), Allocation.USAGE_SCRIPT);

    val rgbaType = Type.Builder(rs, Element.RGBA_8888(rs)).setX(width).setY(height);
    val output = Allocation.createTyped(rs, rgbaType.create(), Allocation.USAGE_SCRIPT);

    input.copyFrom(bytes);

    yuvToRgbIntrinsic.setInput(input);
    yuvToRgbIntrinsic.forEach(output);

    val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
    output.copyTo(bitmap)

    input.destroy()
    output.destroy()
    yuvToRgbIntrinsic.destroy()
    rs.destroy()

    return bitmap
}

【讨论】:

    【解决方案4】:

    根据接受的答案,我可以找到一种使用 RenderScript 内在转换方法将 YUV 转换为 RGB 的更快方法。我在这里找到了直接的例子:Yuv2RgbRenderScript

    它可以像复制 RenderScriptHelper 类中的 convertYuvToRgbIntrinsic 方法一样简单,以替换 Hitesh Patel 在他的回答中给出的 decodeYUV420SP。此外,您还需要初始化一个 RenderScript 对象(示例在 MainActivity 类中)。

    并且不要忘记在项目中添加使用渲染脚本的gradle(在android页面中你可以找到这样做的方法)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-08
      • 2017-01-06
      • 2016-08-24
      • 1970-01-01
      • 2012-05-28
      • 1970-01-01
      相关资源
      最近更新 更多