27

私のアプリケーションでは、サーバーから受け取ったビデオ フレームを 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 -- 私は常に画像を作成していると思いますが、ビットマップを一度だけ作成し、受け取ったときに常に新しいバッファを作成/提供する必要があると思います。
あなたの意見を共有してください。

4

4 に答える 4

45

Android-SDK には YuvImage クラスが用意されているため、次のコードで問題が解決します。

あなたはこれを試すことができます、

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);
    }
}
}
于 2012-02-17T14:42:46.490 に答える
0

受け入れられた回答に基づいて、RenderScript組み込み変換メソッドを使用してYUVからRGBへの変換を行う非常に高速な方法を見つけることができました。ここで直接的な例を見つけました: Yuv2RgbRenderScript

RenderScriptHelper クラスの convertYuvToRgbIntrinsic メソッドをコピーして、Hitesh Patel の回答で提供された decodeYUV420SP を置き換えるのと同じくらい簡単です。また、RenderScript オブジェクトを初期化する必要があります (この例は MainActivity クラスにあります)。

また、レンダリング スクリプトの使用をプロジェクト グラドルに追加することを忘れないでください (Android ページでその方法を見つけることができます)。

于 2019-05-15T09:30:37.027 に答える