我尝试从YUV图像创建一个位图,但图像看起来扭曲(有绿色和粉红色叠加)

时间:2018-04-03 12:04:35

标签: android bitmap camera imageview yuv

这是我使用的功能:

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

这是从发送字节数组的Video SDK调用的。 我也试过这个功能:convertYUV420_NV21toRGB8888 来自以下链接:Extract black and white image from android camera's NV21 format 这两次都是我回来的形象:

enter image description here

这里可能出现什么问题?

编辑:

我也试过了renderscript:

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

1 个答案:

答案 0 :(得分:1)

您附加的图片质量非常低,为320x180 I420