Table of contents
1.
Introduction
2.
Building and Running Shared Camera Sample App
3.
Requesting Camera Permissions
4.
Opening the Camera using created Session
5.
Frequently Asked Questions
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Shared Camera Access using ARCore

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Google is one of the most famous research-orientated companies which mainly focuses on customer problems. In this current era of machine learning and IoT, google has become a very good competitor for all the others. Recently in 2017, Google introduced an ARkit and ARCore, which mainly deals with Augmented Reality and IoT stuff. In the process of building AR sessions, some mini processes require Camera stuff. Thus they also developed a set of useful Camera functions like doing the tasks such as opening a camera, closing a camera, reading data from a camera, etc. We will use this ARCore developed by Google to learn how to get camera permissions.

Building and Running Shared Camera Sample App

To start off, we will follow the steps below.

source

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

  1. 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.
  2. 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.
  3. 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.
  4. 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!

Live masterclass