1

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

Failed to connect to camera service. 

日志因设备而异,但它是以下两条消息之一:

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"/>

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

4

0 回答 0