How to fix the frame rate of camera in Android phone

14,277

Solution 1

Even for the old camera API, if the device lists a supported FPS range of (30, 30), then you should be able to select this range and get consistent, fixed video recording.

Unfortunately, some devices disregard your frame rate request once the scene conditions get too dark, and increase exposure times past 1/30s. For many applications, this is the preferable option, but such applications should simply be selecting a wider frame rate range like (15, 30).

Solution 2

First of all, please note that the camera API that you ask about was deprecated more than 3 years ago. The new camera2 API provides much more control over all aspects of capture, including frame rate.

Especially, if your goal is smooth video recording. Actually, the MediaRecorder performs its job decently on older devices, but I understand that this knowledge has little practical value if for some reason you cannot use the MediaRecorder.

Usually, the list of supported FPS ranges includes fixed ranges, e.g. 30 fps, intended exactly for video recording. Note that you are expected to choose a compliant (recommended) preview (video) resolution.

Two major factors cause frame rate variations within the declared range: exposure adjustments and focus adjustments. To achieve uniform rate, you should disable autofocus. If your camera supports exposure control, you should lock it, too. Refrain from using exotic "scenes" and "effects". SCENE_MODE_BARCODE and EFFECT_MONO don't seem to cause problems with frame rate. Whitebalance is OK, too.

There exist other factors that cause frame rate fluctuations that are completely under your control.

  1. Make sure that your camera callbacks do not interfere with, and are not delayed by the Main (UI) thread. To achieve that, you must open the camera on a secondary HandlerThread. The new camera2 API makes thread management for camera callbacks easier.

  2. Don't use setPreviewCallback() which automatically allocates pixel buffers for each frame. This is a significant burden for garbage collector, which may lock all threads once in a while for major cleanup. Instead, use setPreviewCallbackWithBuffer() and preallocate just enough pixel buffers to keep it always busy.

  3. Don't perform heavy calculations in the context of your onPreviewFrame() callback. Pass all work to a different thread. Do your best to release the pixel buffer as early as possible.

Share:
14,277

Related videos on Youtube

Jun Fang
Author by

Jun Fang

Updated on June 04, 2022

Comments

  • Jun Fang
    Jun Fang almost 2 years

    I want to fix the frame rate of camera preview in Android, i.e., 20fps, or 30 fps. However, we find the frame rate is unstable.

    In the android document, it is said that the frame rate is fluctuated between the minimum frame rate and the maximum one which are defined in getSupportedPreviewFpsRange. https://developer.android.com/reference/android/hardware/Camera.Parameters.html#getSupportedPreviewFpsRange%28%29

    My questions are: 1) Which factors influence the frame rate? exposure time, white balance, frame resolution, background CPU loading, and etc.? 2) Is there any method to fix the frame rate by customised above factors? 3) In my project, higher frame rate is better. If the frame rate is unstable in the end. Can I increase the minimum frame rate? or fix the minimum frame rate? 4) It seems that the video taking is somewhat different with preview model, Can I fix the frame rate or minimum frame rate of video taking in Android?

    Finally, we found that IOS can fix the frame rate using videoMinFrameDuration and videoMaxFrameDuration.

    Thanks.

  • Jun Fang
    Jun Fang about 6 years
    Thanks Alex. Your answer tells me that we can achieved relatively stable fps if we lock exposure time, focus, and some other factors that you mentioned, however, we still don't know the exact fps until we run it. Is there a camera2 API that can fix the fps like IOS? What i means is that if we can achieve around 30fps, i may rather fix it to more stable 20 fps.
  • Alex Cohn
    Alex Cohn about 6 years
    Your assumption is wrong: less is not necessarily more stable. On many devices, 30 FPS is supported, while 20 FPS can only be achieved by tricky unreliable delays.