8

I'm trying to use the Camera2 API to stream camera data to a SurfaceView. I'm following this guide: Camera2 guide

I cannot get past step 5

MainActivity.java::onCreate()

setContentView(R.layout.activity_main);

surfaceView = (SurfaceView)findViewById(R.id.surface);
manager = (CameraManager)getSystemService(Context.CAMERA_SERVICE);

MainActivity.java::onClick()

for (String id : manager.getCameraIdList()) {
    CameraCharacteristics characteristics = manager.getCameraCharacteristics(id);

    Integer direction = characteristics.get(CameraCharacteristics.LENS_FACING);

    if (direction != null && direction == CameraCharacteristics.LENS_FACING_BACK) {
        if (checkCallingOrSelfPermission("android.permission.CAMERA") == PackageManager.PERMISSION_GRANTED)
            manager.openCamera(id, new StateCallback(), null);

        break;
    }
}

MainActivity.java.StateCallback::onOpened(CameraDevice camera)

List<Surface> surfaces = new LinkedList<>();
surfaces.add(surfaceView.getHolder().getSurface());

CaptureRequest.Builder builder = camera.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
builder.addTarget(surfaces.get(0));

camera.createCaptureSession(surfaces, new CameraCaptureSession.StateCallback() {
    @Override
    public void onConfigured(CameraCaptureSession session) {
        Log.i(TAG, "Configured");
    }

    @Override
    public void onConfigureFailed(CameraCaptureSession session) {
        Log.e(TAG, "Configured failed"); // Ends up in this function :(
    }
    }, null);

The program ends up in the onConfigureFailed() function. I don't know what could be the error, and I don't know how to check what is.

My guess would be that I'm missing something in the CaptureRequest, but I have no idea what.

I'm running on a Samsung Galaxy S4.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Arbitur
  • 38,684
  • 22
  • 91
  • 128
  • 1
    Please add logcat output from when you see the failure. The camera service should have more information in logcat about why the session cannot be created. – Eddy Talvala Dec 02 '15 at 15:01
  • hello @Arbitur i am having same issue with s4 , did you find any clue on that ? – Ajay Jun 15 '16 at 06:46

2 Answers2

0

add to onConfigured:

  if (null == cameraDevice) {
        Log.e(TAG, "updatePreview error, return");
        return;
    }
    captureRequestBuilder.set(CaptureRequest.CONTROL_MODE, CameraMetadata.CONTROL_MODE_AUTO);
    try {
        cameraCaptureSessions.setRepeatingRequest(captureRequestBuilder.build(), null, mBackgroundHandler);
    } catch (CameraAccessException e) {
        e.printStackTrace();
    }
0

Override onConfigureFailed() like this:

   @Override
   public void onConfigureFailed(CameraCaptureSession session) {
       ImageReader mReader = ImageReader.newInstance(640, 480, ImageFormat.JPEG, 1);
       takePicture() // function to get image
       createCameraPreview(); // function to set camera Preview on screen
   }

Call createCameraPreview function to restart the camera, otherwise, it will stay stuck. You can change the ImageReader with new values

ImageReader mReader = ImageReader.newInstance(640, 480, ImageFormat.JPEG, 1);

And call the takePicture() function again so that user don't have to click again to capture image.

Livin Mathew
  • 48
  • 1
  • 5
Aravind jayan
  • 101
  • 1
  • 4