Skip to content
Draft
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions docs/maui/views/camera-view.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ This should be added inside the `<dict>` element. Below shows a more complete ex

<key>NSCameraUsageDescription</key>
<string>PROVIDE YOUR REASON HERE</string>

<key>NSMicrophoneUsageDescription</key>
<string>PROVIDE YOUR REASON HERE</string>
</dict>
</plist>
```
Expand Down Expand Up @@ -127,10 +130,15 @@ This should be added inside the `<dict>` element. Below shows a more complete ex

<key>NSCameraUsageDescription</key>
<string>PROVIDE YOUR REASON HERE</string>

<key>NSMicrophoneUsageDescription</key>
<string>PROVIDE YOUR REASON HERE</string>
</dict>
</plist>
```

More details can be found here: https://developer.apple.com/documentation/avfoundation/requesting-authorization-to-capture-and-save-media

### [Windows](#tab/windows)

No setup is required.
Expand Down Expand Up @@ -422,6 +430,87 @@ async void HandleCaptureButtonTapped(object? sender, EventArgs 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 `Command` property of the `Button` to the `StartVideoRecordingCommand` property on the `CameraView`.

```xaml
<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" />`)

```cs
async void StartCameraRecording(object? sender, EventArgs e)
{
using var stream = new MemoryStream();
await Camera.StartVideoRecording(stream);
//some delay
await Camera.StopVideoRecording();
await FileSaver.SaveAsync("recording.mp4", stream);
}
```

## 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`.
Expand Down