// Camera selector.
final CameraSelector colorCameraSelector = new CameraSelector.Builder().requireLensFacing(CameraSelector.LENS_FACING_FRONT).build();
final CameraSelector infraredCameraSelector = new CameraSelector.Builder().requireLensFacing(CameraSelector.LENS_FACING_BACK).build();
// Preview.
final Preview colorPreview = new Preview.Builder().setTargetResolution(targetResolution).setTargetRotation(targetRotation).build();
// Image analysis.
final ImageAnalysis imageAnalysis = new ImageAnalysis.Builder().setTargetResolution(targetResolution).setTargetRotation(targetRotation).setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST).build();
imageAnalysis.setAnalyzer(executorService, new ImageAnalyzer());
// Must unbind the use-cases before rebinding them.
processCameraProvider.unbindAll();
final Camera colorCamera = processCameraProvider.bindToLifecycle(lifeCycleOwner1, colorCameraSelector, colorPreview);
final Camera infraredCamera = processCameraProvider.bindToLifecycle(lifeCycleOwner2, infraredCameraSelector, imageAnalysis);
// Attach the preview view's surface provider to preview use case.
colorPreview.setSurfaceProvider(colorPreviewView.createSurfaceProvider(colorCamera.getCameraInfo()));
From my tests, only the second call to bindToLifeCycle() will actually stick. Are you able to add support for this feature?
(2) Is it possible to start and stop a Preview or an ImageAnalysis without binding it to a LifecycleOwner? You see, our devices have proximity sensors and when we detect a user's presence, we turn on the cameras and show the preview images, and when the user walks away we turn off the cameras. So we need to be able to start and stop the cameras independent of the normal lifecycle callbacks. Currently, the hack I introduced to make this work is to create a fake class that implements LifecycleOwner and manually call lifecycleRegistry.setCurrentState() to the state I need to make the cameras turn on and off.
(3) When building Preview and ImageAnalysis objects, I've found that it's better for me to use setTargetResolution() instead of setAspectRatio(). The reason why is because when we try to draw an oval around the user's face in a graphic overlay, it's difficult figuring out how to transform the coordinates so that the oval isn't stretched oddly. I've been using this template (https://github.com/googlesamples/android-vision/blob/master/visionSamples/multi-tracker/app/src/main/java/com/google/android/gms/samples/vision/face/multitracker/ui/camera/GraphicOverlay.java) as a basis for our graphic overlay, but I'm not sure how to tweak it when using setAspectRatio() instead of setTargetResolution(). Any help?
Finally, thanks so much for creating this library. It's helping reduce ~1000 lines of code from our Camera2 implementation to ~30 lines with CameraX.