Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
The CameraView provides the ability to connect to a camera, display a preview from the camera and take photos. The CameraView also offers features to support taking photos, controlling the flash, saving captured media to a file, and offering different hooks for events.
The following sections will incrementally build on how to use the CameraView in a .NET MAUI application. They rely on the use of a CameraViewModel. that will be set as the BindingContext of the example CameraViewPage.
Platform specific initialization
The CameraView is part of the CommunityToolkit.Maui.Camera nuget package. To first use the CameraView please refer to the Getting started section. The following platform specific setup is required.
The following permissions need to be added to the Platforms/Android/AndroidManifest.xml file:
<uses-permission android:name="android.permission.CAMERA" />
This should be added inside the <manifest> element. Below shows a more complete example:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<application android:allowBackup="true" android:icon="@mipmap/appicon" android:roundIcon="@mipmap/appicon_round" android:supportsRtl="true" />
<uses-permission android:name="android.permission.CAMERA" />
</manifest>
Basic usage
The CameraView can be added to a .NET MAUI application in the following way.
Including the XAML namespace
In order to use the toolkit in XAML the following xmlns needs to be added into your page or view:
xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
Therefore the following:
<ContentPage
x:Class="CommunityToolkit.Maui.Sample.Pages.MyPage"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">
</ContentPage>
Would be modified to include the xmlns as follows:
<ContentPage
x:Class="CommunityToolkit.Maui.Sample.Pages.MyPage"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit">
</ContentPage>
<ContentPage
x:Class="CommunityToolkit.Maui.Sample.Pages.CameraViewPage"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit">
<Grid ColumnDefinitions="*,*,*" RowDefinitions="*,30">
<toolkit:CameraView
Grid.ColumnSpan="3"
Grid.Row="0" />
</Grid>
</ContentPage>
The result will be a surface rendering the output of the default camera connected to the device.
Access the current camera
The SelectedCamera property provides the ability to access the currently selected camera.
The following example shows how to bind the SelectedCamera property from the CameraView to a property on the CameraViewModel with the same name (SelectedCamera).
<ContentPage
x:Class="CommunityToolkit.Maui.Sample.Pages.CameraViewPage"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit">
<Grid ColumnDefinitions="*,*,*" RowDefinitions="*,30,30">
<toolkit:CameraView
Grid.ColumnSpan="3"
Grid.Row="0"
SelectedCamera="{Binding SelectedCamera}" />
</Grid>
</ContentPage>
Control Zoom
The SelectedCamera property provides both a MinimumZoomFactor and a MaximumZoomFactor property, these are read-only and provide developers with a programmatic way of determining what zoom can be applied to the current camera. In order to change the zoom on the current camera the CameraView provides the ZoomFactor property.
Note
If a value is provided outside of the MinimumZoomFactor and MaximumZoomFactor the CameraView will clamp the value to keep it within the bounds.
The following example shows how to add a Slider into the application and setup the following bindings:
- Bind the
Maximumproperty of theSliderto theMaximumZoomFactorof theSelectedCameraproperty. - Bind the
Minimumproperty of theSliderto theMinimumZoomFactorof theSelectedCameraproperty. - Bind the
Valueproperty of theSliderto theCurrentZoomproperty on theCameraViewModelclass.
The final change involves binding the ZoomFactor property of the CameraView to the CurrentZoom property on the CameraViewModel class.
<ContentPage
x:Class="CommunityToolkit.Maui.Sample.Pages.CameraViewPage"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit">
<Grid ColumnDefinitions="*,*,*" RowDefinitions="*,30,30">
<toolkit:CameraView
Grid.ColumnSpan="3"
Grid.Row="0"
SelectedCamera="{Binding SelectedCamera}"
ZoomFactor="{Binding CurrentZoom}" />
<Slider
Grid.Column="0"
Grid.Row="1"
Value="{Binding CurrentZoom}"
Maximum="{Binding SelectedCamera.MaximumZoomFactor, FallbackValue=1}"
Minimum="{Binding SelectedCamera.MinimumZoomFactor, FallbackValue=1}"/>
</Grid>
</ContentPage>
Camera Flash Mode
The CameraView provides the ability to programmatically change the flash mode on the device, the possible options are:
Off- the flash is off and will not be used.On- the flash is on and will always be used.Auto- the flash will automatically be used based on the lighting conditions.
The SelectedCamera property also provides the IsFlashSupported which makes it possible to determine whether the currently selected camera has a flash that can be controlled.
The following example shows how to add a Picker into the application and setup the following bindings:
- Bind the
IsVisibleproperty of thePickerto theIsFlashSupportedof theSelectedCameraproperty. - Bind the
ItemsSourceproperty of thePickerto theFlashModesproperty on theCameraViewModelclass - a simple list of the possible values of theCameraFlashModeenum. - Bind the
SelectedItemproperty of thePickerto theFlashModeproperty on theCameraViewModelclass.
The final change involves binding the CameraFlashMode property of the CameraView to the FlashMode property on the CameraViewModel class.
<ContentPage
x:Class="CommunityToolkit.Maui.Sample.Pages.CameraViewPage"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit">
<Grid ColumnDefinitions="*,*,*" RowDefinitions="*,30,30">
<toolkit:CameraView
Grid.ColumnSpan="3"
Grid.Row="0"
SelectedCamera="{Binding SelectedCamera}"
ZoomFactor="{Binding CurrentZoom}"
CameraFlashMode="{Binding FlashMode}" />
<Slider
Grid.Column="0"
Grid.Row="1"
Value="{Binding CurrentZoom}"
Maximum="{Binding SelectedCamera.MaximumZoomFactor, FallbackValue=1}"
Minimum="{Binding SelectedCamera.MinimumZoomFactor, FallbackValue=1}"/>
<Picker
Grid.Column="1"
Grid.Row="1"
Title="Flash"
IsVisible="{Binding Path=SelectedCamera.IsFlashSupported, FallbackValue=false}"
ItemsSource="{Binding FlashModes}"
SelectedItem="{Binding FlashMode}" />
</Grid>
</ContentPage>
ImageCaptureResolution
The CameraView provides the ability to programmatically change resolution for images captured from the current camera.
Note
This will not change the resolution displayed in the preview from the camera.
The SelectedCamera property also provides the SupportedResolutions which makes it possible to determine which resolutions the current camera supports.
The following example shows how to add a Picker into the application and setup the following bindings:
- Bind the
ItemsSourceproperty of thePickerto theSupportedResolutionsof theSelectedCameraproperty. - Bind the
SelectedItemproperty of thePickerto theSelectedResolutionproperty on theCameraViewModelclass.
The final change involves binding the ImageCaptureResolution property of the CameraView to the SelectedResolution property on the CameraViewModel class.
<ContentPage
x:Class="CommunityToolkit.Maui.Sample.Pages.CameraViewPage"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit">
<Grid ColumnDefinitions="*,*,*" RowDefinitions="*,30,30">
<toolkit:CameraView
Grid.ColumnSpan="3"
Grid.Row="0"
SelectedCamera="{Binding SelectedCamera}"
ZoomFactor="{Binding CurrentZoom}"
CameraFlashMode="{Binding FlashMode}" />
<Slider
Grid.Column="0"
Grid.Row="1"
Value="{Binding CurrentZoom}"
Maximum="{Binding SelectedCamera.MaximumZoomFactor, FallbackValue=1}"
Minimum="{Binding SelectedCamera.MinimumZoomFactor, FallbackValue=1}"/>
<Picker
Grid.Column="1"
Grid.Row="1"
Title="Flash"
IsVisible="{Binding Path=SelectedCamera.IsFlashSupported, FallbackValue=false}"
ItemsSource="{Binding FlashModes}"
SelectedItem="{Binding FlashMode}" />
<Picker
Grid.Column="2"
Grid.Row="1"
Title="Available Resolutions"
ItemsSource="{Binding SelectedCamera.SupportedResolutions}"
SelectedItem="{Binding SelectedResolution}" />
</Grid>
</ContentPage>
CaptureImage
The CameraView provides the ability to programmatically trigger an image capture. This is possible through both the CaptureImage method or the CaptureImageCommand.
The following example shows how to add a Button into the application and setup the following bindings:
- Bind the
Commandproperty of theButtonto theCaptureImageCommandproperty on theCameraView.
<ContentPage
x:Class="CommunityToolkit.Maui.Sample.Pages.CameraViewPage"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit">
<Grid ColumnDefinitions="*,*,*" RowDefinitions="*,30,30">
<toolkit:CameraView
x:Name="Camera"
Grid.ColumnSpan="3"
Grid.Row="0"
SelectedCamera="{Binding SelectedCamera}"
ZoomFactor="{Binding CurrentZoom}"
CameraFlashMode="{Binding FlashMode}" />
<Slider
Grid.Column="0"
Grid.Row="1"
Value="{Binding CurrentZoom}"
Maximum="{Binding SelectedCamera.MaximumZoomFactor, FallbackValue=1}"
Minimum="{Binding SelectedCamera.MinimumZoomFactor, FallbackValue=1}"/>
<Picker
Grid.Column="1"
Grid.Row="1"
Title="Flash"
IsVisible="{Binding Path=SelectedCamera.IsFlashSupported, FallbackValue=false}"
ItemsSource="{Binding FlashModes}"
SelectedItem="{Binding FlashMode}" />
<Picker
Grid.Column="2"
Grid.Row="1"
Title="Available Resolutions"
ItemsSource="{Binding SelectedCamera.SupportedResolutions}"
SelectedItem="{Binding SelectedResolution}" />
<Button
Grid.Column="0"
Grid.Row="2"
Command="{Binding CaptureImageCommand, Source={x:Reference Camera}}"
Text="Capture Image" />
</Grid>
</ContentPage>
Note
In order to use the image that has been captured the CameraView provides the MediaCaptured event.
The following example demonstrates how to use the CaptureImage method:
Note
The C# code below uses the Camera field defined above in XAML (<toolkit:CameraView x:Name="Camera" />)
async void HandleCaptureButtonTapped(object? sender, EventArgs e)
{
try
{
// Use the Camera field defined above in XAML (`<toolkit:CameraView x:Name="Camera" />`)
var captureImageCTS = new CancellationTokenSource(TimeSpan.FromSeconds(3));
Stream stream = await Camera.CaptureImage(captureImageCTS.Token);
}
catch(Exception e)
{
// Handle Exception
Trace.WriteLine(e);
}
}
Video Recording
The CameraView provides the ability to record videos. This is possible through both the StartVideoRecording method or the StartVideoRecordingCommand.
The following example shows how to add a Button into the application and setup the following bindings:
- Bind the
Commandproperty of theButtonto theStartVideoRecordingCommandproperty on theCameraView.
<ContentPage
x:Class="CommunityToolkit.Maui.Sample.Pages.CameraViewPage"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit">
<Grid ColumnDefinitions="*,*,*" RowDefinitions="*,30,30">
<toolkit:CameraView
x:Name="Camera"
Grid.ColumnSpan="3"
Grid.Row="0"
SelectedCamera="{Binding SelectedCamera}"
ZoomFactor="{Binding CurrentZoom}"
CameraFlashMode="{Binding FlashMode}" />
<Slider
Grid.Column="0"
Grid.Row="1"
Value="{Binding CurrentZoom}"
Maximum="{Binding SelectedCamera.MaximumZoomFactor, FallbackValue=1}"
Minimum="{Binding SelectedCamera.MinimumZoomFactor, FallbackValue=1}"/>
<Picker
Grid.Column="1"
Grid.Row="1"
Title="Flash"
IsVisible="{Binding Path=SelectedCamera.IsFlashSupported, FallbackValue=false}"
ItemsSource="{Binding FlashModes}"
SelectedItem="{Binding FlashMode}" />
<Picker
Grid.Column="2"
Grid.Row="1"
Title="Available Resolutions"
ItemsSource="{Binding SelectedCamera.SupportedResolutions}"
SelectedItem="{Binding SelectedResolution}" />
<Button Clicked="StartCameraRecording"
Text="StartVideoRecording" />
<Button Command="{Binding StartVideoRecordingCommand, Source={x:Reference Camera}, x:DataType=toolkit:CameraView}"
CommandParameter="{Binding Stream}"
Text="StartVideoRecording" />
<Button Command="{Binding StopVideoRecordingCommand, Source={x:Reference Camera}, x:DataType=toolkit:CameraView}"
CommandParameter="{Binding Token}"
Text="StopVideoRecording" />
</Grid>
</ContentPage>
Note
You must provide a clean stream in order to record the video.
The following example demonstrates how to use the StartVideoRecording method:
Note
The C# code below uses the Camera field defined above in XAML (<toolkit:CameraView x:Name="Camera" />)
async void StartCameraRecordingWithCustomStream(object? sender, EventArgs e)
{
using var threeSecondVideoRecordingStream = new FileStream("recording.mp4");
await Camera.StartVideoRecording(stream, CancellationToken.None);
await Task.Delay(TimeSpan.FromSeconds(3));
await Camera.StopVideoRecording(CancellationToken.None);
await FileSaver.SaveAsync("recording.mp4", threeSecondVideoRecordingStream);
}
In case you want to record a short video and record video in MemoryStream you can use the next overload of VideoRecording:
async void StartCameraRecording(object? sender, EventArgs e)
{
await Camera.StartVideoRecording(CancellationToken.None);
await Task.Delay(TimeSpan.FromSeconds(3));
var threeSecondVideoRecordingStream = await Camera.StopVideoRecording(CancellationToken.None);
await FileSaver.SaveAsync("recording.mp4", threeSecondVideoRecordingStream);
}
Start preview
The CameraView provides the ability to programmatically start the preview from the camera. This is possible through both the StartCameraPreview method or the StartCameraPreviewCommand.
The following example shows how to add a Button into the application and setup the following bindings:
- Bind the
Commandproperty of theButtonto theStartCameraPreviewCommandproperty on theCameraView.
<ContentPage
x:Class="CommunityToolkit.Maui.Sample.Pages.CameraViewPage"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit">
<Grid ColumnDefinitions="*,*,*" RowDefinitions="*,30,30">
<toolkit:CameraView
x:Name="Camera"
Grid.ColumnSpan="3"
Grid.Row="0"
SelectedCamera="{Binding SelectedCamera}"
ZoomFactor="{Binding CurrentZoom}"
CameraFlashMode="{Binding FlashMode}" />
<Slider
Grid.Column="0"
Grid.Row="1"
Value="{Binding CurrentZoom}"
Maximum="{Binding SelectedCamera.MaximumZoomFactor, FallbackValue=1}"
Minimum="{Binding SelectedCamera.MinimumZoomFactor, FallbackValue=1}"/>
<Picker
Grid.Column="1"
Grid.Row="1"
Title="Flash"
IsVisible="{Binding Path=SelectedCamera.IsFlashSupported, FallbackValue=false}"
ItemsSource="{Binding FlashModes}"
SelectedItem="{Binding FlashMode}" />
<Picker
Grid.Column="2"
Grid.Row="1"
Title="Available Resolutions"
ItemsSource="{Binding SelectedCamera.SupportedResolutions}"
SelectedItem="{Binding SelectedResolution}" />
<Button
Grid.Column="0"
Grid.Row="2"
Command="{Binding CaptureImageCommand, Source={x:Reference Camera}}"
Text="Capture Image" />
<Button
Grid.Column="1"
Grid.Row="2"
Command="{Binding StartCameraPreviewCommand, Source={x:Reference Camera}}"
Text="Capture Image" />
</Grid>
</ContentPage>
The following example demonstrates how to use the StartCameraPreview method:
Note
The C# code below uses the Camera field defined above in XAML (<toolkit:CameraView x:Name="Camera" />)
async void HandleStartCameraPreviewButtonTapped(object? sender, EventArgs e)
{
try
{
var startCameraPreviewTCS = new CancellationTokenSource(TimeSpan.FromSeconds(3));
// Use the Camera field defined above in XAML (`<toolkit:CameraView x:Name="Camera" />`)
await Camera.StartCameraPreview(startCameraPreviewTCS.Token);
}
catch(Exception e)
{
// Handle Exception
Trace.WriteLine(e);
}
}
Stop preview
The CameraView provides the ability to programmatically stop the preview from the camera. This is possible through both the StopCameraPreview method or the StopCameraPreviewCommand.
The following example shows how to add a Button into the application and setup the following bindings:
- Bind the
Commandproperty of theButtonto theStopCameraPreviewCommandproperty on theCameraView.
<ContentPage
x:Class="CommunityToolkit.Maui.Sample.Pages.CameraViewPage"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit">
<Grid ColumnDefinitions="*,*,*" RowDefinitions="*,30,30">
<toolkit:CameraView
x:Name="Camera"
Grid.ColumnSpan="3"
Grid.Row="0"
SelectedCamera="{Binding SelectedCamera}"
ZoomFactor="{Binding CurrentZoom}"
CameraFlashMode="{Binding FlashMode}" />
<Slider
Grid.Column="0"
Grid.Row="1"
Value="{Binding CurrentZoom}"
Maximum="{Binding SelectedCamera.MaximumZoomFactor, FallbackValue=1}"
Minimum="{Binding SelectedCamera.MinimumZoomFactor, FallbackValue=1}"/>
<Picker
Grid.Column="1"
Grid.Row="1"
Title="Flash"
IsVisible="{Binding Path=SelectedCamera.IsFlashSupported, FallbackValue=false}"
ItemsSource="{Binding FlashModes}"
SelectedItem="{Binding FlashMode}" />
<Picker
Grid.Column="2"
Grid.Row="1"
Title="Available Resolutions"
ItemsSource="{Binding SelectedCamera.SupportedResolutions}"
SelectedItem="{Binding SelectedResolution}" />
<Button
Grid.Column="0"
Grid.Row="2"
Command="{Binding CaptureImageCommand, Source={x:Reference Camera}}"
Text="Capture Image" />
<Button
Grid.Column="1"
Grid.Row="2"
Command="{Binding StartCameraPreviewCommand, Source={x:Reference Camera}}"
Text="Capture Image" />
<Button
Grid.Column="2"
Grid.Row="2"
Command="{Binding StopCameraPreviewCommand, Source={x:Reference Camera}}"
Text="Capture Image" />
</Grid>
</ContentPage>
The following example demonstrates how to use the StopCameraPreview method:
Note
The C# code below uses the Camera field defined above in XAML (<toolkit:CameraView x:Name="Camera" />)
void HandleStopCameraPreviewButtonTapped(object? sender, EventArgs e)
{
try
{
// Use the Camera field defined above in XAML (`<toolkit:CameraView x:Name="Camera" />`)
Camera.StopCameraPreview();
}
catch(Exception e)
{
// Handle Exception
Trace.WriteLine(e);
}
}
Examples
You can find an example of this feature in action in the .NET MAUI Community Toolkit Sample Application.
API
You can find the source code for CameraView over on the .NET MAUI Community Toolkit GitHub repository.
.NET MAUI Community Toolkit