Android: setPreviewCallbackWithBuffer, buffer in the queue and no callbacks

本文探讨了在Android应用中优化相机预览帧处理速度的方法,通过使用setPreviewCallbackWithBuffer替代setPreviewCallback,并在回调方法中合理管理缓冲区,实现更高效的帧处理流程。同时,文章指出了在回调时机上的工作规避策略,以及可能存在的预览表面未准备好的问题,提供了解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值