[FIXED] What is the fastest way to encode a bytebuffer image in jpg format?

Issue

I use CameraX to capture an image from the camera using ImageAnalysis. The image is received as 3 ByteBuffers (respectively for the Y, U and V planes). I only keep the Y plane ByteBuffer. The other 2 are discarded.

I would like to reduce the size of this ByteBuffer, for example by encoding it in the jpg format.

Is it better to use MediaCodec or BitmapFactory? Knowing that the encoding time should be as small as possible. Is there another (better or faster) solution?

Solution

A somewhat efficient way to do that is first converting your YUV_420_888 image to NV21, then use Android’s YuvImage#compressToJpeg API to convert it.

For the YUV_420_888 -> NV21 conversion, you can see the code sample here. The performance of this step can be optimized by using libyuv for the format conversion: https://chromium.googlesource.com/libyuv/libyuv

For compressing NV21 to JPEG with YuvImage, you can see the code sample here.

Answered By – Xi 张熹

Answer Checked By – Mildred Charles (Easybugfix Admin)

Leave a Reply

(*) Required, Your email will not be published