onImageAvailable callback called but acquireLatestImage returns NO_BUFFER_AVAILABLE

492 views Asked by At

I am working on Camera2 API to take pictures continuously in native side in C and it's working fine except that sometimes after receivinf onImageAvailable callback, when calling acquireLatestImage, return is NO_BUFFER_AVAILABLE. As per Android documentation : https://developer.android.com/ndk/reference/struct/a-image-reader-image-listener#onimageavailable

Note that it is possible that calling AImageReader_acquireNextImage or AImageReader_acquireLatestImage returns AMEDIA_IMGREADER_NO_BUFFER_AVAILABLE within this callback. For example, when there are multiple images and callbacks queued, if application called AImageReader_acquireLatestImage, some images will be returned to system before their corresponding callback is executed

Can someone please explain when actually this can happen and possible solution for this.

1

There are 1 answers

2
Eddy Talvala On

If you have multiple images already captured into the AImageReader, then calling AImageReader_acquireLatestImage will discard all of them except the newest, and then returns the newest one.

So you get a sequence like this:

onImageAvailable()
onImageAvailable()
onImageAvailable()
acquireLatestImage() -> OK
acquireLatestImage() -> NO_BUFFER_AVAILABLE
acquireLatestImage() -> NO_BUFFER_AVAILABLE
onImageAvailable()
acquireLatestImage() -> OK

The second call to acquireLatestImage() will get NO_BUFFER_AVAILABLE since the previous call discarded all other buffers, and no new images arrived before the second call.

If you want to always see all image buffers, then use acquireNextImage(), which does not discard older buffers and just returns the next one in the queue.

onImageAvailable()
onImageAvailable()
onImageAvailable()
acquireNextImage() -> OK
acquireNextImage() -> OK
acquireNextImage() -> OK
acquireNextImage() -> NO_BUFFER_AVAILABLE
onImageAvailable()
acquireNextImage() -> OK