Skip to content

Commit f5d955a

Browse files
TheCodeTravelerjfversluis
authored andcommitted
Add C# Examples for CameraView.CaptureImage, CameraView.StartCameraPreview and CameraView.StopCameraPreview (#553)
* Update email-validation-behavior.md * Add .NET Hot Reload Docs * Add C# Examples --------- Co-authored-by: Brandon Minnick <13558917+brminnick@users.noreply.github.com> Co-authored-by: Gerald Versluis <gerald@verslu.is> Co-authored-by: Gerald Versluis <gerald.versluis@microsoft.com>
1 parent b5c4ea7 commit f5d955a

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

docs/maui/views/camera-view.md

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,40 @@ The following example shows how to add a `Button` into the application and setup
400400
> [!NOTE]
401401
> In order to use the image that has been captured the `CameraView` provides the `MediaCaptured` event.
402402
403+
The following example demonstrates how to use the `CaptureImage` method:
404+
405+
> [!NOTE]
406+
> The C# code below uses the Camera field defined above in XAML (`<toolkit:CameraView x:Name="Camera" />`)
407+
408+
```cs
409+
async void HandleCaptureButtonTapped(object? sender, EventArgs e)
410+
{
411+
// Use the Camera field defined above in XAML (`<toolkit:CameraView x:Name="Camera" />`)
412+
Camera.MediaCaptured += HandleCameraViewMediaCaptured;
413+
414+
try
415+
{
416+
var captureImageCTS = new CancellationTokenSource(TimeSpan.FromSeconds(3));
417+
await Camera.CaptureImage(captureImageCTS.Token);
418+
}
419+
catch(Exception e)
420+
{
421+
// Handle Exception
422+
Trace.WriteLine(e);
423+
}
424+
finally
425+
{
426+
Camera.MediaCaptured -= HandleCameraViewMediaCaptured;
427+
}
428+
}
429+
430+
void HandleCameraViewMediaCaptured(object? sender, MediaCapturedEventArgs e)
431+
{
432+
Stream stream = e.Media;
433+
// process media
434+
}
435+
```
436+
403437
## Start preview
404438

405439
The `CameraView` provides the ability to programmatically start the preview from the camera. This is possible through both the `StartCameraPreview` method or the `StartCameraPreviewCommand`.
@@ -462,6 +496,30 @@ The following example shows how to add a `Button` into the application and setup
462496
</ContentPage>
463497
```
464498

499+
The following example demonstrates how to use the `StartCameraPreview` method:
500+
501+
> [!NOTE]
502+
> The C# code below uses the Camera field defined above in XAML (`<toolkit:CameraView x:Name="Camera" />`)
503+
504+
```cs
505+
async void HandleStartCameraPreviewButtonTapped(object? sender, EventArgs e)
506+
{
507+
508+
try
509+
{
510+
var startCameraPreviewTCS = new CancellationTokenSource(TimeSpan.FromSeconds(3));
511+
512+
// Use the Camera field defined above in XAML (`<toolkit:CameraView x:Name="Camera" />`)
513+
await Camera.StartCameraPreview(startCameraPreviewTCS.Token);
514+
}
515+
catch(Exception e)
516+
{
517+
// Handle Exception
518+
Trace.WriteLine(e);
519+
}
520+
}
521+
```
522+
465523
## Stop preview
466524

467525
The `CameraView` provides the ability to programmatically stop the preview from the camera. This is possible through both the `StopCameraPreview` method or the `StopCameraPreviewCommand`.
@@ -530,6 +588,28 @@ The following example shows how to add a `Button` into the application and setup
530588
</ContentPage>
531589
```
532590

591+
The following example demonstrates how to use the `StopCameraPreview` method:
592+
593+
> [!NOTE]
594+
> The C# code below uses the Camera field defined above in XAML (`<toolkit:CameraView x:Name="Camera" />`)
595+
596+
```cs
597+
void HandleStopCameraPreviewButtonTapped(object? sender, EventArgs e)
598+
{
599+
600+
try
601+
{
602+
// Use the Camera field defined above in XAML (`<toolkit:CameraView x:Name="Camera" />`)
603+
Camera.StopCameraPreview();
604+
}
605+
catch(Exception e)
606+
{
607+
// Handle Exception
608+
Trace.WriteLine(e);
609+
}
610+
}
611+
```
612+
533613
## Examples
534614

535615
You can find an example of this feature in action in the [.NET MAUI Community Toolkit Sample Application](https://github.com/CommunityToolkit/Maui/blob/main/samples/CommunityToolkit.Maui.Sample/Pages/Views/CameraView/CameraViewPage.xaml.cs).

0 commit comments

Comments
 (0)