How to correctly use the new createCaptureSession() in camera2 in Android?

Viewed 1734

The deprecated createCaptureSession() method is used in an old code in the following way:

cameraDevice.createCaptureSession(Arrays.asList(surface), new CameraCaptureSession.StateCallback() {
     @Override
     public void onConfigured(@NonNull CameraCaptureSession session) {
         if (mycameraDevice == null){
             return;
         }
         cameraCaptureSession = session;
         if (cameraDevice == null){
             return;
         }
         captureRequestBuilder.set(CaptureRequest.CONTROL_MODE, CameraMetadata.CONTROL_MODE_AUTO);
         try {
             cameraCaptureSession.setRepeatingRequest(captureRequestBuilder.build(), null, mBackgroundHandler);
         } catch (CameraAccessException e) {
             e.printStackTrace();
         }
     }
     @Override
     public void onConfigureFailed(@NonNull CameraCaptureSession session) {
         Toast.makeText(MainActivity.this, "Configuration Failed! :(", Toast.LENGTH_SHORT).show();
     }
 }, null); 

I also found out this question on StackOverflow, where it is given that we must do something like:

SessionConfiguration sessionConfiguration = new SessionConfiguration(SessionConfiguration.SESSION_REGULAR, Collections.singletonList(outputConfiguration), new HandlerExecutor(mCameraHandler.getLooper()), mCameraSessionListener);
cameraDevice.createCaptureSession(sessionConfiguration);

Firstly, is this the correct way to use, and if yes, then what is outputConfiguration and how to declare it correctly? In the Youtube tutorial, any outputConfiguration was never created!

So what changes am I expected to make to be able to use the code again?

2 Answers

There's no reason you can't keep using the deprecated version of createCaptureSession - it works just as well as it did before. You'll just have to ignore the Android Studio deprecation warnings.

Basically, over time, we kept having to add more overloads of createCaptureSession with more and more variations of parameters and optional parameters, and it was getting excessive.

So we created SessionConfiguration as a configuration object that's more flexible over time (easier to add new parameters to it), set up one more createCaptureSession that accepts that, and deprecated all the prior versions to direct folks to the one we'd add today if we were designing the API again.

If you want to use the newest option, then you can take a look at OutputConfiguration (it just wraps the list of output Surfaces plus some other optional settings) - you can build one with just the surface you put in the Arrays.asList() call in your sample code.

But you can just keep using what you have - we won't actually break the old methods.

Check this example

camera.createCaptureSession(new SessionConfiguration(SessionConfiguration.SESSION_REGULAR, Arrays.asList(new OutputConfiguration(this.surface) ,new OutputConfiguration(mImageReader.getSurface())),new HandlerExecutor(cameraThreadHandler.getLooper()),new CaptureSessionCallback()));
Related