(原)阅读Android-Camera2Video的demo源码和调试心得
//选择满足4:3的长宽比例的尺寸分辨率
mVideoSize = chooseVideoSize(map.getOutputSizes(MediaRecorder.class));
//获取一下摄像头支持的最大分辨率,防止摄像头不支持,导致没有图像
Size cameraLargest= Collections.max(Arrays.asList(map.getOutputSizes(ImageFormat.JPEG)),new CompareSizesByArea());
//获取最佳的长宽比预览尺寸
// mPreviewSize = chooseOptimalSize(map.getOutputSizes(SurfaceTexture.class),
// width, height, mVideoSize);
//针对rk3288-walkera-board,camera只能打开720p
mPreviewSize = chooseOptimalSize(map.getOutputSizes(SurfaceTexture.class),
1280, 720, cameraLargest);
我们先看一下chooseVideoSize函数:
//这个函数根据长宽比,选择只支持长宽比为4:3的分辨率,同时宽小于1080p
private static Size chooseVideoSize(Size[] choices) {
for (Size size : choices) {
if (size.getWidth() == size.getHeight() * 4 / 3 && size.getWidth() <= 1080) {
return size;
}
}
Log.e(TAG, "Couldn't find any suitable video size");
return choices[choices.length - 1];
}
在这个函数执行以后,得到的分辨率为800x600的分辨率。
然后再来看一下chooseOptimalSize 的选择策略:
//这个函数选择长比宽为aspectRotio一样的分辨率,同时如果长宽大于指定的宽高,就选用中间最小的一个,否则选用choices[0]
private static Size chooseOptimalSize(Size[] choices, int width, int height, Size aspectRatio) {
// Collect the supported resolutions that are at least as big as the preview Surface
//选择合适的长宽比的分辨率
List<Size> bigEnough = new ArrayList<Size>();
int w = aspectRatio.getWidth();
int h = aspectRatio.getHeight();
for (Size option : choices) {
if (option.getHeight() == option.getWidth() * h / w &&
option.getWidth() >= width && option.getHeight() >= height) {
bigEnough.add(option);
} } // Pick the smallest of those, assuming we found any
if (bigEnough.size() > 0) {
return Collections.min(bigEnough, new CompareSizesByArea());
}
else {
Log.e(TAG, "Couldn't find any suitable preview size");
return choices[0];
}
}
针对这种选择策略,我个人觉得很不适用。函数执行结果因为找不到满足的分辨率,所以会进入else的选择,最好返回choices[0],即选择1080p的预览分辨率。
所以导致后面打开摄像头会出现无视频画面的情况。
其实我个人的认为,在选择最佳分辨率的情况,应该是当设备支持的分辨率中有比预览指定的分辨率大的集合的时候,选集合中最小的设备分辨率。否则选择比预览分辨率小的集合中最大的分辨率。
2)点击录像,然后录像停止以后,生成录像文件,但程序崩溃。
出现问题的崩溃点为 startPreview 中从新创建预览请求的函数:
// 创建预览需要的CaptureRequest.Builder
mPreviewBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
后面经过搜索和查找,解决方法为:
private void stopRecordingVideo() {
// UI
mIsRecordingVideo = false;
mButtonVideo.setText(R.string.record);
//modefy by lihaiping1603@aliyun.com on20161207
//这个地方需要优化一下,防止录像的时候会崩溃
// try{
// mPreviewSession.abortCaptures();
// }catch (CameraAccessException e) {
// e.printStackTrace();
// }
//试一下使用close方式,在录像stop前,调用关闭也是可以解决崩溃的问题的
closePreviewSession();
// Stop recording
mMediaRecorder.stop();
mMediaRecorder.reset();
//在停止录像以后调用关闭会话,程序会崩溃
// //试一下使用close方式
// closePreviewSession();
Activity activity = getActivity();
if (null != activity) {
Toast.makeText(activity, "Video saved: " + mNextVideoAbsolutePath,
Toast.LENGTH_SHORT).show();
Log.d(TAG, "Video saved: " + mNextVideoAbsolutePath);
}
mNextVideoAbsolutePath = null;
startPreview();
}
后面经过翻看abortCaptures()的官方解释:
public abstract void abortCaptures ()
Discard all captures currently pending and in-progress as fast as possible.
The camera device will discard all of its current work as fast as possible. Some in-flight captures may complete successfully and call onCaptureCompleted(CameraCaptureSession, CaptureRequest, TotalCaptureResult), while others will trigger their onCaptureFailed(CameraCaptureSession, CaptureRequest, CaptureFailure) callbacks. If a repeating request or a repeating burst is set, it will be cleared.
This method is the fastest way to switch the camera device to a new session with createCaptureSession(List, CameraCaptureSession.StateCallback, Handler) orcreateReprocessableCaptureSession(InputConfiguration, List, CameraCaptureSession.StateCallback, Handler), at the cost of discarding in-progress work. It must be called before the new session is created. Once all pending requests are either completed or thrown away, the onReady(CameraCaptureSession) callback will be called, if the session has not been closed. Otherwise, the onClosed(CameraCaptureSession)callback will be fired when a new session is created by the camera device.
Cancelling will introduce at least a brief pause in the stream of data from the camera device, since once the camera device is emptied, the first new request has to make it through the entire camera pipeline before new output buffers are produced.
This means that using abortCaptures() to simply remove pending requests is not recommended; it's best used for quickly switching output configurations, or for cancelling long in-progress requests (such as a multi-second capture).
至于原因,暂时还不是太清楚,但加上我上面几个函数,就可以解决崩溃的问题。
(原)阅读Android-Camera2Video的demo源码和调试心得的更多相关文章
- Android 自定义相机Demo源码
Github源码:https://github.com/LinJZong/AndroidProject.git 模仿360相机,图片资源来源于360相机,仅供学习使用.使用过程中遇到问题或Bug可发我 ...
- 微信小程序初探(二):阅读官方demo源码
阅读demo有助于理解逻辑,而且demo源码中应该包含了框架开发人员想要表达的意思的精华,先从app.js着手来阅读. 附带贴下说明: https://mp.weixin.qq.com/debug/w ...
- Android Studio 的蓝牙串口通信(附Demo源码下载)
根据相关代码制作了一个开源依赖包,将以下所有的代码进行打包,直接调用即可完成所有的操作.详细说明地址如下,如果觉得有用可以GIthub点个Star支持一下: 项目官网 Kotlin版本说明文档 Jav ...
- android 近百个源码项目【转】
http://www.cnblogs.com/helloandroid/articles/2385358.html Android开发又将带来新一轮热潮,很多开发者都投入到这个浪潮中去了,创造了许许多 ...
- [原]在win上编译 subversion 源码实践Tonyfield的专栏
(百度和网页的作者无关,不对其内容负责。百度快照谨为网络故障时之索引,不代表被搜索网站的即时页面。) [原]在win上编译 subversion 源码实践 2013-6-9阅读400 评论0 (参考 ...
- Asp.net MVC集成Google Calendar API(附Demo源码)
Asp.net MVC集成Google Calendar API(附Demo源码) Google Calendar是非常方便的日程管理应用,很多人都非常熟悉.Google的应用在国内不稳定,但是在国外 ...
- 近期热门微信小程序demo源码下载汇总
近期微信小程序demo源码下载汇总,乃小程序学习分析必备素材!点击标题即可下载: 即速应用首发!原创!电商商场Demo 优质微信小程序推荐 -秀人美女图 图片下载.滑动翻页 微信小程序 - 新词 GE ...
- 【转】Ubuntu 14.04.3上配置并成功编译Android 6.0 r1源码
http://www.linuxidc.com/Linux/2016-01/127292.htm 终于成功把Android 6.0 r1源码的源码编译.先上图,这是在Ubuntu中运行的Android ...
- 使用CEF(三)— 从CEF官方Demo源码入手解析CEF架构与CefApp、CefClient对象
在上文<使用CEF(2)- 基于VS2019编写一个简单CEF样例>中,我们介绍了如何编写一个CEF的样例,在文章中提供了一些代码清单,在这些代码清单中提到了一些CEF的定义的类,例如Ce ...
随机推荐
- BZOJ.3064.CPU监控(线段树 历史最值)
题目链接 \(Description\) 有一个长为n的序列Ai,要求支持查询[l,r]的最值.历史最值,区间加/重设 \(Solution\) 线段树,每个点再维护一个历史(从0到现在)最大值.历史 ...
- JVM笔记(一)数字在JVM中的表示
数字在JVM中的表示 一.整数在JVM中的表示 1.在Java虚拟机中,整数有四种表示: byte:8位 short:16位 int:32位 long:64位 2.原码. ...
- Codechef October Challenge 2018 游记
Codechef October Challenge 2018 游记 CHSERVE - Chef and Serves 题目大意: 乒乓球比赛中,双方每累计得两分就会交换一次发球权. 不过,大厨和小 ...
- centos7 修改同步时间
同步时间安装ntp软件 [root@node2 ~]# yum install ntp 将ntp设为开机启动 [root@node2 ~]# systemctl enable ntpd 修改启动参数, ...
- USBWriter之后恢复磁盘大小
USBWriter之后恢复磁盘大小的方法: 1,cmd 2,diskpart 3,list disk 4,select disk *(*你的U盘代号) (e.g:U盘为磁盘 2 ,则代号为2 ,使用命 ...
- 10款免费开源PHP框架
NO.1 Laravel - 巨匠级PHP开发框架 现在最流行的一款PHP框架,功能强大,学习和使用也非常简单方便,即使你是PHP新手,也不用担心入门问题. NO.2 Phalcon - 最快的PHP ...
- Java虚拟机的最大内存是多少
天分析了当前比较流行的几个不同公司不同版本JVM的最大内存,得出来的结果如下: 公司 JVM版本 最大内存(兆)client 最大内存(兆)server SUN 1.5.x 1492 1520 SUN ...
- AngularJS中处理多个promise
在使用AngularJS中处理promise的时候,有时会碰到需要处理多个promise的情况. 最简单的处理就是每个promise都then.如下: var app = angular.module ...
- html5调用手机陀螺仪实现方向辨识
获取移动设备的陀螺仪,需要知道陀螺仪包含什么. 我们可以让document监听deviceorientation 来获取相关的数据,里面包括3个值 alpha.beta和gamma. 这三个值分别代表 ...
- 操作argc, argv的经典写法
[问题] 我在看boost源代码的时候看到如下的代码, template<class charT> basic_command_line_parser<charT>:: bas ...