Android Camera onPreviewFrame 帧率不一致 [英] Android Camera onPreviewFrame frame rate not consistent

查看:26
本文介绍了Android Camera onPreviewFrame 帧率不一致的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 MediaCodec 通过相机的 PreviewCall back(onPreviewFrame) 对每秒 30 帧的视频进行编码.我编码的视频总是播放得非常快(这是不希望的).因此,我尝试通过设置一个 int frameCount 变量来记住它的计数来检查进入我的相机预览的帧数.我期望的是每秒 30 帧,因为我将相机的预览设置为 30 fps(如下所示).我得到的结果是不一样的.我调用了 onPreviewFrame 回调 10 秒,我返回的 frameCount 数量只有大约 100 帧.这很糟糕,因为我期待 300 帧.我的相机参数设置正确吗?这是 Android 相机预览回调的限制吗?如果这是Android相机预览回调的限制,那么还有没有其他相机回调可以以每秒30帧的速度返回相机的图像数据(nv21,yuv,yv12)?

I am trying to encode a 30 frames per second video using MediaCodec through the Camera's PreviewCall back(onPreviewFrame). The video that I encoded always plays very fast(this is not desired). So, I tried to check the number of frames that is coming into my camera's preview by setting up a int frameCount variable to remember its count. What I am expecting is 30 frames per second because I setup my camera's preview to have 30 fps preview(as shown below). The result that I get back is not the same. I called the onPreviewFrame callback for 10 second, the number of frameCount I get back is only about 100 frames. This is bad because I am expecting 300 frames. Is my camera parameters setup correctly? Is this a limitation of Android's Camera preview call back? And if this is a limitation on the Android Camera's preview call back, then is there any other camera callback that can return the camera's image data(nv21,yuv, yv12) in 30 frames per second?

感谢您阅读并花时间提供帮助.我将不胜感激任何评论和意见.

thanks for reading and taking your time to helpout. i would appreciate any comments and opinions.

以下是使用相机的 onPreviewFrame 编码的视频示例:

Here is an example an encoded video using Camera's onPreviewFrame:

http://www.youtube.com/watch?v=I1Eg2bvrHLM&feature=youtu.be

                Camera.Parameters parameters = mCamera.getParameters();
                parameters.setPreviewFormat(ImageFormat.NV21);
                parameters.setPictureSize(previewWidth,previewHeight);
                parameters.setPreviewSize(previewWidth, previewHeight);
//              parameters.setPreviewFpsRange(30000,30000);

                parameters.setPreviewFrameRate(30);
                mCamera.setParameters(parameters);

                mCamera.setPreviewCallback(previewCallback);
                mCamera.setPreviewDisplay(holder);

推荐答案

不,Android 相机不保证稳定的帧率,尤其是 30 FPS.例如,它可能会在低光照条件下选择更长的曝光时间.

No, Android camera does not guarantee stable frame rate, especially at 30 FPS. For example, it may choose longer exposure at low lighting conditions.

但我们,应用开发者,有一些方法可以让事情变得更糟.

But there are some ways we, app developers, can make things worse.

首先,使用setPreviewCallback() 而不是setPreviewCallbackWithBuffer().这可能会对垃圾收集器造成不必要的压力.

First, by using setPreviewCallback() instead of setPreviewCallbackWithBuffer(). This may cause unnecessary pressure on the garbage collector.

其次,如果 onPreviewFrame() 到达主 (UI) 线程,您会导致任何 UI 操作直接延迟相机帧的到达.要将 onPreviewFrame() 保持在单独的线程上,您应该在辅助 Looper 线程上open() 相机.我在这里详细解释了如何实现:与其他类似类相比,HandlerThread 的最佳使用.

Second, if onPreviewFrame() arrives on the main (UI) thread, you cause any UI action directly delay the camera frames arrival. To keep onPreviewFrame() on a separate thread, you should open() the camera on a secondary Looper thread. Here I explained in detail how this can be achieved: Best use of HandlerThread over other similar classes.

第三,检查处理时间是否小于20ms.

Third, check that processing time is less than 20ms.

这篇关于Android Camera onPreviewFrame 帧率不一致的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