■ CameraHelper 클래스를 사용해 카메라 이미지를 캡처하는 방법을 보여준다.
※ 아래 코드를 실행하면 카메라 이미지가 캡처가 되는데 버튼을 자주 클릭해야 했다.
▶ MainPage.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
<?xml version="1.0" encoding="utf-8"?> <Page x:Class="TestProject.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:muxc="using:Microsoft.UI.Xaml.Controls" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" FontFamily="나눔고딕코딩" FontSize="16"> <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center" Orientation="Vertical" Spacing="10"> <ComboBox Name="frameSourceGroupComboBox" HorizontalAlignment="Center" MinWidth="200" Header="Frame source group"> <ComboBox.ItemTemplate> <DataTemplate> <TextBlock Text="{Binding DisplayName}" /> </DataTemplate> </ComboBox.ItemTemplate> </ComboBox> <muxc:InfoBar Name="inforBar" Title="Error" Severity="Error" IsOpen="False" /> <Border BorderThickness="1" BorderBrush="{ThemeResource CardStrokeColorDefaultBrush}" CornerRadius="{StaticResource ControlCornerRadius}" Background="{ThemeResource CardBackgroundFillColorDefaultBrush}"> <Image Name="currentImage" MinWidth="360" Height="300" /> </Border> <Button Name="captureButton" Style="{StaticResource AccentButtonStyle}" HorizontalAlignment="Center" Content="Capture video frame" /> </StackPanel> </Page> |
▶ MainPage.xaml.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
using System; using System.Collections.Generic; using Windows.Graphics.Imaging; using Windows.Media; using Windows.Media.Capture.Frames; using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Controls; using Microsoft.UI.Xaml.Media.Imaging; using CommunityToolkit.WinUI.Helpers; namespace TestProject; /// <summary> /// 메인 페이지 /// </summary> public sealed partial class MainPage : Page { //////////////////////////////////////////////////////////////////////////////////////////////////// Feld ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 카메라 헬퍼 /// </summary> private CameraHelper cameraHelper; /// <summary> /// 현재 비디오 프레임 /// </summary> private VideoFrame currentVideoFrame; /// <summary> /// 소프트웨어 비트맵 소스 /// </summary> private SoftwareBitmapSource softwareBitmapSource; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainPage() /// <summary> /// 생성자 /// </summary> public MainPage() { InitializeComponent(); Loaded += Page_Loaded; Unloaded += Page_Unloaded; this.captureButton.Click += captureButton_Click; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 페이지 로드시 처리하기 - Page_Loaded(sender, e) /// <summary> /// 페이지 로드시 처리하기 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private async void Page_Loaded(object sender, RoutedEventArgs e) { Loaded -= Page_Loaded; this.softwareBitmapSource = new SoftwareBitmapSource(); this.currentImage.Source = this.softwareBitmapSource; IReadOnlyList<MediaFrameSourceGroup> mediaFrameSourceGroupList = await CameraHelper.GetFrameSourceGroupsAsync(); if(this.cameraHelper == null) { this.cameraHelper = new CameraHelper(); } CameraHelperResult result = await cameraHelper.InitializeAndStartCaptureAsync(); if(result == CameraHelperResult.Success) { this.cameraHelper.FrameArrived += cameraHelper_FrameArrived!; this.frameSourceGroupComboBox.SelectionChanged += frameSourceGroupComboBox_SelectionChanged; this.frameSourceGroupComboBox.ItemsSource = mediaFrameSourceGroupList; this.frameSourceGroupComboBox.SelectedIndex = 0; } SetUIControls(result); } #endregion #region 페이지 언로드시 처리하기 - Page_Unloaded(sender, e) /// <summary> /// 페이지 언로드시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private async void Page_Unloaded(object sender, RoutedEventArgs e) { Unloaded -= Page_Unloaded; if(this.frameSourceGroupComboBox != null) { this.frameSourceGroupComboBox.SelectionChanged -= frameSourceGroupComboBox_SelectionChanged; } if(this.cameraHelper != null) { this.cameraHelper.FrameArrived -= cameraHelper_FrameArrived; await this.cameraHelper.CleanUpAsync(); this.cameraHelper = null!; } if(this.softwareBitmapSource != null) { this.softwareBitmapSource.Dispose(); } } #endregion #region 카메라 헬퍼 프레임 도착시 처리하기 - cameraHelper_FrameArrived(sender, e) /// <summary> /// 카메라 헬퍼 프레임 도착시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void cameraHelper_FrameArrived(object sender, FrameEventArgs e) { this.currentVideoFrame = e.VideoFrame; } #endregion #region 프레임 소스 그룹 콤보 박스 선택 변경시 처리하기 - frameSourceGroupComboBox_SelectionChanged(sender, e) /// <summary> /// 프레임 소스 그룹 콤보 박스 선택 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private async void frameSourceGroupComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { if(this.frameSourceGroupComboBox.SelectedItem is MediaFrameSourceGroup mediaFrameSourceGroup && this.cameraHelper != null) { this.cameraHelper.FrameSourceGroup = mediaFrameSourceGroup; CameraHelperResult result = await cameraHelper.InitializeAndStartCaptureAsync(); SetUIControls(result); } } #endregion #region Capture video frame 버튼 클릭시 처리하기 - captureButton_Click(sender, e) /// <summary> /// Capture video frame 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private async void captureButton_Click(object sender, RoutedEventArgs e) { SoftwareBitmap softwareBitmap = this.currentVideoFrame?.SoftwareBitmap; if(softwareBitmap != null) { if(softwareBitmap.BitmapPixelFormat != BitmapPixelFormat.Bgra8 || softwareBitmap.BitmapAlphaMode == BitmapAlphaMode.Straight) { softwareBitmap = SoftwareBitmap.Convert(softwareBitmap, BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied); } if(this.softwareBitmapSource != null) { await softwareBitmapSource.SetBitmapAsync(softwareBitmap); } } } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region UI 컨트롤 설정하기 - SetUIControls(result) /// <summary> /// UI 컨트롤 설정하기 /// </summary> /// <param name="result">카메라 헬퍼 결과</param> private void SetUIControls(CameraHelperResult result) { bool success = result == CameraHelperResult.Success; if(!success) { this.currentVideoFrame = null!; } this.inforBar.Title = result.ToString(); this.inforBar.IsOpen = !success; this.captureButton.IsEnabled = success; this.currentImage.Opacity = success ? 1 : 0.5; } #endregion } |