Comment on page
Using Cameras
Ultra Low Latency Camera Streaming
NatDevice provides an extensive API for using hardware cameras, encapsulated in the
CameraDevice
class.We begin by finding a camera with a device query.
void Start () {
// Create a device query to find camera devices
var filter = MediaDeviceCriteria.CameraDevice;
var query = new MediaDeviceQuery(filter);
// Get the camera device
var device = query.current as CameraDevice;
}
Make sure to add a
using NatML.Devices
statement at the top of your script.Camera devices work by streaming the camera preview to a user-provided delegate function:
void Start () {
...
// Start the preview
device.StartRunning(OnCameraImage);
}
The delegate function accepts a
CameraImage
which contains a view to the pixel buffer provided by the native camera system, along with EXIF metadata about the image:void OnCameraImage (CameraImage image) {
// Do stuff
...
}
The provided delegate is not guaranteed to be invoked on the Unity main thread, so refrain from using Unity APIs within this method.
The most common use case when using a camera device is to display the camera preview. For this, we will need to stream incoming camera images into a
Texture
. To do so, NatDevice provides the TextureOutput
:async void Start () {
...
// Create a texture output
var textureOutput = new TextureOutput();
// Start the preview
device.StartRunning(textureOutput);
// Wait until the texture has been created
await textureOutput.textureCreated;
// Access the texture
Texture2D previewTexture = textureOutput.texture;
}
Make sure to add a
using NatML.Devices.Outputs
statement at the top of your script.Camera devices allow for capturing high-resolution photos:
// Capture a photo
device.CapturePhoto(OnCameraPhoto);
void OnCameraPhoto (CameraImage cameraImage) {
// Do stuff
...
}
The camera preview can be stopped when it is no longer needed.
// Stop the camera
device.StopRunning();
Last modified 1yr ago