SharedCamera

public class SharedCamera

The SharedCamera class allows the app to share control of the camera with ARCore and obtain image readers and surfaces for rendering. Images can be read concurrently while ARCore is running or while ARCore is paused.

Use this class to obtain wrappers for the CameraDevice and CameraCaptureSession state callbacks, so that camera events and data objects can be shared with ARCore.

When a session is created for shared camera access, ARCore's behavior changes in the following ways:

  • While ARCore is active, the app can use Camera2 APIs, except it must not call CameraCaptureSession#setRepeatingRequest as this will interfere with ARCore operation. The Camera2 APIs can be used directly without restriction while ARCore is paused.
  • Session.getSharedCamera() can be used to obtain an instance of the SharedCamera object.
  • Although ARCore will continue to track normally, it will not be able to use the depth sensor even if one is present on the device.

Example of app activity using shared camera:

Session sharedSession;
 SharedCamera sharedCamera;
 void onCreate(Bundle savedInstanceState) {
   sharedSession = new Session(context, EnumSet.of(Session.Feature.SHARED_CAMERA));
   openCameraForSharing();
   createCameraCaptureSession();
   // ...
 }
 private void openCameraForSharing() {
   sharedCamera = sharedSession.getSharedCamera();
   // Use callback wrapper.
   CameraManager.openCamera(
       cameraID,
       sharedCamera.createARDeviceStateCallback(appDeviceStateCallback, appHandler),
       appHandler);
 }
 private void createCameraCaptureSession() {
   // Get list of ARCore created surfaces. Required for ARCore tracking.
   ArrayList<Surface> surfaceList = sharedCamera.getSurfaces();
   // (Optional) Add a custom CPU image reader surface on devices that support CPU image access.
   ImageReader cpuImageReader = ImageReader.newInstance(…);
   surfaceList.add(cpuImageReader.getSurface());
   // Use callback wrapper.
   cameraDevice.createCaptureSession(
       surfaceList,
       sharedCamera.createARSessionStateCallback(
           appSessionStateCallback, appHandler),
       appHandler);
 }

Public Methods

CameraDevice.StateCallback
createARDeviceStateCallback(CameraDevice.StateCallback appCallback, Handler appHandler)
The openCamera command on the CameraManager should use this wrapped callback as input.
CameraCaptureSession.StateCallback
createARSessionStateCallback(CameraCaptureSession.StateCallback appCallback, Handler appHandler)
Wraps a CameraCaptureSession.StateCallback to allow ARCore to control the camera.
List<Surface>
getArCoreSurfaces()
Gets access to the list of surfaces created and used by ARCore.
SurfaceTexture
getSurfaceTexture()
Gets access to the SurfaceTexture provided by ARCore.
void
setAppSurfaces(String cameraId, List<Surface> surfaces)
Set app created surfaces, to receive additional images when ARCore is active.
void
setCaptureCallback(CameraCaptureSession.CaptureCallback appCaptureCallback, Handler appHandler)
Set CameraCaptureSession.CaptureCallback by sending it to ARCore in order to provide metadata information to the client.

Inherited Methods

Public Methods