■ MediaPlayerElement 엘리먼트에서 MediaCapture 객체를 사용해 카메라 캡처하는 방법을 보여준다.
▶ ExpandToFillGrid.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 |
using Windows.Foundation; using Microsoft.UI.Xaml.Controls; namespace TestProject { /// <summary> /// 전체 확장 그리드 /// </summary> public class ExpandToFillGrid : Grid { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Protected #region 측정하기 (오버라이드) - MeasureOverride(availableSize) /// <summary> /// 측정하기 (오버라이드) /// </summary> /// <param name="availableSize">이용 가능 크기</param> /// <returns>측정 크기</returns> protected override Size MeasureOverride(Size availableSize) { Size desiredSize = base.MeasureOverride(new Size(availableSize.Width, 100)); return desiredSize; } #endregion } } |
▶ MainWindow.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 45 46 47 48 49 50 |
<?xml version="1.0" encoding="utf-8"?> <Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:TestProject" Title="TestProject"> <Grid Margin="10"> <Grid.ColumnDefinitions> <ColumnDefinition Width="*" /> <ColumnDefinition Width="Auto" /> </Grid.ColumnDefinitions> <Grid MinWidth="400" MinHeight="300" RowSpacing="10" ColumnSpacing="4" RowDefinitions="Auto,*" ColumnDefinitions="*,100"> <TextBlock Name="displayNameTextBlock" Grid.Row="0" Grid.Column="0" VerticalAlignment="Center" /> <MediaPlayerElement Name="mediaPlayerElement" Grid.Row="1" Grid.Column="0" Stretch="Uniform" AutoPlay="True" /> <TextBlock Name="captureTextBlock" Grid.Row="0" Grid.Column="1" VerticalAlignment="Center" Text="Captured : " Visibility="Collapsed" /> <Grid Name="captureGrid" Grid.Row="1" Grid.Column="1" Margin="5 0 5 0"> <ScrollViewer VerticalScrollMode="Enabled"> <StackPanel Name="captureImageStackPanel" Spacing="2" /> </ScrollViewer> </Grid> </Grid> <StackPanel Grid.Column="1" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="5 0 0 0"> <ToggleSwitch Name="mirrorTogglePreviewSwitch" Header="Mirror preview" IsOn="False" ToolTipService.ToolTip="Mirrors only the preview, not captured photos" /> <Button Name="captureButton" Content="Capture Photo" /> </StackPanel> </Grid> </Window> |
▶ MainWindow.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 |
using System; using System.Collections.Generic; using System.Linq; using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Controls; using Microsoft.UI.Xaml.Media; using Microsoft.UI.Xaml.Media.Imaging; using Windows.Foundation; using Windows.Media.Capture; using Windows.Media.Capture.Frames; using Windows.Media.Core; using Windows.Media.MediaProperties; using Windows.Storage.Streams; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public sealed partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 미디어 프레임 소스 그룹 /// </summary> private MediaFrameSourceGroup mediaFrameSourceGroup; /// <summary> /// 미디어 캡처 /// </summary> private MediaCapture mediaCapture; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); StartMediaPlayerElement(); ExpandToFillGrid expandToFillGrid = new ExpandToFillGrid(); ScrollViewer scrollViewer = this.captureGrid.Children[0] as ScrollViewer; this.captureGrid.Children.Remove(scrollViewer); this.captureGrid.Children.Add(expandToFillGrid); expandToFillGrid.Children.Add(scrollViewer); this.mirrorTogglePreviewSwitch.Toggled += mirrorPreviewToggleSwitch_Toggled; this.captureButton.Click += captureButton_Click; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region Mirror preview 토글 스위치 토글시 처리하기 - mirrorPreviewToggleSwitch_Toggled(sender, e) /// <summary> /// Mirror preview 토글 스위치 토글시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void mirrorPreviewToggleSwitch_Toggled(object sender, RoutedEventArgs e) { if(this.mirrorTogglePreviewSwitch.IsOn) { this.mediaPlayerElement.RenderTransform = new ScaleTransform() { ScaleX = -1 }; this.mediaPlayerElement.RenderTransformOrigin = new Point(0.5, 0.5); } else { this.mediaPlayerElement.RenderTransform = null; } } #endregion #region Capture 버튼 클릭시 처리하기 - captureButton_Click(sender, e) /// <summary> /// Capture 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private async void captureButton_Click(object sender, RoutedEventArgs e) { ImageEncodingProperties properties = ImageEncodingProperties.CreateJpeg(); InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream(); await this.mediaCapture.CapturePhotoToStreamAsync(properties, stream); stream.Seek(0); BitmapImage bitmapImage = new BitmapImage(); await bitmapImage.SetSourceAsync(stream); Image image = new Image() { Source = bitmapImage }; this.captureImageStackPanel.Children.Insert(0, image); this.captureTextBlock.Visibility = Visibility.Visible; } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 미디어 플레이어 엘리먼트 시작하기 - StartMediaPlayerElement() /// <summary> /// 미디어 플레이어 엘리먼트 시작하기 /// </summary> async private void StartMediaPlayerElement() { IReadOnlyList<MediaFrameSourceGroup> mediaFrameSourceGroupList = await MediaFrameSourceGroup.FindAllAsync(); if(mediaFrameSourceGroupList.Count == 0) { this.displayNameTextBlock.Text = "No camera devices found."; return; } this.mediaFrameSourceGroup = mediaFrameSourceGroupList.First(); this.displayNameTextBlock.Text = "Viewing : " + this.mediaFrameSourceGroup.DisplayName; this. mediaCapture = new MediaCapture(); MediaCaptureInitializationSettings settings = new MediaCaptureInitializationSettings() { SourceGroup = this.mediaFrameSourceGroup, SharingMode = MediaCaptureSharingMode.SharedReadOnly, StreamingCaptureMode = StreamingCaptureMode.Video, MemoryPreference = MediaCaptureMemoryPreference.Cpu }; await this.mediaCapture.InitializeAsync(settings); MediaFrameSource mediaFrameSource = this.mediaCapture.FrameSources[this.mediaFrameSourceGroup.SourceInfos[0].Id]; this.mediaPlayerElement.Source = MediaSource.CreateFromMediaFrameSource(mediaFrameSource); } #endregion } } |