■ CameraControl 클래스에서 카메라를 사용하는 방법을 보여준다. (기능 개선)
[TestLibrary 프로젝트]
▶ CameraControl.xaml
1 2 3 4 5 6 7 8 9 10 11 |
<UserControl x:Class="TestLibrary.CameraControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors"> <dxe:CameraControl Name="cameraControl" Stretch="Fill" ShowSettingsButton="False" AllowAutoStart="False" /> </UserControl> |
▶ CameraControl.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 |
using System.Collections.Generic; using System.Linq; using System.Windows.Controls; using System.Windows.Media; using DevExpress.Xpf.Editors; namespace TestLibrary { /// <summary> /// 카메라 컨트롤 /// </summary> public partial class CameraControl : UserControl { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 캡처 여부 /// </summary> private bool isCapturing = false; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 장치명 - DeviceName /// <summary> /// 장치명 /// </summary> public string DeviceName { get { return this.cameraControl.DeviceInfo.Name; } set { DeviceInfo deviceInfo = GetDeviceInfo(value); this.cameraControl.DeviceInfo = deviceInfo; } } #endregion #region 캡처 여부 - IsCapturing /// <summary> /// 캡처 여부 /// </summary> public bool IsCapturing { get { return this.isCapturing; } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - CameraControl() /// <summary> /// 생성자 /// </summary> public CameraControl() { InitializeComponent(); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 장치명 리스트 구하기 - GetDeviceNameList() /// <summary> /// 장치명 리스트 구하기 /// </summary> /// <returns></returns> public static List<string> GetDeviceNameList() { List<DeviceInfo> deviceInfoList = GetDeviceInfoList(); List<string> list = new List<string>(); foreach(DeviceInfo deviceInfo in deviceInfoList) { list.Add(deviceInfo.Name); } return list; } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// private #region 장치 정보 리스트 구하기 - GetDeviceInfoList() /// <summary> /// 장치 정보 리스트 구하기 /// </summary> /// <returns>장치 정보 리스트</returns> private static List<DeviceInfo> GetDeviceInfoList() { DevExpress.Xpf.Editors.CameraControl control = new DevExpress.Xpf.Editors.CameraControl(); List<DeviceInfo> deviceInfoList = control.GetAvailableDevices().ToList(); return deviceInfoList; } #endregion #region 장치 정보 구하기 - GetDeviceInfo(deviceName) /// <summary> /// 장치 정보 구하기 /// </summary> /// <param name="deviceName">장치명</param> /// <returns>장치 정보</returns> private static DeviceInfo GetDeviceInfo(string deviceName) { if(string.IsNullOrEmpty(deviceName)) { return null; } List<DeviceInfo> deviceInfoList = GetDeviceInfoList(); DeviceInfo deviceInfo = deviceInfoList.Where(d => d.Name == deviceName).FirstOrDefault(); return deviceInfo; } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Instance //////////////////////////////////////////////////////////////////////////////// Public #region 시작하기 - Start() /// <summary> /// 시작하기 /// </summary> public void Start() { if(this.isCapturing) { return; } this.cameraControl.Start(); this.isCapturing = true; } #endregion #region 중단하기 - Stop() /// <summary> /// 중단하기 /// </summary> public void Stop() { if(!this.isCapturing) { return; } this.cameraControl.Stop(); this.isCapturing = false; } #endregion #region 이미지 구하기 - GetImage() /// <summary> /// 이미지 구하기 /// </summary> /// <returns>이미지 소스</returns> public ImageSource GetImage() { return this.cameraControl.TakeSnapshot(); } #endregion } } |
[TestProject 프로젝트]
▶ 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 |
<Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:library="clr-namespace:TestLibrary;assembly=TestLibrary" Width="800" Height="600" Title="CameraControl 클래스 : 카메라 사용하기 (기능 개선)" FontFamily="나눔고딕코딩" FontSize="16"> <Grid Margin="10"> <Grid.RowDefinitions> <RowDefinition Height="*" /> <RowDefinition Height="Auto" /> </Grid.RowDefinitions> <Border Grid.Row="0" HorizontalAlignment="Center" VerticalAlignment="Center" BorderThickness="1" BorderBrush="Black"> <library:CameraControl Name="cameraControl" Width="600" Height="480" /> </Border> <StackPanel Grid.Row="1" HorizontalAlignment="Right" VerticalAlignment="Center" Orientation="Horizontal"> <Button Name="startButton" Width="100" Height="30" Content="시작" /> <Button Name="imageButton" Margin="10 0 0 0" Width="100" Height="30" Content="이미지" /> </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 |
using System.ComponentModel; using System.IO; using System.Linq; using System.Windows; using System.Windows.Media; using System.Windows.Media.Imaging; using TestLibrary; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); Closing += Window_Closing; this.startButton.Click += startButton_Click; this.imageButton.Click += imageButton_Click; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 윈도우 닫을 경우 처리하기 - Window_Closing(sender, e) /// <summary> /// 윈도우 닫을 경우 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void Window_Closing(object sender, CancelEventArgs e) { this.cameraControl.Stop(); } #endregion #region 시작 버튼 클릭시 처리하기 - startButton_Click(sender, e) /// <summary> /// 시작 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void startButton_Click(object sender, RoutedEventArgs e) { if(this.startButton.Content.ToString() == "시작") { this.cameraControl.DeviceName = CameraControl.GetDeviceNameList().First(); this.cameraControl.Start(); this.startButton.Content = "중단"; } else { this.cameraControl.Stop(); this.startButton.Content = "시작"; } } #endregion #region 이미지 버튼 클릭시 처리하기 - imageButton_Click(sender, e) /// <summary> /// 이미지 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void imageButton_Click(object sender, RoutedEventArgs e) { ImageSource imageSource = this.cameraControl.GetImage(); PngBitmapEncoder encoder = new PngBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(imageSource as BitmapImage)); using(FileStream stream = new FileStream("d:\\sample.png", FileMode.Create, FileAccess.Write, FileShare.None)) { encoder.Save(stream); } } #endregion } } |