本讲是android camera专题系列的第12讲,我们介绍android camera2 api专题的streamconfigurationmap实战之如何获取录像size。
更多资源:
资源 | 描述 |
---|---|
在线课程 | |
知识星球 | 星球名称:深入浅出android camera 星球id: 17296815 |
极客笔记圈 |
获取录像size
录像组件
- mediarecorder / mediacodec
需要考虑的点
- 录像size:camera支持列表 与 encoder支持列表 交集
-
正常帧率
-
高帧率
-
判断video encoder是否支持某种size和fps
/**
* check if encoder can support this size and frame rate combination by querying
* mediacodec capability. check is based on size and frame rate. ignore the bit rate
* as the bit rates targeted in this test are well below the bit rate max value specified
* by avc specification for certain level.
*/
private static boolean issupportedbyavcencoder(android.util.size sz, int framerate) {
mediaformat format = mediaformat.createvideoformat(
mediaformat.mimetype_video_avc, sz.getwidth(), sz.getheight());
format.setinteger(mediaformat.key_frame_rate, framerate);
mediacodeclist mcl = new mediacodeclist(mediacodeclist.regular_codecs);
return mcl.findencoderforformat(format) != null;
}
获取camera支持的录像正常帧率size列表
private void doinitsupportedvideosizes(camerafeatures camera_features,
streamconfigurationmap configs) throws cameracontrollerexception {
android.util.size[] camera_video_sizes = configs.getoutputsizes(mediarecorder.class);
camera_features.msupportedvideosizes = new arraylist<>();
int min_fps = 9999;
for(int[] r : this.maefpsranges) {
min_fps = math.min(min_fps, r[0]);
}
if( camera_video_sizes == null ) {
// camera_video_sizes is null on samsung galaxy note 10 and s20 for camera id 4!
log.e(tag, "no video sizes returned by getoutputsizes");
throw new cameracontrollerexception();
}
else {
for(android.util.size camera_size : camera_video_sizes) {
long mfd = configs.getoutputminframeduration(mediarecorder.class, camera_size);
int max_fps = (int)((1.0 / mfd) * 1000000000l);
if (issupportedbyavcencoder(camera_size, max_fps)) {
arraylist fr = new arraylist<>();
fr.add(new int[] {max_fps, max_fps});
cameracontroller.size normal_video_size = new cameracontroller.size(
camera_size.getwidth(),
camera_size.getheight(),
fr,
false);
camera_features.msupportedvideosizes.add(normal_video_size);
if( mydebug.log ) {
log.i(tag, "doinitsupportedvideosizes normal video size: " normal_video_size
",max fps:" max_fps);
}
} else {
log.e(tag, "doinitsupportedvideosizes not support!"
"camera_size:" camera_size.tostring()
",max_fps:" max_fps);
}
}
}
collections.sort(camera_features.msupportedvideosizes, new cameracontroller.sizesorter());
}
获取camera支持的录像高帧率size列表
private void doinitsupportedhighspeedvideosizes(camerafeatures camera_features,
streamconfigurationmap configs) {
hs_fps_ranges = new arraylist<>();
camera_features.msupportedvideosizeshighspeed = new arraylist<>();
for (range r : configs.gethighspeedvideofpsranges()) {
hs_fps_ranges.add(new int[] {r.getlower(), r.getupper()});
}
collections.sort(hs_fps_ranges, new cameracontroller.rangesorter());
if( mydebug.log ) {
log.i(tag, "supported high speed video fps ranges: ");
for (int[] f : hs_fps_ranges) {
log.i(tag, " hs range: [" f[0] "-" f[1] "]");
}
}
android.util.size[] camera_video_sizes_high_speed = configs.gethighspeedvideosizes();
for(android.util.size camera_size : camera_video_sizes_high_speed) {
for (range r : configs.gethighspeedvideofpsrangesfor(camera_size)) {
int profile_fps = getfpsfromhighspeedprofileforsize(camera_size);
if (r.getupper() > r.getlower()) {
log.e(tag, "doinitsupportedhighspeedvideosizes fps range:" r.tostring());
continue;
}
if (!issupportedbyavcencoder(camera_size, r.getupper())) {
log.e(tag, "doinitsupportedhighspeedvideosizes high speed recording "
camera_size "@" r.getlower() "fps"
" is not supported by camcorderprofile, fps:" profile_fps);
continue;
}
arraylist fr = new arraylist<>();
fr.add(new int[] { r.getlower(), r.getupper()});
cameracontroller.size hs_video_size = new cameracontroller.size(
camera_size.getwidth(),
camera_size.getheight(),
fr,
true);
if (mydebug.log) {
log.i(tag, "doinitsupportedhighspeedvideosizes high speed video size: "
hs_video_size
", fps range:" r.tostring());
}
camera_features.msupportedvideosizeshighspeed.add(hs_video_size);
}
}
collections.sort(camera_features.msupportedvideosizeshighspeed, new cameracontroller.sizesorter());
}