Android Camera2 api multiple surfaces ConfigureFailed

1.6k views Asked by At

i'm trying to develop an android camera app using camera2 API. the code works fine. however after testing it on nexus 5X the createCaptureSession always fail if i add multiple Surface and the onConfigureFailed method get called below is the code i'm using

any idea why this is happening only with 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

There are 2 answers

0
pecific_rim On

Try adding some delay before 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();
    }
}
0
Aace On

Toning down the resolution seems to have solved this problem. I experienced it on a Samsung S7 (SM-G930V) running Android 7.0. My resolution of choice was 2048x1152 or output size around 2048 for width or height.