Android: setPreviewCallbackWithBuffer, buffer in the queue and no callbacks

来源:互联网 发布:mac 笔记本电脑 编辑:程序博客网 时间:2024/05/15 21:44

If you want to process Camera preview frames on Android, you need to acquire them withsetPreviewCallback or setPreviewOneShotCallback or setPreviewCallbackWithBuffer on the Camera.

setPreviewCallback works as advertised, but slows the camera to just few frames per second. For better camera FPS, usesetPreviewCallbackWithBuffer (it can re-use the same buffer without allocating it every frame). Just add the same buffer to the queue after it is processed in Camera.PreviewCallback. If the queue is empty, the preview frames are skipped (and it may improve FPS too, so it is a good thing).

My problem was that even if I had the buffer in the queue, the callback didn't run. However, if I calledsetPreviewCallbackWithBuffer some seconds later (not in onResume of the activity), the entire thing worked as intended.

I suppose, it has something to do with the surface for the preview not being ready when I call thesetPreviewCallbackWithBuffer. This bug may be related: http://code.google.com/p/android/issues/detail?id=13966

My workaround is:

  • onResume of the activity, do setPreviewOneShotCallback; and the callback will happen when the camera is ready;
  • in the callback method, when it is called the first time, allocate the buffer andsetPreviewCallbackWithBuffer;
  • after every callback addCallbackBuffer to the Camera.
0 0
原创粉丝点击