Displaying YUV Image in Android

前端 未结 4 1626
不知归路
不知归路 2020-11-29 01:07

In my application, we need to display the Video frame receives from server to our android application,
Server is sending video data @ 50 frame per second, having encoded

4条回答
  •  不知归路
    2020-11-29 01:33

    Create a bitmap after getting Width and height in onCreate.

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

    And in onPreviewFrame.

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

    And

    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;
    }
    

    And Bonus.

    if(cameraId==CameraInfo.CAMERA_FACING_FRONT)
    {   
        matrix.setRotate(270F);
    }
    
    finalBitmap = Bitmap.createBitmap(editedBitmap, 0, 0, widthPreview, heightPreview, matrix, true);
    

提交回复
热议问题