Android: setPreviewCallbackWithBuffer, buffer in the queue and no callbacks

If you want to process Camera preview frames on Android, you need to acquire them with setPreviewCallback 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, use setPreviewCallbackWithBuffer (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 called setPreviewCallbackWithBuffer 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 the setPreviewCallbackWithBuffer. 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 and setPreviewCallbackWithBuffer;
  • after every callback addCallbackBuffer to the Camera.

你可能感兴趣的:(Android: setPreviewCallbackWithBuffer, buffer in the queue and no callbacks)