■ 웹 카메라 사용하기

------------------------------------------------------------------------------------------------------------------------

※ 웹 카메라 캡처 부분을 캡처할 수 없었다.


TestSolution.zip


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

    MinWidth="400"

    MinHeight="300"

    Title="웹 카메라 사용하기"

    FontFamily="나눔고딕코딩"

    FontSize="16">

    <Grid Margin="10">

        <Grid.RowDefinitions>

            <RowDefinition Height="*"    />

            <RowDefinition Height="10"   />

            <RowDefinition Height="Auto" />

        </Grid.RowDefinitions>

            <Border Grid.Row="0"

                BorderThickness="1"

                BorderBrush="Black">

                <library:WebCameraControl x:Name="webCameraControl" />

            </Border>

        <StackPanel Grid.Row="2"

            Orientation="Horizontal"

            HorizontalAlignment="Center"

            VerticalAlignment="Center">

            <ComboBox x:Name="videoCaptureDeviceComboBox"

                Margin="0 5 0 5"

                Width="200"

                Height="30"

                VerticalContentAlignment="Center"

                DisplayMemberPath="Name" />

            <Button x:Name="startButton"

                Margin="20 5 0 5"

                Width="100"

                Height="30"

                IsEnabled="True"

                Content="시작하기" />

            <Button x:Name="stopButton"

                Margin="10 5 0 5"

                Width="100"

                Height="30"

                Content="중단하기"

                IsEnabled="{Binding Path=IsCapturing, ElementName=webCameraControl}" />

            <Button x:Name="imageButton"

                Margin="10 5 0 5"

                Width="100"

                Height="30"

                Content="이미지"

                IsEnabled="{Binding Path=IsCapturing, ElementName=webCameraControl}" />

        </StackPanel>

    </Grid>

</Window>

 

 

MainWindow.xaml.cs

 

 

using Microsoft.Win32;

using System.Windows;

using System.Windows.Controls;

 

using TestLibrary;

 

namespace TestProject

{

    /// <summary>

    /// 메인 윈도우

    /// </summary>

    public partial class MainWindow

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region 생성자 - MainWindow()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainWindow()

        {

            InitializeComponent();

 

            this.videoCaptureDeviceComboBox.ItemsSource = this.webCameraControl.GetVideoCaptureDeviceList();

 

            if(this.videoCaptureDeviceComboBox.Items.Count > 0)

            {

                this.videoCaptureDeviceComboBox.SelectedItem = this.videoCaptureDeviceComboBox.Items[0];

            }

 

            this.videoCaptureDeviceComboBox.SelectionChanged += videoCaptureDeviceComboBox_SelectionChanged;

            this.startButton.Click                           += startButton_Click;

            this.stopButton.Click                            += stopButton_Click;

            this.imageButton.Click                           += imageButton_Click;

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Private

 

        #region 비디오 캡처 장치 콤보 박스 선택 변경시 처리하기 - videoCaptureDeviceComboBox_SelectionChanged(sender, e)

 

        /// <summary>

        /// 비디오 캡처 장치 콤보 박스 선택 변경시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void videoCaptureDeviceComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)

        {

            this.startButton.IsEnabled = e.AddedItems.Count > 0;

        }

 

        #endregion

        #region 시작하기 버튼 클릭시 처리하기 - startButton_Click(sender, e)

 

        /// <summary>

        /// 시작하기 버튼 클릭시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void startButton_Click(object sender, RoutedEventArgs e)

        {

            VideoCaptureDevice videoCaptureDevice = (VideoCaptureDevice)this.videoCaptureDeviceComboBox.SelectedItem;

 

            this.webCameraControl.StartCapture(videoCaptureDevice);

 

            this.startButton.IsEnabled = false;

        }

 

        #endregion

        #region 중단하기 버튼 클릭시 처리하기 - startButton_Click(sender, e)

 

        /// <summary>

        /// 중단하기 버튼 클릭시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void stopButton_Click(object sender, RoutedEventArgs e)

        {

            this.webCameraControl.StopCapture();

 

            this.startButton.IsEnabled = true;

        }

 

        #endregion

        #region 이미지 버튼 클릭시 처리하기 - startButton_Click(sender, e)

 

        /// <summary>

        /// 이미지 버튼 클릭시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void imageButton_Click(object sender, RoutedEventArgs e)

        {

            SaveFileDialog saveFileDialog = new SaveFileDialog { Filter = "비트맵 이미지|*.bmp" };

 

            if(saveFileDialog.ShowDialog() == true)

            {

                this.webCameraControl.GetCurrentImage().Save(saveFileDialog.FileName);

            }

        }

 

        #endregion

    }

}

 

------------------------------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker

댓글을 달아 주세요