转载请注明出处:http://www.cnblogs.com/lihaiping/p/6142512.html
 
最近因为项目需要使用到camera的功能,所以针对官方的demo源码进行一番阅读,并修改了一个record录像以后程序崩溃的bug。
 
这里主要记录下调试过程的情况:
 
1)打开rk3288-walkera-board上基于android5.1的camera以后,出现无视频画面的黑屏情况。
 经过查找主要是因为camera适用720P打开,而在程序的预览过程中,选择用了1080p打开camera,导致视频画面出不来。
相关代码在opencamera函数中:
            //选择满足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 ()

Added in API level 21

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源码和调试心得的更多相关文章

  1. Android 自定义相机Demo源码

    Github源码:https://github.com/LinJZong/AndroidProject.git 模仿360相机,图片资源来源于360相机,仅供学习使用.使用过程中遇到问题或Bug可发我 ...

  2. 微信小程序初探(二):阅读官方demo源码

    阅读demo有助于理解逻辑,而且demo源码中应该包含了框架开发人员想要表达的意思的精华,先从app.js着手来阅读. 附带贴下说明: https://mp.weixin.qq.com/debug/w ...

  3. Android Studio 的蓝牙串口通信(附Demo源码下载)

    根据相关代码制作了一个开源依赖包,将以下所有的代码进行打包,直接调用即可完成所有的操作.详细说明地址如下,如果觉得有用可以GIthub点个Star支持一下: 项目官网 Kotlin版本说明文档 Jav ...

  4. android 近百个源码项目【转】

    http://www.cnblogs.com/helloandroid/articles/2385358.html Android开发又将带来新一轮热潮,很多开发者都投入到这个浪潮中去了,创造了许许多 ...

  5. [原]在win上编译 subversion 源码实践Tonyfield的专栏

    (百度和网页的作者无关,不对其内容负责。百度快照谨为网络故障时之索引,不代表被搜索网站的即时页面。) [原]在win上编译 subversion 源码实践 2013-6-9阅读400 评论0 (参考 ...

  6. Asp.net MVC集成Google Calendar API(附Demo源码)

    Asp.net MVC集成Google Calendar API(附Demo源码) Google Calendar是非常方便的日程管理应用,很多人都非常熟悉.Google的应用在国内不稳定,但是在国外 ...

  7. 近期热门微信小程序demo源码下载汇总

    近期微信小程序demo源码下载汇总,乃小程序学习分析必备素材!点击标题即可下载: 即速应用首发!原创!电商商场Demo 优质微信小程序推荐 -秀人美女图 图片下载.滑动翻页 微信小程序 - 新词 GE ...

  8. 【转】Ubuntu 14.04.3上配置并成功编译Android 6.0 r1源码

    http://www.linuxidc.com/Linux/2016-01/127292.htm 终于成功把Android 6.0 r1源码的源码编译.先上图,这是在Ubuntu中运行的Android ...

  9. 使用CEF(三)— 从CEF官方Demo源码入手解析CEF架构与CefApp、CefClient对象

    在上文<使用CEF(2)- 基于VS2019编写一个简单CEF样例>中,我们介绍了如何编写一个CEF的样例,在文章中提供了一些代码清单,在这些代码清单中提到了一些CEF的定义的类,例如Ce ...

随机推荐

  1. 设置Sublime Text 3的光标样式

    升级了Sublime Text 3,结果光标变成了这个样子,非常不习惯: 查了文档http://www.sublimetext.com/3 ,Build 3059中得描述: Added setting ...

  2. ADODB 手册

        PHP ADODB1.99版手册 (修正版)   PHP ADODB 1.99版手册中文翻译 <修正版> ADODB PHP 在数据库的支持上是很令人称道的,几乎所有的知名数据库系 ...

  3. linux 校准时间

    ntpdate cn.pool.ntp.org //查看硬件时间可以是用hwclock,hwclock --show 或者hwclock -r [root@localhost ~]# hwclock ...

  4. Linux的SSH免密登录认证过程研究

    一.先看下SSH免密登录使用到的工具和生成的文件 工具:ssh-keygen用于生成秘钥文件,其中秘钥分为公钥和私钥.ssh-copy-id用于复制公钥文件到被控制机. 文件:ssh-keygen生成 ...

  5. Python开源框架、库、软件和资源大集合

    A curated list of awesome Python frameworks, libraries, software and resources. Inspired by awesome- ...

  6. Android开发中遇到的问题(一)——Android模拟器端口被占用问题的解决办法

    一.问题描述 今天在Eclipse中运行Android项目时遇到"The connection to adb is down, and a severe error has occured& ...

  7. 【转】Intellij IDEA调试功能

    http://www.cnblogs.com/winner-0715/p/5422952.html 先编译好要调试的程序.1.设置断点

  8. centos7 apache设置伪静态 开启rewrite_module

    设置伪静态除了要生成.htaccess文件外,还需要查看服务器是否开启了rewrite_module.经过一番的纠结,处理方法如下: 编辑Apache配置文件 nano /etc/httpd/conf ...

  9. BTrace使用小结

    简介 BTrace是一个安全的JVM动态追踪工具,最初为原Sun公司Kenai项目下面的一个子项目. 典型的使用场景是,“我要查个问题,可那个方法没有打印入口参数和返回结果日志”,“我想看某个方法的执 ...

  10. Markdown 语法手册 - 完整版(下)

    6. 引用 语法说明: 引用需要在被引用的文本前加上>符号. 代码: > 这是一个有两段文字的引用, > 无意义的占行文字1. > 无意义的占行文字2. > > 无 ...