Skip to content

Android Camera2 get size and fps info

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

Camera

We use android.hardware.Camera to get supported preview size and fps infomation.

Camera.open for camera instance, then camera.getParameters() for parameters.

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

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

Above code for some phones (eg. HONOR X10) that have lifting front camera, Camera.open 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 moreCamera.open.

Camera2

gradle camera2

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

We need androidx packages.

  • compileSdk 31
  • androidx.camera:camera-core:1.1.0-beta01
  • androidx.camera:camera-camera2:1.1.0-beta01

apply plugin: 'com.android.library'

android {
    compileSdk 31
    defaultConfig {
        minSdkVersion 19
        targetSdkVersion 31
        versionCode 110
        versionName "1.1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

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

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'androidx.appcompat:appcompat:1.3.1'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

    implementation "androidx.camera:camera-core:1.1.0-beta01"
    implementation "androidx.camera:camera-camera2:1.1.0-beta01"
}
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.

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    CameraManager cameraManager = (CameraManager) mContext.getSystemService(Context.CAMERA_SERVICE);
}
Not like camera1, camera2 can get the infomation about camera without calling Camera.open.

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

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    // do your work
}

Range

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 AndroidTutorial - Github 反馈问题 - Gitee 最近更新的文章 请作者喝咖啡

Ads