Requesting Camera Permissions
To get the camera permissions, we need to request permission, as shown below.
protected void onResume() {
// To request Camera Permission
if (!CameraPermissionHelper.hasCameraPermission(this)) {
CameraPermissionHelper.requestCameraPermission(this);
}
}

You can also try this code with Online Java Compiler
Run Code
And then, we need to create a session, as shown below, using the Session () method.
//To create a session
sharedSession = new Session(this, EnumSet.of(Session.Feature.SHARED_CAMERA))
// To store the shared camera reference.
sharedCamera = sharedSession.getSharedCamera();
// To store the ID of the camera
cameraId = sharedSession.getCameraConfig().getCameraId();

You can also try this code with Online Java Compiler
Run Code
We can also resume our recently created session using the resumeARCore() method.
Opening the Camera using created Session
The ARCore provides an ARCore-wrapped API, This API contains a method called openCamera() method that
using this API callback, we can open the camera as follows.
CameraDevice.StateCallback wrappedCallback =
sharedCamera.createARDeviceStateCallback(cameraDeviceCallback, backgroundHandler);
// Storing a reference to the camera service.
cameraManager = (CameraManager) this.getSystemService(Context.CAMERA_SERVICE);
// Opening the camera device
cameraManager.openCamera(cameraId, wrappedCallback, backgroundHandler);

You can also try this code with Online Java Compiler
Run Code
To create a new capture session we can do as follows
public void onOpened(@NonNull CameraDevice cameraDevice) {
Log.d(TAG, "Camera device ID " + cameraDevice.getId() + " opened.");
SharedCameraActivity.this.cameraDevice = cameraDevice;
createCameraPreviewSession();
}

You can also try this code with Online Java Compiler
Run Code
void createCameraPreviewSession() {
try {
// Create an ARCore-compatible capture request using `TEMPLATE_RECORD`.
previewCaptureRequestBuilder =
cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_RECORD);
// Build a list of surfaces, starting with ARCore provided surfaces.
List<Surface> surfaceList = sharedCamera.getArCoreSurfaces();
// (Optional) Add a CPU image reader surface.
surfaceList.add(cpuImageReader.getSurface());
// Add ARCore surfaces and CPU image surface targets.
for (Surface surface : surfaceList) {
previewCaptureRequestBuilder.addTarget(surface);
}
// Wrap our callback in a shared camera callback.
CameraCaptureSession.StateCallback wrappedCallback =
sharedCamera.createARSessionStateCallback(cameraSessionStateCallback, backgroundHandler);
// Create a camera capture session for camera preview using an ARCore wrapped callback.
cameraDevice.createCaptureSession(surfaceList, wrappedCallback, backgroundHandler);
} catch (CameraAccessException e) {
Log.e(TAG, "CameraAccessException", e);
}
}

You can also try this code with Online Java Compiler
Run Code
And along the way, we have a lot to explore about ARCore functionalities using this link.
Frequently Asked Questions
-
How do I implement shared camera access using ARCore?
ARCore is a google platform that provides all the useful methods that are used for accessing camera permissions, how to open a camera, how to create a camera session, how to use this camera session to capture things etc. We can implement shared camera access using sessions.
-
What permission do I need to access to use the device’s camera?
The ARCore samples include CameraPermissionHelper, which provides some utilities to provide camera permissions details such as CameraId, Camera device, etc.
-
How to open a camera using java code?
After creating a session, we have to use this session to open the camera. We also need a camera id of a particular camera device. We can use a cameraManager.openCamera() method to open a camera.
-
How to capture using a shared camera using ARCore?
To capture something using a shared camera, we need to use an ARCore-wrapped callback that contains all suitable methods related to capturing using a camera device.
Conclusion
In this article, we have extensively discussed how to get camera permissions, how to open a camera, how to close it, how to create a camera capture using previously created sessions, etc. Please refer to this link for official documentation for more information. You can explore more through their official documentation.
Hey Ninjas! You can check and explore more unique courses on machine learning concepts through our official website, Coding Ninjas, and checkout Coding Ninjas Studio to learn through articles and other important stuff to your growth.
Happy Learning!