学习Camera开发时遇到的问题

总结:


1、用模拟器启动不了camera,提示:java.lang.runtimeException:setparameters failed.....
因为parameters.setPictureSize(320, 480)(设置分辨率)的参数有误

这个可以看logcat中

ERROR/QualcommCameraHardware(93): picture_size_values=640x480,320x240,176x144,160x120 找这句在这个当中选择
如果不清楚分辨率可以却掉这句话,再运行就OK 了。

2、连续两次以上的拍照,会出现异常:java.lang.RuntimeException:fail to connect to camera service。
因为没有对camera进行释放,或者说系统有自动释放功能但是还没有被处理。可以通过手动释放:
/**
* 停止预览
*/
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
mCamera.stopPreview();

//手动释放 一定得加!
mCamera.release();

mCamera=null;

}

还有记得权限一定要加上

-----------------------------------------------------------------------------------------------------------------

camera 打开之后就出现了 java.lang.RuntimeException: Fail to connect to camera service
由于版本原因引起的,在2.1系统的是不是正常的,然后再试试2.2,2.3的!
根据2.2.和 2.3 构造函数的源码

在建项目时要建2.3以上的,如已经建好但是在window-preferences中改版本也是没用的,这个是我出现的问题

记住:camera.open(0) 是后置摄像头 camera.open(1) 是前置摄像头 这是我试验出来的

2.3版本
Camera(int cameraId) {
mShutterCallback = null;
mRawImageCallback = null;
mJpegCallback = null;
mPreviewCallback = null;
mPostviewCallback = null;
mZoomListener = null;

Looper looper;
if ((looper = Looper.myLooper()) != null) {
mEventHandler = new EventHandler(this, looper);
} else if ((looper = Looper.getMainLooper()) != null) {
mEventHandler = new EventHandler(this, looper);
} else {
mEventHandler = null;
}

native_setup(new WeakReference<Camera>(this), cameraId);
}

2.2版本

Camera() {
mShutterCallback = null;
mRawImageCallback = null;
mJpegCallback = null;
mPreviewCallback = null;
mPostviewCallback = null;
mZoomCallback = null;

Looper looper;
if ((looper = Looper.myLooper()) != null) {
mEventHandler = new EventHandler(this, looper);
} else if ((looper = Looper.getMainLooper()) != null) {
mEventHandler = new EventHandler(this, looper);
} else {
mEventHandler = null;
}

native_setup(new WeakReference<Camera>(this));
}

2.3 多了一个参数 cameraId

多了一个构造函数 Camera(int cameraId)

希望对大家有所帮助
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值