Adjorno
Adjorno

Reputation: 51

How to create android.media.Image of the YUV_420 format from the Bitmap?

I am trying to mock Camera API in order to come up with end-to-end test. The Camera API produces android.media.Image(s) and posts it to the Surface to be consumed by ImageReader.acquireLatestImage().

My idea is to create a mechanism based on ImageWriter so I could queue predefined test JPEG images or video files in order to mimic Camera API functionality.

As far as I understand there are two options:

Unfortunately I could not get any success at the moment.

Does someone know any working method to mock Camera dependency but keep the data source?

Upvotes: 1

Views: 992

Answers (1)

Adjorno
Adjorno

Reputation: 51

The library libyuv-android (https://github.com/crow-misia/libyuv-android) has helped with the problem. Something like this:

val yuvBuffer = I420Buffer.allocate(width, height)
val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
val argbBuffer = AbgrBuffer.allocate(width, height)
bitmap.copyPixelsToBuffer(argbBuffer.asBuffer())
argbBuffer.convertTo(yuvBuffer)
val imageWriter = ImageWriter.newInstance(targetSurface, 1, ImageFormat.YUV_420_888)

val image = imageWriter.dequeueInputImage()
image.planes[0].buffer.put(yuvBuffer.planeY.buffer)
image.planes[1].buffer.put(yuvBuffer.planeU.buffer)
image.planes[2].buffer.put(yuvBuffer.planeV.buffer)
imageWriter.queueInputImage(image)

Upvotes: 1

Related Questions