mebjas / mebjas.github.io

Repository for hosting my personal home page and blog.
https://blog.minhazav.dev
Apache License 2.0
13 stars 6 forks source link

How to write Image to ImageWriter in Android | Minhaz’s Blog #45

Open utterances-bot opened 1 year ago

utterances-bot commented 1 year ago

How to write Image to ImageWriter in Android | Minhaz’s Blog

Android framework provides ImageWriter class as a way for applications to produce Image data int a Surface which can then be consumed by other components like CameraDevice. I found a couple of hiccups while trying to write jpeg images to ImageWriter buffers and found that there is scarce documentation around this out in the wild. In this article I’ll share briefly on how to write jpeg images or YUV images to ImageWriter in Android.

https://blog.minhazav.dev/how-to-write-images-to-ImageWriter-in-Android/

dburckh commented 1 year ago

I've been struggling with this issue for quite a while.

  1. I haven't found a device that will allow me to create a ImageWriter with ImageFormat.JPEG on a SurfaceView. I always get "Exception configuring surface"
  2. The YuvImage class is a great way to convert YUV to JPEG. I'm using it with YUY2. No problems.
  3. I have managed to get an ImageWriter to write YUV420 to a SurfaceView. The best way I have found to convert JPEG to YUV420 is to decode the JPEG with BitmapFactory, then process it with libyuv. Libyuv is stupid fast. The problem is figuring out which YUV420 variant the Image is using. Luckily, Android only supports NV21 and YV12. If your plane[1] pixelStride is 2, it's NV21 else it's YV12(I420 in libyuv)
  4. You can also directly convert YUY2 to YUV420 with libyuv.

A few libyuv Android libraries from github: https://github.com/crow-misia/libyuv-android https://github.com/dburckh/libyuv-aar