Android Camera2 api多个表面ConfigureFailed

时间:2016-12-18 22:30:30

标签: android-camera2

我正在尝试使用camera2 API开发Android相机应用。代码工作正常。然而,在nexus 5X上测试之后,如果添加多个Surface并且下面调用的onConfigureFailed方法是使用

的代码,则createCaptureSession总是会失败

任何想法为什么这只发生在nexus 5X

protected void startPreview() {
  if (null == mCameraDevice) {
    // Log.d(TAG, "mCameraDevice is null");
    return;
  }
  //close Preview Session
  if (mCaptureSession != null) {
    mCaptureSession.close();
    mCaptureSession = null;
  }

  try {
    SurfaceTexture texture = mTextureView.getSurfaceTexture();
    assert texture != null;
    texture.setDefaultBufferSize(mPreviewSize.getWidth(), mPreviewSize.getHeight());
    Surface previewSurface = new Surface(texture);
    mPreviewBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
    mPreviewBuilder.addTarget(previewSurface);

    //mCameraDevice.createCaptureSession(Arrays.asList(previewSurface, mImageReader), new CameraCaptureSession.StateCallback(){        this line fails
    mCameraDevice.createCaptureSession(Arrays.asList(previewSurface), new CameraCaptureSession.StateCallback(){ // this works
      @Override
      public void onConfigured(CameraCaptureSession cameraCaptureSession) {

        mCaptureSession = cameraCaptureSession;

        mPreviewBuilder.set(CaptureRequest.CONTROL_AF_MODE,CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
        updatePreview();
      }

      @Override
      public void onConfigureFailed(CameraCaptureSession cameraCaptureSession) {
        Toast.makeText(this, "onConfigureFailed", Toast.LENGTH_SHORT).show();
      }
    }, null);

  } catch (Exception e) {
    Log.e(TAG, "startPreview faild",e);
    e.printStackTrace();
  }
}

2 个答案:

答案 0 :(得分:1)

调低分辨率似乎已经解决了这个问题。我在运行Android 7.0的三星S7(SM-G930V)上体验过它。我选择的分辨率为2048x1152或宽度或高度的输出大小约为2048.

答案 1 :(得分:0)

尝试在createCaptureSession()之前添加一些延迟。

protected void startPreview() {
    if (null == mCameraDevice) {
        // Log.d(TAG, "mCameraDevice is null");
        return;
    }
    //close Preview Session
    if (mCaptureSession != null) {
        mCaptureSession.close();
        mCaptureSession = null;
    }

    try {
        SurfaceTexture texture = mTextureView.getSurfaceTexture();
        assert texture != null;
        texture.setDefaultBufferSize(mPreviewSize.getWidth(), mPreviewSize.getHeight());
        Surface previewSurface = new Surface(texture);
        mPreviewBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
        mPreviewBuilder.addTarget(previewSurface);

        //GIVE SOME DELAY BEFORE createCaptureSession()
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                //mCameraDevice.createCaptureSession(Arrays.asList(previewSurface, mImageReader), new CameraCaptureSession.StateCallback(){        this line fails
                mCameraDevice.createCaptureSession(Arrays.asList(previewSurface), new CameraCaptureSession.StateCallback(){ // this works
                    @Override
                    public void onConfigured(CameraCaptureSession cameraCaptureSession) {

                        mCaptureSession = cameraCaptureSession;

                        mPreviewBuilder.set(CaptureRequest.CONTROL_AF_MODE,CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
                        updatePreview();
                    }

                    @Override
                    public void onConfigureFailed(CameraCaptureSession cameraCaptureSession) {
                        Toast.makeText(this, "onConfigureFailed", Toast.LENGTH_SHORT).show();
                    }
                }, null);
            }
        }, 100);

    } catch (Exception e) {
        Log.e(TAG, "startPreview faild",e);
        e.printStackTrace();
    }
}