0

I am making an app for exposure fusion but I have a small hiccup on my ZTE test device. On the Pixel emulator, I am able to take an Image from the ImageReader and convert it to a Mat and then back to a Bitmap to be displayed in an ImageView. This is the code:

        int width = image.getWidth();
        int height = image.getHeight();

        Image.Plane yPlane = image.getPlanes()[0];
        Image.Plane uPlane = image.getPlanes()[1];
        Image.Plane vPlane = image.getPlanes()[2];

        ByteBuffer yBuffer = yPlane.getBuffer();
        ByteBuffer uBuffer = uPlane.getBuffer();
        ByteBuffer vBuffer = vPlane.getBuffer();

        int ySize = yBuffer.remaining();
        int uSize = uBuffer.remaining();
        int vSize = vBuffer.remaining();

        int uvPixelStride = uPlane.getPixelStride();

        Mat yuvMat = new Mat(height + (height / 2), width, CvType.CV_8UC1);

        byte[] data = new byte[ySize + uSize + vSize];
        yBuffer.get(data, 0, ySize);
        uBuffer.get(data, ySize, uSize);
        vBuffer.get(data, ySize + uSize, vSize);

        if (uvPixelStride == 1) {
            yuvMat.put(0, 0, data);

            Mat rgb = new Mat(image.getHeight(), image.getWidth(), CvType.CV_8UC4);

            Imgproc.cvtColor(yuvMat, rgb, Imgproc.COLOR_YUV420p2RGBA);

            return rgb;

Now, for the ZTE, the pixel stride for the U and V planes are 2 but I can't seem to get it to display correctly.

Jacked up image from ZTE

This is the code I'm using right now:

Mat yuv = new Mat(image.getHeight(), image.getWidth(), CvType.CV_8UC1);
            yuv.put(0, 0, data);

            Mat rgb = new Mat(image.getHeight(), image.getWidth(), CvType.CV_8UC4);
            Imgproc.cvtColor(yuv, rgb, Imgproc.COLOR_YUV2RGBA_NV21);

            return rgb;

Any help would be greatly appreciated.

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
Brian Burgess
  • 274
  • 3
  • 6
  • if you a bgr(a) image from disk or from cam, convert it to yuv and convert it back to bgr(a), does it work? – Micka Dec 11 '17 at 23:27
  • Haven't tried it yet honestly... I was able to convert it to a Bitmap in a very time consuming process but it took about 15 - 20s so I abandoned that attempt. – Brian Burgess Dec 11 '17 at 23:36
  • For anyone who runs across this... I found the solution in [another question](https://stackoverflow.com/questions/30510928/convert-android-camera2-api-yuv-420-888-to-rgb?rq=1). – Brian Burgess Dec 12 '17 at 12:43

0 Answers0