Skip to content

Android Camera2 get size and fps info

update: 2022-2-8
  • 2022-2-8 create


We use android.hardware.Camera to get supported preview size and fps infomation. for camera instance, then camera.getParameters() for parameters.

for (int i = 0; i < Camera.getNumberOfCameras(); i++) {
    Camera camera = null;
    try {
        camera =;
        Parameters parameters = camera.getParameters();
        supportedSizes = parameters.getSupportedPreviewSizes();
        supportedFpsRanges = parameters.getSupportedPreviewFpsRange();
    } catch (RuntimeException e) {
        Log.e(TAG, "Failed to open, skipping", e);
    } finally {
        if (camera != null) {

android.hardware.Camera: we call it「camera」or「camera1」 belowing

Above code for some phones (eg. HONOR X10) that have lifting front camera, could rise the front camera. User may feel weird about it, the front camera just show up and go back.

HONOR X10 front camera

HONOR X10 front camera

We should consider using Camera2 to get preview size. No


gradle camera2

This demo is a module. Now we take a look in module gradle.

We need androidx packages.

  • compileSdk 31

apply plugin: ''

android {
    compileSdk 31
    defaultConfig {
        minSdkVersion 19
        targetSdkVersion 31
        versionCode 110
        versionName "1.1.0"
        testInstrumentationRunner ""

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), ''

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'androidx.appcompat:appcompat:1.3.1'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation ''
    androidTestImplementation ''

    implementation ""
    implementation ""
NOTICE: Here minSdkVersion 19. So we need to judge Android version.

camera2's version in beta01, we believe camera2 is developing.

Use camera2

First look at these imported package, most about camera2

import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraCharacteristics;
import android.hardware.camera2.CameraManager;
import android.hardware.camera2.params.StreamConfigurationMap;
import android.os.Build;

Check the running Android SDK version, required API >= 21(LOLLIPOP).

Get the CameraManager instance by using getSystemService.

    CameraManager cameraManager = (CameraManager) mContext.getSystemService(Context.CAMERA_SERVICE);
Not like camera1, camera2 can get the infomation about camera without calling

Camera.Size in android.hardware package is deprecated. Camera2 uses android.util.Size.

List<android.util.Size> supportedUtilSize = new ArrayList<>();
CameraCharacteristics characteristics = cameraManager.getCameraCharacteristics("0"); // 0 or 1
StreamConfigurationMap configs = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
supportedUtilSize = Arrays.asList(configs.getOutputSizes(ImageFormat.JPEG));

For fps range, here use Range class.

CameraCharacteristics characteristics = cameraManager.getCameraCharacteristics("1"); // 0 or 1
Range<Integer>[] ranges21 = characteristics.get(CameraCharacteristics.CONTROL_AE_AVAILABLE_TARGET_FPS_RANGES);

Don't forget to check Android version

    // do your work


We got Range to store fps range in above code.

package android.util;

public final class Range<T extends Comparable<? super T>> {

Range needs T who extends Comparable. We used Range before, here is Integer class infomation.

package java.lang;

public final class Integer extends Number implements Comparable<Integer> 

Range provides some useful functions. It remands me set in math.

Some function:

public boolean contains(T value)

eg. We got [1, 100], input 99, output true.

public boolean contains(Range<T> range)

eg. For [1, 300], input [2, 333], output false.

public T clamp(T value)

eg. For [1, 10], input 3, output 3. Input 32, output 10(the max value).

also publish at

作者: |
示例: AndroidTutorial Gitee, Tutorial Github