rosu alin
rosu alin

Reputation: 5830

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

This is the function that I use:

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

And this is being called from a Video SDK which sends the byte array. I have also tried this function: convertYUV420_NV21toRGB8888 from the following link: Extract black and white image from android camera's NV21 format And both times this is the image that I get back:

enter image description here

What could go wrong here?

EDIT:

I also tried with 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());
    }

But the same. How can I know that the bite array I get from the camera is valid?

I also included a file here, that shows the byte array I receive: https://www.dropbox.com/s/fbubwpx06ypr61e/byte%20array.txt?dl=0

Upvotes: 2

Views: 992

Answers (1)

Alex Cohn
Alex Cohn

Reputation: 57173

The image you attached is very low quality 320x180 I420.

Upvotes: 1

Related Questions