无法连接到摄像头服务/摄像头服务器死机

时间:2014-12-29 15:20:28

标签: android exception camera camera-api

我需要一些帮助。我们创建了一个仅使用前置摄像头的应用程序。但是在某些设备上我们遇到了问题。因此,在某些设备上,Camera.open()方法会抛出异常:

Failed to connect to camera service. 

日志因设备而异,但是是2条消息之一:

Camera W 21325 Camera server died!

CameraBase W 18851 An error occurred while connecting to camera: 1

在其他设备上它工作正常。 这是我用来访问相机的代码 -

public Camera getFrontFacingCamera() {
    Camera object = null;
    try {
        object = Camera.open(findFrontFacingCamera());
    } catch (Exception e) {
        Mint.logException(e);
    }
    return object;
}

private static int findFrontFacingCamera() {
    int cameraId = -1;
    @SuppressWarnings("deprecation")
    int numberOfCameras = Camera.getNumberOfCameras();

    for (int i = 0; i < numberOfCameras; i++) {
        CameraInfo info = new CameraInfo();
        Camera.getCameraInfo(i, info);
        if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
            cameraId = i;
            break;
        }
    }
    return cameraId;
}

private void onCreate() {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
cameraObject = getFrontFacingCamera();
showCamera = new ShowCamera(this, cameraObject);
preview = (FrameLayout) findViewById(R.id.camera_preview);
preview.addView(showCamera);
}

@Override
protected void onPause() {
    super.onPause();
    if (cameraObject != null) {
        cameraObject.release();
        cameraObject = null;
        preview.removeView(showCamera);
        showCamera = null;
    }

ShowCamera.java

public class ShowCamera extends SurfaceView implements SurfaceHolder.Callback {

private SurfaceHolder holdMe;
private Camera mCamera;
Context context;

public ShowCamera(Context context,Camera camera) {
  super(context);
  this.context=context;
  mCamera = camera;
  holdMe = getHolder();
  holdMe.addCallback(this);
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
  try   {
     mCamera.setPreviewDisplay(holder);
     mCamera.startPreview();
  } catch (IOException e) {
  }
}

@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
   this.getHolder().removeCallback(this);
   mCamera.release();
}
}

清单:

<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera.front" android:required="true"/>
<uses-feature android:name="android.hardware.camera.autofocus" android:required="false"/>

有谁知道如何纠正这个问题?

0 个答案:

没有答案