I am working towards a camera app that can record video from the two back cameras on a phone. I have an OpenCamera function that is able to open the main back camera, but fails to open the ultrawide camera. Any insight into how to work with the multicamera API would be greatly appreciated
The below function opens the first camera, but fails to open the second. The ultraWideCallback reaches the error condition.
private void openCamera() {
CameraManager manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
CameraManager ultraWideManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
Log.e(TAG, "is camera open");
try {
cameraId = manager.getCameraIdList()[0];
ultraWideID = ultraWideManager.getCameraIdList()[2];
CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId);
StreamConfigurationMap map = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
assert map != null;
imageDimension = map.getOutputSizes(SurfaceTexture.class)[0];
// Add permission for camera and let user grant the permission
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_CAMERA_PERMISSION);
return;
}
// This call works
manager.openCamera(cameraId, stateCallback, null);
// The below code does not open the second camera
CameraCharacteristics ultraWideCharacteristics = manager.getCameraCharacteristics(ultraWideID);
StreamConfigurationMap ultraWideMap = ultraWideCharacteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
assert ultraWideMap != null;
ultraWideManager.openCamera(ultraWideID, ultraWideCallback, null);
} catch (CameraAccessException e) {
e.printStackTrace(); // No exception occurs
}
Log.e(TAG, "openCamera");
}
You've hardcoded the ultrawide camera as the third camera in the ID list; this may be true on some devices, but certainly not at all universal.
In addition, most mobile devices do not support opening multiple top-level cameras at the same time, because the camera processing pipeline hardware is actually shared between sensors. Some do support concurrent camera use (usually front+back), but that has some strict limits on maximum resolutions that can be used, etc.
That said, more comprehensive access to multi-sensor clusters that are common on higher-end devices is something the Android camera APIs have been adding support for, via the LOGICAL_MULTI_CAMERA capability. Devices that support this path will have a single logical camera that's composed of multiple physical sub-cameras (such as wide, ultra-wide, and tele cameras), and you can configure output streams from the physical sub-cameras directly.
Subject to the limits of the hardware, still, but it's fairly likely you can stream both the wide and ultra-wide at the same time. There's a lot of docs in that link above that describe how to use the API and how to query whether a given combination of outputs is actually supported.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With