첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

TestSolution.zip
다운로드

[TestLibrary 프로젝트]

▶ CameraControl.xaml

<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>

 

728x90

 

▶ CameraControl.xaml.cs

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
    }
}

 

300x250

 

[TestProject 프로젝트]

▶ MainWindow.xaml

<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

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
    }
}
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요