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
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"
}
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);
}
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
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
Range¶
We got Range
Range needs T who extends Comparable. We used Range
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
作者: rustfisher.com | rf.cs@foxmail.com
示例: AndroidTutorial Gitee, Tutorial Github
本文链接: https://www.an.rustfisher.com/android/media/camera2-get-size-fps-en/
一家之言,仅当抛砖引玉。如有错漏,还请指出。如果喜欢本站的内容,还请支持作者。也可点击1次下方的链接(链接内容与本站无关),谢谢支持服务器。
如有疑问,请与我联系:Android issues - gitee