Preview callback in Camera2 is significantly slower than in Camera1

半世苍凉 提交于 2019-12-03 09:29:44

I had the same performance problems on an app supporting both Camera1 and Camera2 APIs. When Android version was above Lollipop I used to switch to Camera2 API resulting in very bad performances (I had two target at time: an ImageReader and a Surface).

I ended up to use Camera2 API only when there was full Hardware support by the phone. You can check using the CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL

Hope it helps

This is just an observation but I'll post it anyway.

You say you are registering an OnImageAvailableListener. This listener does not deliver images but a reference to the same ImageReader you subscribed to. And then you must call either acquireLatestImage or acquireNextImage to get the actual image.

There is a paragraph in the docs that might be helpful to understand what is going on:

The image data is encapsulated in Image objects, and multiple such objects can be accessed at the same time, up to the number specified by the maxImages constructor parameter. New images sent to an ImageReader through its Surface are queued until accessed through the acquireLatestImage() or acquireNextImage() call. Due to memory limits, an image source will eventually stall or drop Images in trying to render to the Surface if the ImageReader does not obtain and release Images at a rate equal to the production rate.

So some things that might help:

  • request large memory in the manifest
  • Pass a large enough maxImages argument to the ImageReader constructor (You would get IllegalStateException if you exhaust the queue anyway).
  • Prefer acquireLatestImage over acquireNextImage for real-time processing. This method releases older images automatically while the other one does not, and thus using acquireNextImage by mistake will increasingly slowdown image delivery until you run out of memory.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!