项目中把摄像头预览抽象了出来,可能很多模块都会用到这个功能
如:教学模块、教练学员登退签、定时拍照、随机拍照都需要这个功能。
设置模块也需要预览摄像头的效果,检查是否可用等等
模块A
mserviceConnection = new AdditionServiceConnection();
Intent it = new Intent();
it.setAction(Constant.WISDON_MODULAR_DATA_ACTION_SERVICE);
context.bindService(it, mserviceConnection, Context.BIND_AUTO_CREATE);
public static final String WISDON_MODULAR_DATA_ACTION_SERVICE = "com.hst.cz.cam.aidl.CamModularService";
模块A、绑定一个服务,指向com.hst.cz.cam.aidl.CamModularService,我们称呼为相机模块,
public class AidlCamModularService extends Service {
ICamModularPush.Stub mBinder = new ICamModularPush.Stub() {
@Override
public byte[] getM(int arg0, int arg1) throws RemoteException {
return null;
}
@Override
public String push(String jsonStr) throws RemoteException {
int id = addJsonStr(jsonStr);
Intent intent = new Intent(context, BackService.class);
intent.putExtra("aidl", 0);
intent.putExtra("json", sendJsonArray[id - 1].toString());
context.startService(intent);
return null;
}
};
}
相机模块启动服务
public class BackService extends Service {
...
case 1: {
if (isExistUsbCamera()) {
cameraArray[i - 1] = Camera.open(0);
return true;
} else {
TTSControl.log("找不到摄像头", context);
}
}
break;
case 2: {
if (isExistCVBSCamera()) {
cameraArray[i - 1] = Camera.open(7);
return true;
}
}
...
/*通过判断驱动文件是否存在,来判断设备*/
public boolean isExistUsbCamera() {
String path = "/dev/video0";
return new File(path).exists();
}