CameraManager

A system services manager for detecting, representing, and connecting to CameraDevices. GetCameraIdList to select the camera to open, getCameraCharacteristics to get fixed camera capabilities, then call openCamera to open the camera, In the CameraDevice. StateCallback# onOpened CameraDevice camera instance, Then call CameraDevice#createCaptureSession and CameraDevice#createCaptureRequest to set up preview capture etc

Query the ability of camera devices

public CameraCharacteristics getCameraCharacteristics (String cameraId)
Copy the code

Query the ability of camera devices. These functions are immutable for a given camera. Starting at API level 29, this function can also be used to query the capabilities of physical cameras that can only be used as part of a logical multiple camera. These cameras can not directly through openCamera (String, CameraDevice StateCallback, Handler) to open. Also starting with API level 29, although most basic CAMERA information is still available even without CAMERA permission, certain values are not available to applications that do not hold that permission. The unavailable keys are listed by CameraCharacteristics#getKeysNeedingPermission.

Query the camera extension capability of the camera device

public CameraExtensionCharacteristics getCameraExtensionCharacteristics (String cameraId)
Copy the code

Query the camera extension capability of the camera device.

Gets the list of camera ids

public String[] getCameraIdList ()
Copy the code

Returns a list of currently connected camera devices by identifier, including cameras that may be in use by other camera API clients. Non-removable cameras use integers starting from 0 as identifiers, while removable cameras have unique identifiers for each individual device, even if they are the same model. This list does not include physical cameras that can only be used as part of a logical multi-camera device.

Get ConcurrentCameraIds

public Set<Set<String>> getConcurrentCameraIds ()
Copy the code

Returns a composite collection of currently connected camera device identifiers, supporting concurrent configuration of camera device sessions. Devices in these combinations can be configured simultaneously by the same client camera application. However, the simultaneous use of these camera devices by two different applications is not guaranteed. For concurrent operations, in chronological order: the application must first close any open camera with a configured session using CameraDevice#close. Before any camera device configuration session, you must use the openCamera (String, CameraDevice StateCallback, Handler) open all at the same time operating camera equipment. Combination of each equipment warranty can query getCameraCharacteristics (CameraCharacteristics. SCALER_MANDATORY_CONCURRENT_STREAM_COMBINATIONS) flow combination of support . For concurrent operation, if the camera equipment is from CameraCharacteristics. CONTROL_ZOOM_RATIO_RANGE specified is not empty scaling range, the full scale range may not be applicable. Applications can use CaptureRequest during concurrent operation. The CONTROL_ZOOM_RATIO > = 1 and < = CameraCharacteristics SCALER_AVAILABLE_MAX_DIGITAL_ZOOM This group can include camera devices that are currently in use by other camera API customers. The concurrent CameraExtensionSession CameraExtensionSession is not currently supported. This combination does not contain physical cameras that can only be used as part of a logical multi-camera device. If the new camera ID becomes available via AvailabilityCallback#onCameraUnavailable(String), the client can call this method to check if a new combination of camera ids that can be streamed simultaneously is available.

isConcurrentSessionConfigurationSupported

public boolean isConcurrentSessionConfigurationSupported (Map<String, SessionConfiguration> cameraIdAndSessionConfig)
Copy the code

Check whether the provided set of camera devices and their corresponding SessionConfiguration can be configured simultaneously. This method performs a runtime check on a given SessionConfiguration and camera ID combination. Results confirm whether a camera capture session can be successfully created simultaneously on a given camera device using CameraDevice#createCaptureSession(SessionConfiguration) using the passed SessionConfiguration. This method can be called at any time before, during, and after an activity capture session. It does not affect normal camera behavior in any way, and must be much faster than creating regular or restricted capture sessions. Although this approach is faster than creating a new capture session, it is not intended to be used to explore the entire space of supported concurrent flow combinations. Available to enforce concurrent flow combination can query getCameraCharacteristics (CameraCharacteristics. SCALER_MANDATORY_CONCURRENT_STREAM_COMBINATIONS) key. Note that session parameters are ignored and there is no need to call SessionConfiguration#setSessionParameters. Need to Manifest. Permission. CAMERA

Opens the connection to the camera with the given ID

public void openCamera (String cameraId, CameraDevice.StateCallback callback, Handler handler)
Copy the code

Opens the connection to the camera with the given ID. Use getCameraIdList() to get a list of available camera devices. Please note that even if is a list of id, if the device in the calling getCameraIdList () and openCamera (String, CameraDevice StateCallback, Handler) between disconnected, Or if a higher-priority camera API client starts using the camera device, open may fail. \

