티스토리 뷰

반응형

실버라이트4.0 부터 기본적으로 웹켐과 오디오를 기본으로 사용할 수 있는 기능이 추가되어있다.
이 기능을 이용하는 방법을 알아보도록 하자..그런데, 왜 뜬금없이 웹켐에 대한 이야기를 하는 것일까? 다음 강좌에서 촬영한 사진을 WCF RIA Service로 서버로 전송하는 것을 올릴려고 하기 때문이다.


1. 화면보기

일단 기존 화면에 버튼을 하나 추가하고 버튼을 클릭하면 차일드 윈도우를 출력하고, 그 곳에서 웹켐 활성화를 시키고 사진 촬영을 하게 된다. 촬영시작을 누르면 위의 화면 처럼 실버라이트에서 카메라와 오디오에 엑세스를 허용할 것인지를 물어본다.(이렇게 나오도록 만든다고..몇시간 삽질을....나만 어렵게 적용했다는 생각이..쿨럭;;)


WebCam_Child.xaml

<controls:ChildWindow x:Class="SL4_RIA_Sample.WebCam_Child"
           xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
           xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
           xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"
           Width="400" Height="300"
           Title="WebCam_Child" Loaded="ChildWindow_Loaded">
    <Grid x:Name="LayoutRoot" Margin="2">
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>

        <!--이미지가 보이는 곳~-->
        <Rectangle Grid.Row="0" Name="rectVideo" Fill="Black" Margin="4" />

        <Button x:Name="CancelButton" Content="닫기" Click="CancelButton_Click" Width="75" Height="23" HorizontalAlignment="Right" Margin="0,12,0,0" Grid.Row="1" />
        <Button x:Name="OKButton" Content="촬영" Click="OKButton_Click" Width="75" Height="23" HorizontalAlignment="Right" Margin="0,12,79,0" Grid.Row="1" />
        <Button x:Name="StartButton" Content="촬영시작" Click="StartButton_Click" Width="75" Height="23" HorizontalAlignment="Right" Margin="0,12,158,0" Grid.Row="1" />
    </Grid>
</controls:ChildWindow>


WebCam_Child.xaml.cs

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace SL4_RIA_Sample
{
    public partial class WebCam_Child : ChildWindow
    {
        private CaptureSource _capture;

        public WebCam_Child()
        {
            InitializeComponent();
        }

        private void ChildWindow_Loaded(object sender, RoutedEventArgs e)
        {
            //캡춰소스 인스턴스
            _capture = new CaptureSource();
            _capture.CaptureImageCompleted += new EventHandler<CaptureImageCompletedEventArgs>(_capture_CaptureImageCompleted);
        }

        void _capture_CaptureImageCompleted(object sender, CaptureImageCompletedEventArgs e)
        {
            //이미지 캡춰가 완료되면 여기로
            WriteableBitmap wb = e.Result;
            //화면에 촬영된 이미지 보여주기
            if (_capture != null)
            {
                _capture.Stop();
                ImageBrush image = new ImageBrush();
                image.ImageSource = wb;
                rectVideo.Fill = image;
            }
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            //캡춰 시작
            _capture.CaptureImageAsync();
            //this.DialogResult = true;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void StartButton_Click(object sender, RoutedEventArgs e)
        {
            Button btn = ((Button)sender);

            if (_capture != null)
            {
                //일단 동작 정지
                _capture.Stop();

                if (btn.Content.ToString() == "촬영시작")
                {
                    //기본 비디오 디바이스를 가지고옴
                    _capture.VideoCaptureDevice = CaptureDeviceConfiguration.GetDefaultVideoCaptureDevice();
                    //기본 오디오 디바이스를 가지고옴
                    _capture.AudioCaptureDevice = CaptureDeviceConfiguration.GetDefaultAudioCaptureDevice();

                    //비디오 브러쉬 만들고
                    VideoBrush videoBrush = new VideoBrush();
                    videoBrush.Stretch = Stretch.Uniform;
                    videoBrush.SetSource(_capture);

                    //xaml에 있는 사각형을 비디오브러쉬로 채운다
                    rectVideo.Fill = videoBrush;
                    // 사용자에게 인증을 받는 부분 - 이 부분은 폼로드 이벤트에 넣치 않는다. 별도의 이벤트 함수에서 처리하도록

                    // 해야 한다, 않그러면, 사용자에게 인증 받는 창이 뜨지를 안아서 한참을 헤메는 사태가 발생한다는..

                    if (CaptureDeviceConfiguration.AllowedDeviceAccess || CaptureDeviceConfiguration.RequestDeviceAccess())
                    {
                        _capture.Start();
                        btn.Content = "촬영종료";
                    }
                }
                else
                {
                    btn.Content = "촬영시작";
                }
            }
        }
    }
}

2. 다음에는
이 소스에 추가해서 촬영한 사진을 업로드 하는 것에 대해서 적어보도록 하겠다.

반응형
댓글