android-discuss icon indicating copy to clipboard operation
android-discuss copied to clipboard

Camera多次addCallbackBuffer的作用是什么?

Open start141 opened this issue 10 years ago • 5 comments

Camera类可以执行多次addCallbackBuffer方法,然后onPreviewFrame(byte[] data, Camera camera)回调会循环返回addCallbackBuffer添加的buffer(即onPreviewFrame返回的data),多次addCallbackBuffer的作用是什么?有什么样的场景适用?求大神速来回复,3Q!

start141 avatar Apr 24 '15 17:04 start141

源码里面这两个方法的注释说的很清楚了。 addCallbackBuffer()

Adds a pre-allocated buffer to the preview callback buffer queue. Applications can add one or more buffers to the queue. When a preview frame arrives and there is still at least one available buffer, the buffer will be used and removed from the queue. If a frame arrives and there is no buffer left, the frame is discarded.

setPreviewCallbackWithBuffer() :

Installs a callback to be invoked for every preview frame, using buffers supplied with addCallbackBuffer(byte[]), in addition to displaying them on the screen. The callback will be repeatedly called for as long as preview is active and buffers are available. The purpose of this method is to improve preview efficiency and frame rate by allowing preview frame memory reuse.

R1NC avatar Apr 25 '15 04:04 R1NC

感谢楼上的回答,不过答案没有围绕提问重点,提问是多次addCallbackBUffer,以及有哪些应用场景

来自 魅族 MX4 Pro

-------- 原始邮件 -------- 发件人:Rinc Liu [email protected] 时间:周六 4月25日 12:29 收件人:android-cn/android-discuss [email protected] 抄送:start141 [email protected] 主题:Re: [android-discuss] Camera多次addCallbackBuffer的作用是什么? (#143)

源码里面这两个方法的注释说的很清楚了。 addCallbackBuffer()

Adds a pre-allocated buffer to the preview callback buffer queue. Applications can add one or more buffers to the queue. When a preview frame arrives and there is still at least one available buffer, the buffer will be used and removed from the queue. Then preview callback is invoked with the buffer. If a frame arrives and there is no buffer left, the frame is discarded.

setPreviewCallbackWithBuffer() :

Installs a callback to be invoked for every preview frame, using buffers supplied with addCallbackBuffer(byte[]), in addition to displaying them on the screen. The callback will be repeatedly called for as long as preview is active and buffers are available. The purpose of this method is to improve preview efficiency and frame rate by allowing preview frame memory reuse.


Reply to this email directly or view it on GitHub: https://github.com/android-cn/android-discuss/issues/143#issuecomment-96127821

start141 avatar Apr 25 '15 04:04 start141

上面注释已经说的很清楚了。那我再翻译并概括一下: 1.可以多次添加buffer到queue,如果preview帧到达时该buffer可用就从queue取出来;如果不可用就丢弃该帧; 2.每次preview并且buffer可用的时候callback会被重复调用; 3.通过对buffer内存的重用可以提高preview效率和帧率; 至于适用场景,应该是需要实时预览,并且对质量和帧率要求很高的时候。

R1NC avatar Apr 25 '15 04:04 R1NC

@RincLiu 邀请你回答此问题: https://github.com/android-cn/android-discuss/issues/144

start141 avatar Apr 25 '15 04:04 start141

我们要获取相机预览的每一帧回调,就得在onPreviewFrame(byte[] data, Camera camera)中调用camera. addCallbackBuffer(data) ,系统复用data数组。如果这里不调用,会导致onPreviewFrame停止回调

pop1234o avatar Feb 26 '19 12:02 pop1234o