DeepMedia logodeepmedia

Camera SDK

Version 0.8.5

Search

Choose platform

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.JpegPictureCameraFeature.jpegPicture during manager initialization.

Capturing pictures

Assuming you have correctly opened a device, capturing pictures takes a single API call.

swift
guard let device: CameraDevice = manager.currentDevice else { return } // Forgot to open? let picture: Picture = try await device.takePicture(encoding: .jpeg, meterIfNeeded: true)
kotlin logokotlin
val 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 picture
  • width and height: two integers representing the picture size. The size is always the maximum available size that the camera sensor can handle in its current configuration
  • data: a java.nio.ByteBufferFoundation.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:

kotlin logokotlin
public 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.

KindRequired Device StateQualityResolutionMeteringSpeedFormats
PictureOpenMaximum qualityAs high as possibleYesSlowerJPEG
SnapshotStreamingLower qualityThat of the preview view/streamNoneFasterJPEG, raw pixel data

To capture snapshots, use the takeSnapshot API. You can use:

  • Snapshot.Encoding.JpegSnapshot.Encoding.None for JPEG
  • Snapshot.Encoding.NoneSnapshot.Encoding.none for raw pixel data
swift
guard let device: CameraDevice = manager.currentDevice else { return } // Forgot to open? let snapshot: Snapshot = try await device.takeSnapshot(encoding: .jpeg, surface: "MyPreviewSurface")
kotlin logokotlin
val 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 snapshot
  • width and height: two integers representing the snapshot size
  • data: a java.nio.ByteBufferFoundation.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:

kotlin logokotlin
public 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 { ... }

Subscribe to the DeepMedia Newsletter

The latest news about DeepMedia products, open source projects and software development at our company.

By clicking “Subscribe”, you agree that DeepMedia may use your email address to send you newsletters, including commercial communications, and to process your personal data for this purpose. You agree that DeepMedia may process said data using third-party services for this purpose in accordance with the DeepMedia Privacy Policy. You can revoke this consent at any time using the unsubscribe link included in each email or by writing at contact@deepmedia.io.