我尝试从 YUV 图像创建位图,但图像看起来失真(有绿色和粉红色叠加层)

I try to create a bitmap from a YUV Image, but the image looks distorted (has green and pink overlay)

这是我使用的函数:

public void vidyoConferenceFrameReceivedCallback(final int participantId, final int width, final int height, final byte[] rawImageBytes) {
    if (selfView == null || selfView.getVisibility() == View.GONE)
        return;
    try {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        YuvImage yuvImage = new YuvImage(rawImageBytes, ImageFormat.NV21, width, height, null);
        yuvImage.compressToJpeg(new Rect(0, 0, width / 2, height / 2), 50, out);
        byte[] imageBytes = out.toByteArray();
        final Bitmap image = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);
        new Handler(Looper.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                selfView.setImageBitmap(image);
                System.gc();
            }
        });
    } catch (Exception e) {
        Logger.info("Error on vidyoConferenceFrameReceivedCallback: " + e.getMessage());
    }
}

这是从发送字节数组的视频 SDK 调用的。 我也试过这个功能:convertYUV420_NV21toRGB8888 来自以下 link:Extract black and white image from android camera's NV21 format 这两次都是我得到的图像:

这里会出什么问题?

编辑:

我也试过渲染脚本:

try {
        byte[] outBytes = new byte[W * H * 4];
        rs = RenderScript.create(getActivity());
        yuvToRgbIntrinsic = ScriptIntrinsicYuvToRGB.create(rs, Element.RGBA_8888(rs));


        Type.Builder yuvType = new Type.Builder(rs, Element.U8(rs))
                .setX(W).setY(H)
                .setYuvFormat(android.graphics.ImageFormat.NV21);
        Allocation in = Allocation.createTyped(rs, yuvType.create(), Allocation.USAGE_SCRIPT);


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

        in.copyFrom(rawImageBytes);

        yuvToRgbIntrinsic.setInput(in);
        yuvToRgbIntrinsic.forEach(out);

        out.copyTo(outBytes);

        final Bitmap bmpout = Bitmap.createBitmap(W, H, Bitmap.Config.ARGB_8888);
        out.copyTo(bmpout);

        new Handler(Looper.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                remoteView.setImageBitmap(bmpout);
                System.gc();
            }
        });
    } catch (Exception e) {
        Logger.info("Error on vidyoConferenceFrameReceivedRemoteCallback: " + e.getMessage());
    }

但是一样。我怎么知道我从相机得到的咬合数组是有效的?

我还在这里包含了一个文件,它显示了我收到的字节数组: https://www.dropbox.com/s/fbubwpx06ypr61e/byte%20array.txt?dl=0

您附加的图像 320x180 质量很差 I420