Starting at API level 23, devices that have called the AvailabilityCallback#onCameraUnavailable(String) callback because the device is being used by a lower priority, It is still possible to open the background camera API client by calling this method when the camera API client being called has a higher priority than the current camera API client using the device. In general, if the top-level foreground activity is running in your application process, your process will have the highest priority when accessing the camera, and this will succeed even if the camera device is being used by another camera API client. Any loss of control of the camera in this way the low priority application will receive CameraDevice. StateCallback. OnDisconnected (CameraDevice) callback. In the same application twice to open the same camera ID will also lead CameraDevice. StateCallback. OnDisconnected (CameraDevice) callback is triggered CameraDevice since the first session to open, All ongoing tasks have been deleted. Successfully open the camera, will use the new open CameraDevice call CameraDevice. StateCallback# onOpened. CameraDevice#createCaptureSession and CameraDevice#createCaptureRequest can then be set up to operate the camera device prior to API level 30, When an application tries to open multiple CameraDevices with different ids and the device does not support such combinations, openCamera(String, CameraDevice.StateCallback, Handler) will fail and throw a CameraAccessException or one or more opened CameraDevices will disconnect and receive CameraDevice. StateCallback. OnDisconnected (CameraDevice) callback. Which behavior will occur depends on the device implementation and may vary from device to device. Starting with API level 30, if the device does not support the open camera combination, Ensure openCamera (String, CameraDevice StateCallback, Handler) call will fail and not CameraDevice disconnect any existing. If the camera is disconnected during initialization after this function call returns, The CameraDevice. StateCallback# onDisconnected and is in off state CameraDevice in the disconnected state (and CameraDevice. StateCallback# onOpened will be skipped). If opening the camera device fails, the onError method of the device callback will be called, and subsequent calls on the camera device will throw a CameraAccessException. Need to Manifest. Permission. CAMERA

Opens the connection to the camera with the given ID.

public void openCamera (String cameraId, Executor executor, CameraDevice.StateCallback callback)
Copy the code

Opens the connection to the camera with the given ID. The behavior of this method matches that of openCamera(java.lang.String, StateCallback, Android.os.handler), except that its Executor is used as a parameter instead of a Handler. Need to Manifest. Permission. CAMERA

Register for availability callbacks

public void registerAvailabilityCallback (Executor executor, CameraManager.AvailabilityCallback callback)
Copy the code

Register callbacks to receive notifications about the availability of camera equipment. The behavior of this method with registerAvailabilityCallback (android. Hardware. Camera2. CameraManager. AvailabilityCallback, Android.os.handler), except that it uses Executor as a parameter instead of Handler.

Register for availability callbacks

public void registerAvailabilityCallback (CameraManager.AvailabilityCallback callback, Handler handler)
Copy the code

Register callbacks to receive notifications about the availability of camera equipment. Registering the same callback again will replace the handler with the new handler provided. The first time a callback is registered, it is immediately invoked with the availability status of all the currently known camera devices. AvailabilityCallback#onCameraUnavailable(String) is called whenever any camera API client opens the camera device. Starting with API level 23, other camera API clients may still be able to open such a camera device, ejected if the existing client has a higher priority than the existing client of the camera device. Since this callback is registered with the camera service, remember to unregister once it is no longer necessary; Otherwise the callback will continue to receive events indefinitely and may prevent other resources from being released. Specifically, callbacks will be invoked independently of the general activity life cycle and the state of the individual CameraManager instances. \

registerTorchCallback

public void registerTorchCallback (Executor executor, CameraManager.TorchCallback callback)
Copy the code

Register a callback to receive notifications about the status of torch mode. The behavior of this method with registerTorchCallback (android. Hardware. Camera2. CameraManager. TorchCallback, android. The OS. The Handler) the behavior of the match, The difference is that its Executor is used as a parameter instead of a Handler.

Register the torch callback

public void registerTorchCallback (CameraManager.TorchCallback callback, Handler handler)
Copy the code

Register a callback to receive notifications about the status of torch mode. Registering the same callback again will replace the handler with the new handler provided. The first time a callback is registered, it is immediately called with the flashlight mode state of all currently known camera devices with flash. Since this callback is registered with the camera service, remember to unregister once it is no longer necessary; Otherwise the callback will continue to receive events indefinitely and may prevent other resources from being released. Specifically, callbacks will be invoked independently of the general activity life cycle and the state of the individual CameraManager instances.

Setting torch Mode

public void setTorchMode (String cameraId, boolean enabled)
Copy the code

Sets the flashlight mode for the flash component of the camera with the given ID without turning on the camera device. Use getCameraIdList() to get a list of available camera devices, and use getCameraCharacteristics(String) to check whether the camera device has a flash unit. Please note that even if the camera device has a flash unit, turning on flashlight mode may fail if you are using the camera equipment or other camera resources required to turn on flashlight mode. If the call setTorchMode (String, Boolean) successful open or close the flashlight mode, CameraManager. TorchCallback# onTorchModeChanged will be invoked. However, even if flashlight mode is successfully turned on, the application does not have exclusive ownership of the flash or camera equipment. When the camera flash unit belong to equipment is not available, or other resources cannot be used to keep the flashlight open camera with (CameraManager. TorchCallback# onTorchModeUnavailable is called), a flashlight mode will be closed and become unusable. In addition, other applications can free call setTorchMode (String, Boolean) shut down the torch mode (CameraManager. TorchCallback# onTorchModeChanged is called). Flashlight mode will be turned off if the application that recently turned flashlight mode on exits.

