Skip to content

Commit c4a4a01

Browse files
committed
Renaming cameracaptureui how-to
1 parent cde07ba commit c4a4a01

File tree

1 file changed

+180
-0
lines changed

1 file changed

+180
-0
lines changed
Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
---
2+
title: Capture photos and video in a desktop app with the Windows built-in camera UI
3+
description: Learn how to capture photos and video using the camera UI built into Windows.
4+
ms.topic: article
5+
ms.date: 07/23/2024
6+
ms.author: drewbat
7+
author: drewbatgit
8+
ms.localizationpriority: medium
9+
#customer intent: As a developer, I want to access the camera in a Windows app using WinUI 3.
10+
---
11+
12+
# Capture photos and video in a desktop app with the Windows built-in camera UI
13+
14+
This article describes how to use the [CameraCaptureUI](/windows/windows-app-sdk/api/winrt/microsoft.windows.media.capture) class to capture photos or videos by using the camera UI built into Windows. This feature allows your app to get a user-captured photo or video with just a few lines of code.
15+
16+
If you want to provide your own camera UI, or if your scenario requires more robust, low-level control of the capture operation, then you should use the [MediaCapture](/uwp/api/windows.media.capture.mediacapture) class, and implement your own capture experience. For more information, see [Basic photo, video, and audio capture with MediaCapture](basic-photo-capture.md).
17+
18+
Note that the **CameraCaptureUI** class in the [Microsoft.Windows.Media.Capture](/windows/windows-app-sdk/api/winrt/microsoft.windows.media.capture) namespace is not supported for UWP apps. For information on using the UWP version of this feature, see [Capture photos and video in a UWP app with the Windows built-in camera UI](/windows/uwp/audio-video-camera/capture-photos-and-video-with-cameracaptureui)
19+
20+
Create a new instance of **CameraCaptureUI**, passing in the [AppWindow.Id](/windows/windows-app-sdk/api/winrt/microsoft.ui.windowing.appwindow.id) property of your app window. The [PhotoSettings](/windows/windows-app-sdk/api/winrt/microsoft.windows.media.capture.cameracaptureui.photosettings) property allows you to specify some constraints on the captured photo, including the file format and maximum resolution and whether UI allows the user to crop the photo after it's captured. The [VideoSettings](/windows/windows-app-sdk/api/winrt/microsoft.windows.media.capture.cameracaptureui.videosettings) property provides similar properties for video capture, such as the maximum resolution and duration and whether the UI allows the user to trip the video after it's captured.
21+
22+
Call [CaptureFileAsync](/windows/windows-app-sdk/api/winrt/microsoft.windows.media.capture.cameracaptureui.capturefileasync) to launch the camera capture UI asynchronously. Use one of the values from the [CameraCaptureUIMode](/windows/windows-app-sdk/api/winrt/microsoft.windows.media.capture.cameracaptureuimode) to specify whether the UI should allow photo capture, video capture, or both. When **CaptureFileAsync** completes, it will return a [StorageFile](/uwp/api/windows.storage.storagefile) file object containing the captured photo or video. If the returned object is null it means that either the user cancelled the capture operation or an error occurred.
23+
24+
The following example demonstrates launching the **CameraCaptureUI** for photo capture specifying the image format as PNG and disabling cropping. In this example the captured photo is set as the source for an [Image](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.controls.image) control.
25+
26+
### [C#](#tab/csharp)
27+
28+
```csharp
29+
var cameraCaptureUI = new CameraCaptureUI(this.AppWindow.Id);
30+
cameraCaptureUI.PhotoSettings.Format = CameraCaptureUIPhotoFormat.Png;
31+
cameraCaptureUI.PhotoSettings.AllowCropping = false;
32+
33+
// Capture a photo asynchronously
34+
StorageFile photo = await cameraCaptureUI.CaptureFileAsync(CameraCaptureUIMode.Photo);
35+
36+
if (photo != null)
37+
{
38+
// Photo capture was successful
39+
40+
// Show the captured photo in a XAML Image control
41+
using (IRandomAccessStream fileStream = await photo.OpenAsync(Windows.Storage.FileAccessMode.Read))
42+
{
43+
// Set the image source to the selected bitmap
44+
BitmapImage bitmapImage = new BitmapImage();
45+
await bitmapImage.SetSourceAsync(fileStream);
46+
iCapturedImage.Source = bitmapImage;
47+
}
48+
} else
49+
{
50+
// Photo capture failed or was cancelled
51+
}
52+
53+
```
54+
55+
### [C++/WinRT](#tab/cpp)
56+
57+
```cpp
58+
// Get the WindowId for the window
59+
Microsoft::UI::WindowId windowId = this->AppWindow().Id();
60+
61+
// Initialize CameraCaptureUI with a window handle
62+
winrt::Microsoft::Windows::Media::Capture::CameraCaptureUI cameraUI(windowId);
63+
64+
// Configure Photo Settings
65+
cameraUI.PhotoSettings().Format(CameraCaptureUIPhotoFormat::Png);
66+
cameraUI.PhotoSettings().AllowCropping(false);
67+
68+
// Capture a photo asynchronously
69+
auto photo = co_await cameraUI.CaptureFileAsync(CameraCaptureUIMode::Photo);
70+
71+
72+
if (photo != nullptr)
73+
{
74+
// Photo capture was successful
75+
76+
// Show the captured photo in a XAML Image control
77+
IRandomAccessStream fileStream = co_await photo.OpenAsync(Windows::Storage::FileAccessMode::Read);
78+
BitmapImage bitmapImage = BitmapImage();
79+
co_await bitmapImage.SetSourceAsync(fileStream);
80+
iCapturedImage().Source(bitmapImage);
81+
82+
}
83+
else
84+
{
85+
// Photo capture failed or was cancelled
86+
}
87+
```
88+
89+
---
90+
91+
The following example demonstrates launching the **CameraCaptureUI** for video capture specifying the maximum video as standard definition and disabling trimming. In this example the captured photo is set as the source for an [MediaPlayerElement](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.controls.mediaplayerelement) control.
92+
93+
### [C#](#tab/csharp)
94+
95+
```csharp
96+
var cameraCaptureUI = new CameraCaptureUI(this.AppWindow.Id);
97+
cameraCaptureUI.VideoSettings.MaxResolution = CameraCaptureUIMaxVideoResolution.StandardDefinition;
98+
cameraCaptureUI.VideoSettings.AllowTrimming = true;
99+
StorageFile videoFile = await cameraCaptureUI.CaptureFileAsync(CameraCaptureUIMode.Video);
100+
101+
if (videoFile != null)
102+
{
103+
// Video capture was successful
104+
105+
// Show the captured video in a MediaPlayerElement control
106+
mediaPlayerElement.Source = MediaSource.CreateFromStorageFile(videoFile);
107+
mediaPlayerElement.MediaPlayer.Play();
108+
}
109+
else
110+
{
111+
// Photo capture failed or was cancelled
112+
}
113+
```
114+
115+
### [C++/WinRT](#tab/cpp)
116+
117+
```cpp
118+
Microsoft::UI::WindowId windowId = this->AppWindow().Id();
119+
120+
winrt::Microsoft::Windows::Media::Capture::CameraCaptureUI cameraUI(windowId);
121+
122+
cameraUI.VideoSettings().MaxResolution(CameraCaptureUIMaxVideoResolution::StandardDefinition);
123+
cameraUI.VideoSettings().AllowTrimming(true);
124+
StorageFile videoFile = co_await cameraUI.CaptureFileAsync(CameraCaptureUIMode::Video);
125+
126+
if (videoFile != nullptr)
127+
{
128+
// Video capture was successful
129+
130+
// Show the captured video in a MediaPlayerElement control
131+
auto source = winrt::Windows::Media::Core::MediaSource::CreateFromStorageFile(videoFile);
132+
mediaPlayerElement().Source(MediaSource::CreateFromStorageFile(videoFile));
133+
134+
}
135+
else
136+
{
137+
// Photo capture failed or was cancelled
138+
}
139+
```
140+
141+
---
142+
143+
The **CameraCaptureUI** creates randomized names for captured media files, so you may want to rename and move captured files to keep them organized. THe following example moves and renames a captured file.
144+
145+
146+
### [C#](#tab/csharp)
147+
148+
```csharp
149+
StorageFile photo = await cameraCaptureUI.CaptureFileAsync(CameraCaptureUIMode.Photo);
150+
151+
if (photo != null)
152+
{
153+
// Move and rename captured photo
154+
StorageFolder destinationFolder =
155+
await ApplicationData.Current.LocalFolder.CreateFolderAsync("ProfilePhotoFolder",
156+
CreationCollisionOption.OpenIfExists);
157+
158+
await photo.CopyAsync(destinationFolder, "ProfilePhoto.jpg", NameCollisionOption.ReplaceExisting);
159+
await photo.DeleteAsync();
160+
}
161+
```
162+
163+
### [C++/WinRT](#tab/cpp)
164+
165+
```cpp
166+
auto photo = co_await cameraUI.CaptureFileAsync(CameraCaptureUIMode::Photo);
167+
168+
169+
if (photo != nullptr)
170+
{
171+
// Move and rename captured photo
172+
StorageFolder destinationFolder = co_await ApplicationData::Current().LocalFolder()
173+
.CreateFolderAsync(L"ProfilePhotoFolder", CreationCollisionOption::OpenIfExists);
174+
175+
co_await photo.CopyAsync(destinationFolder, L"ProfilePhoto.jpg", NameCollisionOption::ReplaceExisting);
176+
co_await photo.DeleteAsync();
177+
}
178+
```
179+
180+
---

0 commit comments

Comments
 (0)