title | author | description | keywords | dev_langs | ||
---|---|---|---|---|---|---|
CameraHelper |
skommireddi |
The CameraHelper provides helper methods to easily use the available camera frame sources to preview video, capture video frames and software bitmaps. |
windows 10, uwp, windows community toolkit, windows toolkit, CameraHelper, Camera, Frame Source, Video Frame, Software Bitmap |
|
The CameraHelper provides helper methods to easily use the available camera frame sources to preview video, capture video frames and software bitmaps. The helper currently shows camera frame sources that support color video preview or video record streams.
Important
Make sure you have the webcam capability enabled for your app to access the device's camera.
[!div class="nextstepaction"] Try it in the sample app
// Creates a Camera Helper and gets video frames from an available frame source.
using Microsoft.Toolkit.Uwp.Helpers.CameraHelper;
CameraHelper _cameraHelper = new CameraHelper();
var result = await _cameraHelper.InitializeAndStartCaptureAsync();
// Camera Initialization and Capture failed for some reason
if(result != CameraHelperResult.Success)
{
// get error information
var errorMessage = result.ToString();
}
else
{
// Subscribe to get frames as they arrive
_cameraHelper.FrameArrived += CameraHelper_FrameArrived;
}
private void CameraHelper_FrameArrived(object sender, FrameEventArgs e)
{
// Gets the current video frame
VideoFrame currentVideoFrame = e.VideoFrame;
// Gets the software bitmap image
SoftwareBitmap softwareBitmap = currentVideoFrame.SoftwareBitmap;
}
' Creates a Camera Helper and gets video frames from an available frame source.
Imports Microsoft.Toolkit.Uwp.Helpers
Dim _cameraHelper As CameraHelper = New CameraHelper()
Dim result = Await _cameraHelper.InitializeAndStartCaptureAsync()
' Camera Initialization and Capture failed for some reason
If result <> CameraHelperResult.Success Then
' get error information
Dim errorMessage = result.ToString()
Else
' Subscribe to get frames as they arrive
AddHandler _cameraHelper.FrameArrived, AddressOf CameraHelper_FrameArrived
End If
Private Sub CameraHelper_FrameArrived(ByVal sender As Object, ByVal e As FrameEventArgs)
' Gets the current video frame
Dim currentVideoFrame As VideoFrame = e.VideoFrame
' Gets the software bitmap image
Dim softwareBitmap As SoftwareBitmap = currentVideoFrame.SoftwareBitmap
End Sub
As a developer, you will need to make sure the CameraHelper resources are cleaned up when appropriate. For example, if the CameraHelper is only used on one page, make sure to clean up the CameraHelper when navigating away from the page.
Likewise, make sure to handle app suspending and resuming - CameraHelper should be cleaned up when suspending and re-initialized when resuming.
Call CameraHelper.CleanupAsync()
to clean up all internal resources. See the CameraHelper sample page in the sample app for full example.
Property | Type | Description |
---|---|---|
FrameSourceGroup | MediaFrameSourceGroup | Gets the currently selected MediaFrameSourceGroup for video preview. User can set this property to preview video from a specific source. If no MediaFrameSourceGroup is provided, Camera Helper selects the first available camera source to use for media capture. |
PreviewFrameSource | MediaFrameSource | Gets the currently selected MediaFrameSource for video preview. |
Methods | Return Type | Description |
---|---|---|
GetFrameSourceGroupsAsync() | Task<IReadOnlyList<MediaFrameSourceGroup>> | Gets a read only list of MediaFrameSourceGroups that support color video record or video preview streams. |
InitializeAndStartCaptureAsync() | Task<CameraHelperResult> | Initializes Media Capture and Frame Reader for video preview and capture frames in real time. |
CleanUpAsync() | Task | Use this asynchronous method to dispose Camera Helper resources |
Dispose() | void | Use this method to dispose Camera Helper resources |
Events | Description |
---|---|
FrameArrived | Fires when a new frame arrives. |
Demonstrates using Camera Helper to get video frames from a specific media frame source group.
using Microsoft.Toolkit.Uwp.Helpers.CameraHelper;
var availableFrameSourceGroups = await CameraHelper.GetFrameSourceGroupsAsync();
if(availableFrameSourceGroups != null)
{
CameraHelper cameraHelper = new CameraHelper() { FrameSourceGroup = availableFrameSourceGroups.FirstOrDefault() };
var result = await cameraHelper.InitializeAndStartCaptureAsync();
// Camera Initialization succeeded
if(result == CameraHelperResult.Success)
{
// Subscribe to get frames as they arrive
cameraHelper.FrameArrived += CameraHelper_FrameArrived;
// Optionally set a different frame source format
var newFormat = cameraHelper.FrameFormatsAvailable.Find((format) => format.VideoFormat.Width == 640);
if (newFormat != null)
{
await cameraHelper.PreviewFrameSource.SetFormatAsync(newFormat);
}
}
}
Imports Microsoft.Toolkit.Uwp.Helpers
Dim availableFrameSourceGroups = Await CameraHelper.GetFrameSourceGroupsAsync()
If availableFrameSourceGroups IsNot Nothing Then
Dim cameraHelper As CameraHelper = New CameraHelper() With {
.FrameSourceGroup = availableFrameSourceGroups.FirstOrDefault()
}
Dim result = Await cameraHelper.InitializeAndStartCaptureAsync()
If result = CameraHelperResult.Success Then
AddHandler cameraHelper.FrameArrived, AddressOf CameraHelper_FrameArrived
Dim newFormat = cameraHelper.FrameFormatsAvailable.Find(Function(format) format.VideoFormat.Width = 640)
If newFormat IsNot Nothing Then
Await cameraHelper.PreviewFrameSource.SetFormatAsync(newFormat)
End If
End If
End If
CameraHelper Sample Page Source. You can see this in action in the Windows Community Toolkit Sample App.
Device family | Universal, 10.0.17134.0 or higher |
---|---|
Namespace | Microsoft.Toolkit.Uwp |
NuGet package | Microsoft.Toolkit.Uwp |