Cancel the registration availability callback

public void unregisterAvailabilityCallback (CameraManager.AvailabilityCallback callback)
Copy the code

Delete previously added callbacks; The callback will no longer receive connect and disconnect callbacks. Deleting an unregistered callback is invalid.

Unregister TorchCallback

public void unregisterTorchCallback (CameraManager.TorchCallback callback)
Copy the code

Delete previously added callbacks; Callbacks will no longer receive torch-mode state callbacks. Deleting an unregistered callback is invalid.

CameraManager.AvailabilityCallback

Callback for camera device available or not open. A camera becomes available when it is no longer in use or when a new removable camera is connected. When certain applications or services start using the camera, or when the removable camera is disconnected, they become unavailable. Extend this callback and pass a subclass instance, CameraManager# registerAvailabilityCallback availability to receive such notification of changes.

onCameraAccessPrioritiesChanged

public void onCameraAccessPrioritiesChanged (a)
Copy the code

Called whenever the camera access priority changes. Notify the camera that the access priority has changed and the camera can now be turned on. An application that was previously denied camera access because a higher priority user was already using the camera, or was disconnected from the active camera session because a higher priority user tried to open the camera, should try to open the camera again if it still wants to use it. Note that multiple applications may receive this callback at the same time, and only one of them will actually successfully open the camera, depending on the exact access priority and timing. This approach is useful if multiple applications may be in a recovery state at the same time, and the user switches focus between them, or the application currently using the camera moves between full screen and Picture in Picture (PiP). In this case,

Available on camera

public void onCameraAvailable (String cameraId)
Copy the code

The new camera is ready for use.

Camera unavailable

public void onCameraUnavailable (String cameraId)
Copy the code

Previously available cameras are no longer available. If the application has an active CameraDevice instance for the now disconnected camera, the application will receive a DisConnection Error.

Works on physical cameras

public void onPhysicalCameraAvailable (String cameraId, String physicalCameraId)
Copy the code

Physical cameras are available again. By default, the logic of multiple cameras all the physical camera are available, so onPhysicalCameraAvailable (String, String) in the call logic when multiple cameras onCameraAvailable (String), Any physical cameras that do not call logical multiple cameras. But if some specific physical camera is not available at the beginning, onPhysicalCameraUnavailable (String, String) can be in after call onCameraAvailable (String).

onPhysicalCameraUnavailable

public void onPhysicalCameraUnavailable (String cameraId, String physicalCameraId)
Copy the code

Previously available physical cameras are no longer available. By default, the logic of multiple cameras all the physical camera are available, so onPhysicalCameraAvailable (String, String) in the call logic when multiple cameras onCameraAvailable (String), Any physical cameras that do not call logical multiple cameras. If some specific physical camera is not available, onPhysicalCameraUnavailable (String, String) can be in after call onCameraAvailable (String).

CameraManager.TorchCallback

Camera flash Flashlight mode becomes unavailable, disabled or enabled by callback. \

Flashlight mode becomes unavailable when the camera device it belongs to becomes unavailable or other camera resources it needs become busy due to other higher priority camera activity. It is disabled when flashlight mode is turned off or the camera device it belongs to is no longer in use and other camera resources it needs are no longer busy. When the application calls the CameraManager. SetTorchMode (String, Boolean) closed camera flashlight mode, or if we do not support while maintaining multiple flashlight mode, when the application is open another camera flashlight mode, the camera flashlight mode will be closed. Flashlight mode in through the open enabled when CameraManager. SetTorchMode (String, Boolean). Flashlight mode CameraManager. SetTorchMode (String, Boolean) only when disabled or enabled by setting. Extend this callback and pass an instance of the subclass, CameraManager#registerTorchCallback, to receive notification of such a state change. \

onTorchModeChanged

public void onTorchModeChanged (String cameraId, boolean enabled)
Copy the code

The flashlight of the camera model has enabled or disabled, can through the CameraManager. SetTorchMode (String, Boolean) changes.

OnTorchMode unavailable

public void onTorchModeUnavailable (String cameraId)
Copy the code

Through CameraManager. SetTorchMode (String, Boolean) set up the flashlight of the camera model. If by calling the open before the torch pattern CameraManager. SetTorchMode (String, Boolean), it will be in CameraManager. TorchCallback# onTorchModeUnavailable call before shut down. CameraManager. SetTorchMode (String, Boolean) will fail, until the flashlight mode again into the disabled or enabled.