Skip to content

Commit 0298f81

Browse files
committed
Adding a few more examples
1 parent 99a66ac commit 0298f81

File tree

1 file changed

+117
-11
lines changed

1 file changed

+117
-11
lines changed

hub/apps/develop/camera/camera-capture-ui.md

Lines changed: 117 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,35 @@ Create a new instance of **CameraCaptureUI**, passing in the [AppWindow.Id](/win
2121

2222
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.
2323

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+
2426
### [C#](#tab/csharp)
2527

2628
```csharp
27-
var cameraCaptureUI = new CameraCaptureUI(this.AppWindow.Id);
28-
cameraCaptureUI.PhotoSettings.Format = CameraCaptureUIPhotoFormat.Png;
29-
cameraCaptureUI.PhotoSettings.AllowCropping = false;
29+
var cameraCaptureUI = new CameraCaptureUI(this.AppWindow.Id);
30+
cameraCaptureUI.PhotoSettings.Format = CameraCaptureUIPhotoFormat.Png;
31+
cameraCaptureUI.PhotoSettings.AllowCropping = false;
3032

31-
// Capture a photo asynchronously
32-
StorageFile photo = await cameraCaptureUI.CaptureFileAsync(CameraCaptureUIMode.Photo);
33+
// Capture a photo asynchronously
34+
StorageFile photo = await cameraCaptureUI.CaptureFileAsync(CameraCaptureUIMode.Photo);
3335

34-
if (photo != null)
35-
{
36-
// Photo capture was successful
37-
} else
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))
3842
{
39-
// Photo capture failed or was cancelled
43+
// Set the image source to the selected bitmap
44+
BitmapImage bitmapImage = new BitmapImage();
45+
await bitmapImage.SetSourceAsync(fileStream);
46+
iCapturedImage.Source = bitmapImage;
4047
}
41-
CaptureVideo();
48+
} else
49+
{
50+
// Photo capture failed or was cancelled
4251
}
52+
4353
```
4454

4555
### [C++/WinRT](#tab/cpp)
@@ -62,6 +72,13 @@ auto photo = co_await cameraUI.CaptureFileAsync(CameraCaptureUIMode::Photo);
6272
if (photo != nullptr)
6373
{
6474
// 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+
6582
}
6683
else
6784
{
@@ -71,4 +88,93 @@ else
7188
7289
---
7390
91+
The following example demonstrates launching the **CameraCaptureUI** for video capture specifying the maximum resolution 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+
```
74179

180+
---

0 commit comments

Comments
 (0)