0

I trying to create an Android App that using Camera2 API, as part of the functionality I want to develop a module that saving multiple images produced by ImageReader as followed:

Image image = reader.acquireLatestImage();

I'm getting the followed Exception:

IllegalStateException too many images are currently acquired

as mentioned in the documention: https://developer.android.com/reference/android/media/ImageReader#acquireLatestImage()

This is because the image returned from 'acquireLatestImage' is still belongs to the ImageReader Queue.

Is there any way to detach images returning from 'ImageReader' ?
Is there a way to copy an image, preferably without to store it on disk, that is a resource consuming operation ?

Thank's

LiorA
  • 53
  • 1
  • 12

1 Answers1

1

If this is a YUV_420_888 image, you can copy each of the plane ByteBuffers to keep the data around indefinitely. That is somewhat expensive, of course.

Unfortunately, there's no way to easily detach the Image from the ImageReader. The Reader is a circular buffer queue internally, so removing an Image would require the Reader to allocate a new image to replace the one removed, which is somewhat expensive as well.

It can be done by using an ImageWriter.queueInputImage, connected to another ImageReader with the same configuration as the original ImageReader. When you receive image A from your first ImageReader, and you want to keep it, you can queue it into the ImageWriter, and then get the Image handle again from the second ImageReader. Still need to set maxImages on the second reader high enough to account for all the Images you want to keep around at once, of course. That's fairly cumbersome, of course, and if you're doing this continually you'll cause a lot of memory reallocation and copies may be just as expensive (and simpler in many ways).

Eddy Talvala
  • 17,243
  • 2
  • 42
  • 47
  • for now I just defined the queue size to be one image larger, and hold it as a (final)global variable in a different class. I know its not the best practice solution, but it works for now, until I will implement another solution. Thanks on the effort. – LiorA Jun 18 '20 at 13:15
  • 1
    Increasing the queue size is perfectly reasonable for a small count of images - the Reader doesn't allocate that many images unless it needs to, and if you really do need to keep the image around, there's no way around needing memory for it somewhere. – Eddy Talvala Jun 18 '20 at 18:46