Other versions of this page
Pictures
Still pictures in the JPEG format can be captured by acting on the current CameraDevice
. Support for more formats is
planned to be available in future versions of ther SDK.
Make sure you declare
CameraFeature.JpegPicture
CameraFeature.jpegPicture
during manager initialization.
Capturing pictures
Assuming you have correctly opened a device, capturing pictures takes a single API call.
swiftguard let device: CameraDevice = manager.currentDevice else { return } // Forgot to open?
let picture: Picture = try await device.takePicture(encoding: .jpeg, meterIfNeeded: true)
kotlinval device: CameraDevice = manager.currentDevice.value ?: return // Forgot to open?
val picture: Picture = device.takePicture(encoding = Picture.Encoding.Jpeg, meterIfNeeded = true)
You can use the extra parameter called meterIfNeeded
to device whether you want to run the exposure, focus and white
balance routines before the picture is taken. Defaults to true
as this is recommended for higher quality results.
Please check the metering docs for more information.
Picture results
As a result, you will receive an instance of the Picture
type, with the following properties:
id
: an integer that can be used to uniquely identify the picturewidth
andheight
: two integers representing the picture size. The size is always the maximum available size that the camera sensor can handle in its current configurationdata
: ajava.nio.ByteBuffer
Foundation.Data
reference with the actual image bytes that can, for example, be written to disk.
Picture results must be released using
release()
, otherwise memory will be leaked.
In addition, we provide some handy extension utilities for managing the byte buffer:
kotlinpublic fun Picture.toInputStream(release: Boolean = true): InputStream { ... }
public suspend fun Picture.toFile(file: File, release: Boolean = true) { ... }
public suspend fun Picture.toBitmap(maxSize: Int? = null, release: Boolean = true): Bitmap { ... }
You can also use the data
bytes to load the image pixels in memory for display, e.g. by using UIImage(data: picture.data)
.
Capturing snapshots
Snapshots provide a lower-quality, high speed alternative to pictures which can be useful in some scenarios. As the name suggests, snapshots are captured as a "screenshot" of what's seen in the current camera preview.
Kind | Required Device State | Quality | Resolution | Metering | Speed | Formats |
---|---|---|---|---|---|---|
Picture | Open | Maximum quality | As high as possible | Yes | Slower | JPEG |
Snapshot | Streaming | Lower quality | That of the preview view/stream | None | Faster | JPEG, raw pixel data |
To capture snapshots, use the takeSnapshot
API. You can use:
Snapshot.Encoding.Jpeg
Snapshot.Encoding.None
for JPEGSnapshot.Encoding.None
Snapshot.Encoding.none
for raw pixel data
swiftguard let device: CameraDevice = manager.currentDevice else { return } // Forgot to open?
let snapshot: Snapshot = try await device.takeSnapshot(encoding: .jpeg, surface: "MyPreviewSurface")
kotlinval device: CameraDevice = manager.currentDevice.value ?: return // Forgot to open?
val snapshot: Snapshot = device.takeSnapshot(encoding = Snapshot.Encoding.Jpeg, surface = "MyPreviewSurface")
Now how it is possible to pass the name of a preview surface for the engine to determine the correct size and orientation of the final picture.
- When not passing any value, the engine will try to find the only available preview (covers most use cases)
- When explicitly passing null / nil, the snapshot will use the raw sensor orientation and size
- In case of multiple previews, use the desired surface name as declared during surface creation (
createSurface
)
Snapshot results
As a result, you will receive an instance of the Snapshot
type, with the following properties:
id
: an integer that can be used to uniquely identify the snapshotwidth
andheight
: two integers representing the snapshot sizedata
: ajava.nio.ByteBuffer
Foundation.Data
reference with the actual bytes
Like pictures, snapshot results must be released using
release()
, otherwise memory will be leaked.
In addition, we provide some handy extension utilities for managing the byte buffer:
kotlinpublic fun Snapshot.toInputStream(release: Boolean = true): InputStream { ... }
public suspend fun Snapshot.toFile(file: File, release: Boolean = true) { ... }
public suspend fun Snapshot.toBitmap(maxSize: Int? = null, release: Boolean = true): Bitmap { ... }