Android相机开发中遇到的一些问题

如果说Android适配的话,Android相机方面的适配是最让人头疼的问题了。如果你的项目中有关于自定义相机方面的模块,可以说你多多少少会碰到这些异常:(1)setParamters failed(2)Camera Error 100(3)startPreView() failed(4)getParamters failed(5)拍出的照片方向倒转了(6)拍出的照片左右反即镜面效果。在这只列出

目前所能记忆起的一些常见异常,后期如果有后续发现 ,此篇博文会继续更新,主要收集相机模块的常见异常与解决方法。

首先说说为什么相机这块的适配很麻烦,因为Android的机型过多,而且很多厂商在Rom的时候对相机这块有修改。记得第一次遇到相机的适配问题是集成二维码扫描的功能,在魅族X4的手机上,直接相机不能预览也就是不能捕获数据,后来是把预览帧率的代码给注释了,然后就正常了。

1、关于Camera Error 100的错误:

    /**
     * Media server died. In this case, the application must release the
     * Camera object and instantiate a new one.
     * @see Camera.ErrorCallback
     */
    public static final int CAMERA_ERROR_SERVER_DIED = 100;
以上是Android SDK 对于Camera Error 100的描述,大概意思我相信大家都能看懂,就是要release并且重新初始化一个Camera对象,然而我在采用此种方法的时候,并不能很好的解决问题。关于此种错误我也Google了好久也在stackoverflow上看了很多帖子,每个人解决该问题的方式不一样,确切的说是针对不同的机型出现Camera Error 100的原因不同。

下面我列举几个例子以及解决方式:

(1)机型:HUAWEI GRA-CL00。

原因:设置了错误的VideoSize,该手机的getSupportedVideoSizes()方法返回为null。

解决方法:注释掉MediaRecorder的setVideoSize()方法。

(2)机型:Lenovo S810t

原因:设置的VideoEncoder不支持。

解决方法:采用默认的值,即.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);

(3)机型:HTC Z715e和AMOI N820

原因:4.0.x一个已知的问题,一开始录像会报:freeAllBuffersExceptCurrentLocked called but mQueue is not empty。这个暂时没有解决掉,但是大家可以看下下面一句话:

This is a known problem with Android 4.0.x and it doesn't look like there is an easy fix for it.

综上所述:关于Camera Error 100的问题,不同的手机可能是不同的原因导致的,目前就我个人来说没有好的方案,和网上的其他做Camera应用的网友也交流过,对于此问题也没有好的方案目前,如果知道此问题的解决方案的人看到此文,还麻烦告诉我一下,哈哈。目前,我对于Camera Error 100的错误的解决流程是:先看看视频码率与视频尺寸的大小是否设置有问题,然后看看视频的编码格式等等,其实就是根据经验靠猜测。

这里给出几个关于Camera Error 100错误的帖子:http://stackoverflow.com/questions/6837975/camera-error-100

http://stackoverflow.com/questions/8647628/android-camera-server-died-and-camera-error-100

http://stackoverflow.com/questions/2734886/droids-mediaserver-dies-on-camera-takepicture

2、关于Camera的onPictureTaken()方法有时不执行的问题

在维护相机的应用的过程中,发现有时Camera的onPictureTaken方法不会执行,关于此问题的解决方法API中已经给出很好的解决方案了,如下:

* <p>This method is only valid when preview is active (after
     * {@link #startPreview()}).  Preview will be stopped after the image is
     * taken; callers must call {@link #startPreview()} again if they want to
     * re-start preview or take more pictures. This should not be called between
     * {@link android.media.MediaRecorder#start()} and
     * {@link android.media.MediaRecorder#stop()}.
     *
     * <p>After calling this method, you must not call {@link #startPreview()}
     * or take another picture until the JPEG callback has returned.
意思就是在调用takePicture方法之前,一定确保已经调用过startPreview方法了。

3、关于拍照的快门声的处理

对于拍照的快门声我曾经走入过误区,如果你看了源码你会发现CameraInfo类有一个属性,如下:

  /**
         * <p>Whether the shutter sound can be disabled.</p>
         *
         * <p>On some devices, the camera shutter sound cannot be turned off
         * through {@link #enableShutterSound enableShutterSound}. This field
         * can be used to determine whether a call to disable the shutter sound
         * will succeed.</p>
         *
         * <p>If this field is set to true, then a call of
         * {@code enableShutterSound(false)} will be successful. If set to
         * false, then that call will fail, and the shutter sound will be played
         * when {@link Camera#takePicture takePicture} is called.</p>
         */
        public boolean canDisableShutterSound;


该属性主要用来判断该设备是否支持关闭快门声,一般我们都会根据此属性来判断该设备是否可以关闭快门声,如果不可以关闭,我们会提示用户该设备暂不支持关闭快门声。然而我在实践中发现,对于关闭快门声我们可以直接在调用takePicture方法时,给shutterCallback参数传入Null就可以实现关闭快门声,你会发现即使
canDisableShutterSound属性的值为false我们也成功的关闭了关门声。

4、关于startPreview、setParameters、getParameters的异常,因为这几个方法会因为种种原因有时crash,所以建议使用这些方法时加上try catch进行捕获相应的异常进而做相应的处理。

Android调用camera错误setParameters failed深层解析

5、关于MediaRecorder的stop方法停止的异常,有时是因为去stop的时候MediaRecorder此时的状态是不允许stop,这时我们可以采用如下方法来进行停止:

</pre><pre code_snippet_id="1665780" snippet_file_name="blog_20160428_7_6131756" name="code" class="java">	if (mMediaRecorder != null) {
			

				try {
					mMediaRecorder.setOnErrorListener(null);
					mMediaRecorder.setOnInfoListener(null);
					// 停止
					mMediaRecorder.stop();

				} catch (RuntimeException e) {
					e.printStackTrace();

					// 如果发生异常,很可能是在不合适的状态执行了stop操作
					// 所以等待一会儿
					try {
						Thread.sleep(100);
						<span style="font-family: Arial, Helvetica, sans-serif;">mMediaRecorder.stop();</span>

					} catch (Exception e1) {
						e1.printStackTrace();
					}
				}

				// 再次尝试停止MediaRecorder
//				try {
//					mMediaRecorder.stop();
//				} catch (Exception e) {

//				}

				// 等待,让停止彻底执行完毕
				try {
					Thread.sleep(100);
				} catch (InterruptedException e1) {

				}
				mMediaRecorder.release();
				mMediaRecorder = null;
			}

 

暂时就总结这么多,如果后续有Camera相关的东西要总结,我会更新此文。文中如果有错误之处,还请知道的指正一下。

转载请注明出处:http://blog.csdn.net/gc_gongchao/article/details/51079773


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值