Sceneform-android-sdk: [Solved] How can I convert frame to jpg file ?

Created on 9 Jan 2020  路  2Comments  路  Source: google-ar/sceneform-android-sdk

I can get frame image by calling frame.acquireCameraImage(), and then it returns a YUV_420_888 format image to me, but I cannot find any solutions to get jpg file. Please help!

Most helpful comment

public static byte[] NV21toJPEG(byte[] nv21, int width, int height) {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        YuvImage yuv = new YuvImage(nv21, ImageFormat.NV21, width, height, null);
        yuv.compressToJpeg(new Rect(0, 0, width, height), 100, out);
        return out.toByteArray();
    }
    public static byte[] YUV_420_888toNV21(Image image) {
        byte[] nv21;
        ByteBuffer yBuffer = image.getPlanes()[0].getBuffer();
        ByteBuffer uBuffer = image.getPlanes()[1].getBuffer();
        ByteBuffer vBuffer = image.getPlanes()[2].getBuffer();

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

        nv21 = new byte[ySize + uSize + vSize];

        //U and V are swapped
        yBuffer.get(nv21, 0, ySize);
        vBuffer.get(nv21, ySize, vSize);
        uBuffer.get(nv21, ySize + vSize, uSize);

        return nv21;
    }

All 2 comments

public static byte[] NV21toJPEG(byte[] nv21, int width, int height) {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        YuvImage yuv = new YuvImage(nv21, ImageFormat.NV21, width, height, null);
        yuv.compressToJpeg(new Rect(0, 0, width, height), 100, out);
        return out.toByteArray();
    }
    public static byte[] YUV_420_888toNV21(Image image) {
        byte[] nv21;
        ByteBuffer yBuffer = image.getPlanes()[0].getBuffer();
        ByteBuffer uBuffer = image.getPlanes()[1].getBuffer();
        ByteBuffer vBuffer = image.getPlanes()[2].getBuffer();

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

        nv21 = new byte[ySize + uSize + vSize];

        //U and V are swapped
        yBuffer.get(nv21, 0, ySize);
        vBuffer.get(nv21, ySize, vSize);
        uBuffer.get(nv21, ySize + vSize, uSize);

        return nv21;
    }

@DoctorB Thanks! That is what I'm looking for.

To add that, using code below can rotate the image to right angle.

            // rotate image
            Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
            final int rotate = 90;

            Matrix matrix = new Matrix();
            matrix.postRotate(rotate);

            return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
                    bitmap.getHeight(), matrix, true);
Was this page helpful?
0 / 5 - 0 ratings

Related issues

Brian-Kwon picture Brian-Kwon  路  3Comments

khonakr picture khonakr  路  3Comments

peronecode picture peronecode  路  3Comments

yashvv picture yashvv  路  3Comments

StevenOttoG picture StevenOttoG  路  4Comments