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");
}
New contributor
Mayaank Pillai is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.