Other versions of this page
Concepts
API Design
Unless otherwise stated, the SDK APIs can be considered thread-safe. When any action is expensive in terms of time or performance,
it will be represented by a suspending / async function which you can
execute in a CoroutineScope
await
in your code.
In general, the SDK makes extensive use of kotlinx.coroutine
APIs: for example, many mutable variables are exposed as a StateFlow<T>
.
Basic experience with such APIs and concepts is required in order to use the SDK efficiently.
In general, the SDK makes extensive use of Swift concurrency and Combine APIs. For example, you may find mutable variables
exposed as a Sequence
or a Publisher
. Basic experience with such APIs and concepts is required in order to use the SDK efficiently.
The Camera SDK APIs are distributed among two main classes:
CameraManager
CameraDevice
Camera Manager
The entry point of the library is called CameraManager
. A manager is responsible for managing a set of devices, each of them
representing a connection to one or more physical camera sensors currently available on the device.
Only use one
CameraManager
at a time.
Creating a Manager
The CameraManager
has a public constructor accepting an array of CameraFeature
s:
kotlinval manager = CameraManager(features = listOf(CameraFeature.Preview, CameraFeature.JpegPicture))
swiftlet manager = CameraManager(features: [.preview, .jpegPicture])
These features are hints to what you plan to do with the cameras. It is very important that this list reflects the intended usage, otherwise the manager may fail at allocating resources and sensor connections, or provide low-quality outputs.
Feature | Meaning |
---|---|
CameraFeature.Preview .preview | Streaming real-time preview of the camera stream into one or more views. |
CameraFeature.JpegPicture .jpegPicture | Capturing high quality JPEG pictures. |
CameraFeature.Video .video | Recording video files. |
CameraFeature.Barcode .barcode | Detecting barcodes in real-time. |
Manager Lifecycle
You should use one manager at a time, because managers take an exclusive lock on system resources like camera access.
Whenever you are done with a manager, and before creating a new one, make sure it leaves the scope so it is garbage collected and goes through deinitialization. This will make sure that resources are released for the next manager (or next app).
Whenever you are done with a manager, and before creating a new one, make sure you invoke CameraManager.destroy()
.
This will make sure that resources are released for the next manager (or next app).
kotlinmanager.destroy()
Permissions
By design, the SDK will not ask the user for camera access permissions, nor microphone usage in case of video recordings. It is your responsibility to do so and, once granted, interact with APIs that would otherwise fail.
When permissions are missing and an API requiring it is invoked, it will fail by throwing a
MissingPermissionException
MissingPermissionError
.