Other versions of this page
Devices
Once you allocate a camera manager, you can start to query and interact with the device sensors,
represented by the io.deepmedia.camera.CameraDevice
CameraDevice
class.
Selecting Devices
You can access a list of all devices using CameraManager.devices
. If needed, you can select them by inspecting their properties:
kotlinval manager: CameraManager = ...
val frontCamera: CameraDevice? = manager.devices.firstOrNull {
it.direction == CameraDevice.Direction.Front
}
swiftlet manager: CameraManager = ...
let frontCamera: CameraDevice? = manager.devices.first {
$0.direction == .front
}
Some of the device properties you may consider are:
id: String
: a unique identifier for this devicedirection: CameraDevice.Direction
: either front, back or externalchildren: [CameraDevice]
: the underlying devices for logical cameras made of multiple physical sensors under the hoodcapabilities: CameraCapabilities
: a precise description of what this device can do
As you go through the guide, you'll learn about more properties that can be used for selection.
Managing Devices
Open
For a device to be used in any way it must be opened first, and that's done via the openDevice
function:
kotlinsuspend fun openDevice(
id: String? = null,
direction: CameraDevice.Direction? = null
): CameraDevice = ...
swiftfunc openDevice(
id: String? = nil,
direction: CameraDevice.Direction? = nil
) async throws -> CameraDevice { ... }
Opening a camera means making it active and ready to be used for the use-cases you declared during initialization. For example:
- If the preview feature was selected, the camera will stream into any preview surface that you define
- If the picture feature was selected, the camera gets ready to receive the
takePicture
command - If the video feature was selected, the camera gets ready to interact with the recorder
- If the barcode feature was selected, the camera will start looking for barcodes in the scene
In general, it makes the camera ready to do what you intended to do.
Close
Once you're done with a camera, you can either open another one, or close all of them.
kotlinsuspend fun closeDevices(): Unit = ...
swiftfunc closeDevices() async throws { ... }
Tip: use
CameraManager.flipDevice()
for the manager to check what camera is currently open, and open one that has the oppositedirection
.
Observing Devices
Current Device
Only one device can be opened at any given time. You can retrieve and observe changes with the CameraManager.currentDevice
property:
kotlinval current: CameraDevice? = manager.currentDevice.value
val flow: StateFlow<CameraDevice?> = manager.currentDevice
swiftlet current: CameraDevice? = manager.currentDevice
let publisher: AnyPublisher<CameraDevice?, Never> = manager.currentDevicePublisher
Device Status
At any given time, a single device has a status represented by the CameraDevice.Status
enum:
kotlinval current: CameraDevice.Status = device.status.value
val flow: StateFlow<CameraDevice.Status> = device.status
swiftlet current: CameraDevice.Status = device.status
let publisher: AnyPublisher<CameraDevice.Status, Never> = device.statusPublisher
Check the table below to understand the meaning of each status enum case:
Status | Meaning |
---|---|
CameraDevice.Status.Closed .closed | Device is in its closed, idle state. |
CameraDevice.Status.Open .open | Device was opened by the manager and is currently active, connected and ready to receive commands. |
CameraDevice.Status.Streaming .streaming | Like the open state, but on top, the device is currently streaming image data in real-time (for preview, video recording, and/or barcode detection). |
CameraDevice.Status.Unavailable .unavailable | Device has become unavailable, e.g. because camera permissions were not granted by the user, it was disconnected, it's in use by another app, or other hardware-related reasons. |