我使用Surface View和Surface Holder打开USB相机。相机打开,但一段时间后,我得到“错误100,相机服务器死了”。我搜索了很多教程,但错误没有解决。如果有人知道这个错误,请帮助我。
我使用下面的代码打开相机。
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
start = (Button)findViewById(R.id.start);
start.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View arg0) {
start_camera();
}
});
surfaceView = (SurfaceView)findViewById(R.id.surfaceView);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
rawCallback = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
Log.d("Log", "onPictureTaken - raw");
}
};
}
private void start_camera()
{
try{
camera = Camera.open(0);
}catch(RuntimeException e){
Log.e(tag, "init_camera: 2=" + e);
return;
}
Camera.Parameters param;
param = camera.getParameters();
//modify parameter
param.setPreviewFrameRate(20);
param.setPreviewSize(640, 480);
camera.setParameters(param);
try {
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
} catch (Exception e) {
return;
}
}
答案 0 :(得分:0)
mCamera = Camera.open(mCameraId);
mCamera.setErrorCallback(new Camera.ErrorCallback() {
@Override
public void onError(int error, Camera camera) {
KLog.e("相机服务出错,错误代码:" + error);
switch (error) {
case Camera.CAMERA_ERROR_SERVER_DIED:
case Camera.CAMERA_ERROR_UNKNOWN:
try {
mCamera.setPreviewCallback(null);
mCamera.setPreviewDisplay(null);
mCamera.stopPreview();
mCamera.release();
mCamera = null;
start();
} catch (IOException e) {
e.printStackTrace();
}
break;
}
}
});