I want to manually render the raw bytes of an image (byte[]) to the Surface provided by ImageReader.
Let's assume there is an ImageReader configured as follows:
var reader = ImageReader.newInstance(width, height, ImageFormat.JPEG, 1);
var surface = reader.getSurface();
Is it possible to manually send the raw bytes of an image to the Surface so that the image can be obtained from the reader using reader.acquireNextImage()? Typically, this is handled by the CameraDevice or MediaCodec. However, I have a use case where I need to send an image manually.
I tried drawing a bitmap to the Canvas object,
Canvas canvas = surface.lockCanvas(null);
canvas.drawBitmap(bitmap, 0, 0, null);
surface.unlockCanvasAndPost(canvas);
In this case, reader.acquireNextImage() successfully returns an Image object, but attempting to access the planes with image.getPlanes() results in a crash with the following exception.
.....................
runtime.cc:669] native: #07 pc 0000000000006f14 /system/lib64/liblog.so (__android_log_assert+312)
runtime.cc:669] native: #08 pc 000000000000284c /system/lib64/libmedia_jni_utils.so (android::getLockedImageInfo(android::CpuConsumer::LockedBuffer*, int, int, unsigned char**, unsigned int*, int*, int*)+1764)
runtime.cc:669] native: #09 pc 0000000000041ad4 /system/lib64/libmedia_jni.so (Image_createSurfacePlanes(_JNIEnv*, _jobject*, int, int, unsigned long)+916)
runtime.cc:669] native: #10 pc 0000000000222244 /apex/com.android.art/lib64/libart.so (art_quick_generic_jni_trampoline+148)
runtime.cc:669] native: #11 pc 0000000000212b80 /apex/com.android.art/lib64/libart.so (nterp_helper+5648)
runtime.cc:669] native: #12 pc 000000000054131a [anon:dalvik-classes2.dex extracted in memory from /system/framework/framework.jar!classes2.dex] (android.media.ImageReader$SurfaceImage.getPlanes+50)
......................