Full Multi Camera Support for Android
How can we support multiple cameras that is ultrawide and zoom using the current implementation of Android's GDMPCameraHelper
In case of Windows and Linux hosts since it uses OpenCV by specifying the index of camera we are able to get every camera, how about doing that on Android hosts.
We can have a function similar to this and instead of only FRONT and BACK we will have cameras based on their indexes.
private CameraSelector getCameraSelectorByIndex(int index) {
ListenableFuture<ProcessCameraProvider> cameraProviderFuture = ProcessCameraProvider.getInstance(activity);
try {
ProcessCameraProvider cameraProvider = cameraProviderFuture.get();
List<CameraInfo> availableCameras = new ArrayList<>(cameraProvider.getAvailableCameraInfos());
if (index < 0 || index >= availableCameras.size()) {
throw new IndexOutOfBoundsException("Camera index out of bounds.");
}
CameraInfo selectedCameraInfo = availableCameras.get(index);
return new CameraSelector.Builder().selectCamera(selectedCameraInfo).build();
} catch (InterruptedException | ExecutionException e) {
Log.e(TAG, "Error selecting camera by index: " + e.getMessage());
return null;
}
}
PS - I am not able to get my build for Android to succeed following the instructions in the BUILDING.md.
Python - 3.12
JDK - 11/17/22
AndroidSDK - 34.0.0
AndroidNDK - 21.0.xx...
Hi @DarkMatter-999, I agree that adopting code similar to this could certainly enable access to multiple cameras of a android device instead of only back or front, but I suppose mobile app developers knowing which camera facing they are about to open is important, so we cannot just passing an index without knowing which facing of each cameras.
Some kind of camera enumeration method would be required to make opening camera by index more robust, it is currently not implemented because enumerating webcams on desktop with OpenCV is inefficient and available information is limited, but replacing camera helper OpenCV backend with more platform-specific code for desktop is planned. I think the ideal opportunity to work on camera enumeration support is when initial backend for Linux Pipewire/Windows WMF is there.
As a temporary workaround, I would suggest something like this, but there is no guarantee that default back/front cameras will always be the first two elements returned by getAvailableCameraInfos, as I did not found any related claims on CameraX docs:
...
ListenableFuture<ProcessCameraProvider> cameraProviderFuture = ProcessCameraProvider.getInstance(activity);
ProcessCameraProvider cameraProvider = cameraProviderFuture.get();
List<CameraInfo> availableCameras = new ArrayList<>(cameraProvider.getAvailableCameraInfos());
if (index < 0 || index >= availableCameras.size()) {
throw new IndexOutOfBoundsException("Camera index out of bounds.");
}
CameraSelector selector;
if (index == 0)
selector = CameraSelector.DEFAULT_FRONT_CAMERA;
else if (index == 1)
selector = CameraSelector.DEFAULT_BACK_CAMERA;
else
selector = availableCameras.get(index).getCameraSelector();
...
How do you think about this? Will such changes help you to access multiple cameras on android?
Regarding the building problem for android, please open another issue and describe the error you are facing.
Yeah this one seems better as we can first handle the DEFAULT_FRONT_CAMERA and DEFAULT_BACK_CAMERA as the standard and if other cameras exist then choose them.
and as for the build error part... that was all on me
i used export ANDOROID_HOME=~/Android/Sdk/ instead of export ANDROID_HOME=~/Android/Sdk/
worked fine afterwards. Thanks
Hi, since GDMP v0.5, camera helper has been considered deprecated, and no new features will be developed for it. You might want to consider CameraServer-based methods for accessing camera frames on Godot Engine in future.
I will close the issue as MediaPipeCameraHelper and GDMPCameraHelper for Android's backend is no longer developed, please open the issue on CameraServerExtension if accessing multiple cameras on Android is still a problem for you.