■ 웹 카메라 사용하기

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


TestApplication.zip


MainPage.xaml

 

 

<Page x:Class="TestApplication.MainPage"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:local="using:TestApplication"

    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

    mc:Ignorable="d">

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

        <Grid.ChildrenTransitions>

            <TransitionCollection>

                <EntranceThemeTransition />

            </TransitionCollection>

        </Grid.ChildrenTransitions>

        <Grid.RowDefinitions>

            <RowDefinition Height="Auto" />

            <RowDefinition Height="Auto" />

            <RowDefinition Height="Auto" />

            <RowDefinition Height="Auto" />

            <RowDefinition Height="Auto" />

            <RowDefinition Height="Auto" />

        </Grid.RowDefinitions>

        <TextBlock Grid.Row="0"

            Style="{StaticResource HeaderTextBlockStyle}"

            HorizontalAlignment="Center"

            VerticalAlignment="Center"

            IsHitTestVisible="false"

            TextWrapping="NoWrap"

            Text="웹 카메라 테스트" />

        <StackPanel Grid.Row="1"

            HorizontalAlignment="Center"

            VerticalAlignment="Center"

            Orientation="Horizontal">

            <Button x:Name="initializeVideoButton"

                HorizontalAlignment="Left"

                Margin="10"

                Width="180"

                Content="비디오 초기화" />

            <Button x:Name="initializeAudioButton"

                Margin="10"

                Width="120"

                Content="오디오 초기화" />

            <Button x:Name="closeCameraButton"

                Margin="10"

                Width="120"

                Content="카메라 닫기" />

        </StackPanel>

        <StackPanel Grid.Row="2"

            HorizontalAlignment="Center"

            VerticalAlignment="Center"

            Orientation="Horizontal">

            <Button x:Name="takePhotoButton"

                Margin="10"

                Width="120"

                IsEnabled="False"

                Content="사진 찍기" />

            <Button x:Name="recordVideoButton"

                Margin="10"

                Width="150"

                IsEnabled="False"

                Content="비디오 기록 시작" />

            <Button x:Name="recordAudioButton"

                Margin="10"

                Width="150"

                IsEnabled="False"

                Content="오디오 기록 시작" />

        </StackPanel>

        <StackPanel Grid.Row="3"

            HorizontalAlignment="Center"

            VerticalAlignment="Center"

            Orientation="Horizontal">

            <StackPanel Margin="10">

                <TextBlock Name="previewTextBlock"

                    HorizontalAlignment="Center"

                    Height="30"

                    Margin="10"

                    Text="미리 보기" />

                <Canvas Name="previewCanvas"

                    Width="300"

                    Height="300"

                    Background="Gray"

                    Visibility="Visible">

                    <CaptureElement x:Name="previewCaptureElement"

                        HorizontalAlignment="Left"

                        Width="{Binding ElementName=previewCanvas, Path=ActualWidth}"

                        Height="{Binding ElementName=previewCanvas, Path=ActualHeight}"

                        Visibility="Visible" />

                </Canvas>

            </StackPanel>

            <StackPanel Margin="10">

                <TextBlock Name="lastCapturedPhotoTextBlock"

                    HorizontalAlignment="Center"

                    Margin="10"

                    Height="30"

                    Visibility="Visible"

                    Text="마지막 촬영 사진" />

                <Canvas Name="lastCapturedPhotoCanvas"

                    Width="300"

                    Height="300"

                    Background="Gray"

                    Visibility="Visible">

                    <Image x:Name="lastCapturedPhotoImage"

                        Width="{Binding ElementName=lastCapturedPhotoCanvas, Path=ActualWidth}"

                        Height="{Binding ElementName=lastCapturedPhotoCanvas, Path=ActualHeight}"

                        Visibility="Visible" />

                </Canvas>

            </StackPanel>

            <StackPanel Margin="10">

                <TextBlock Name="lastRecordedVideoTextBlock"

                    HorizontalAlignment="Center"

                    Margin="10"

                    Height="30"

                    Visibility="Visible"

                    Text="마지막 기록 비디오" />

                <Canvas Name="lastRecordedVideoCanvas"

                    Width="300"

                    Height="300"

                    Background="Gray"

                    Visibility="Visible">

                    <MediaElement x:Name="lastRecordedVideoMediaElement"

                        Width="{Binding ElementName=lastRecordedVideoCanvas, Path=ActualWidth}"

                        Height="{Binding ElementName=lastRecordedVideoCanvas, Path=ActualHeight}"

                        Visibility="Visible" />

                </Canvas>

            </StackPanel>

            <StackPanel>

                <Canvas

                    Margin="0"

                    Width="0"

                    Height="0">

                    <MediaElement x:Name="playbackMediaElement"

                        Width="0"

                        Height="0" />

                </Canvas>

            </StackPanel>

        </StackPanel>

        <TextBlock Grid.Row="4"

            HorizontalAlignment="Center"

            VerticalAlignment="Center"

            Margin="10"

            Text="status" />

        <TextBox x:Name="statusTextBlock" Grid.Row="5"

            HorizontalAlignment="Center"

            VerticalAlignment="Center"

            Margin="10"

            Height="100"

            Width="600"

            TextWrapping="Wrap"

            IsReadOnly="True" />

    </Grid>

</Page>

 

 

MainPage.xaml.cs

 

 

using System;

using Windows.Media;

using Windows.Media.Capture;

using Windows.Media.MediaProperties;

using Windows.Storage;

using Windows.Storage.Streams;

using Windows.UI.Core;

using Windows.UI.Xaml;

using Windows.UI.Xaml.Controls;

using Windows.UI.Xaml.Media.Imaging;

 

namespace TestApplication

{

    /// <summary>

    /// 메인 페이지

    /// </summary>

    public sealed partial class MainPage : Page

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Enumeration

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

 

        #region 액션 - Action

 

        /// <summary>

        /// 액션

        /// </summary>

        private enum Action

        {

            /// <summary>

            /// 활성화

            /// </summary>

            ENABLED,

 

            /// <summary>

            /// 비활성화

            /// </summary>

            DISABLED

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Field

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

 

        #region Field

 

        /// <summary>

        /// 미디어 캡처 파일

        /// </summary>

        private MediaCapture mediaCapture;

 

 

        /// <summary>

        /// 사진 파일명

        /// </summary>

        private readonly string PHOTO_FILE_NAME = "photo.png";

 

        /// <summary>

        /// 비디오 파일명

        /// </summary>

        private readonly string VIDEO_FILE_NAME = "video.mp4";

 

        /// <summary>

        /// 오디오 파일명

        /// </summary>

        private readonly string AUDIO_FILE_NAME = "audio.mp3";

 

        /// <summary>

        /// 사진 저장소 파일

        /// </summary>

        private StorageFile photoStorageFile;

 

        /// <summary>

        /// 비디오 저장소 파일

        /// </summary>

        private StorageFile videoStorageFile;

 

        /// <summary>

        /// 오디오 저장소 파일

        /// </summary>

        private StorageFile audioStorageFile;

 

 

        /// <summary>

        /// 미리보기 여부

        /// </summary>

        private bool isPreviewing;

 

        /// <summary>

        /// 레코딩 여부

        /// </summary>

        private bool isRecording;

 

        #endregion

 

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

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

 

        #region 생성자 - MainPage()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainPage()

        {

            this.InitializeComponent();

 

            SetInitializeButtonVisibility(Action.ENABLED);

            SetVideoButtonVisibility(Action.DISABLED);

            SetAudioButtonVisibility(Action.DISABLED);

 

            this.isPreviewing = false;

            this.isRecording  = false;

 

            this.initializeVideoButton.Click += initializeVideoButton_Click;

            this.initializeAudioButton.Click += initializeAudioButton_Click;

            this.closeCameraButton.Click     += closeCameraButton_Click;

            this.takePhotoButton.Click       += takePhotoButton_Click;

            this.recordVideoButton.Click     += recordVideoButton_Click;

            this.recordAudioButton.Click     += recordAudioButton_Click;

        }

 

        #endregion

 

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

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

        //////////////////////////////////////////////////////////////////////////////// Event

 

        #region 비디오 초기화 버튼 클릭시 처리하기 - initializeVideoButton_Click(sender, e)

 

        /// <summary>

        /// 비디오 초기화 버튼 클릭시 처리하기

        /// </summary>

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

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

        private async void initializeVideoButton_Click(object sender, RoutedEventArgs e)

        {

            SetInitializeButtonVisibility(Action.DISABLED);

            SetVideoButtonVisibility(Action.DISABLED);

            SetAudioButtonVisibility(Action.DISABLED);

 

            try

            {

                if(this.mediaCapture != null)

                {

                    if(this.isPreviewing)

                    {

                        await this.mediaCapture.StopPreviewAsync();

 

                        this.lastCapturedPhotoImage.Source = null;

 

                        this.lastRecordedVideoMediaElement.Source = null;

 

                        this.isPreviewing = false;

                    }

 

                    if(isRecording)

                    {

                        await this.mediaCapture.StopRecordAsync();

 

                        this.isRecording = false;

 

                        this.recordVideoButton.Content = "비디오 기록 시작";

                        this.recordAudioButton.Content = "오디오 기록 시작";

                    }

 

                    this.mediaCapture.Dispose();

 

                    this.mediaCapture = null;

                }

 

 

                this.statusTextBlock.Text = "비디오 캡처를 위한 카메라 초기화 중...";

 

 

                this.mediaCapture = new MediaCapture();

 

                await this.mediaCapture.InitializeAsync();

 

                this.statusTextBlock.Text = "비디오 기록을 위한 장치가 성공적으로 초기화되었습니다!";

 

                this.mediaCapture.Failed += mediaCapture_Failed;

 

                this.mediaCapture.RecordLimitationExceeded += mediaCapture_RecordLimitExceeded;

 

                this.previewCaptureElement.Source = this.mediaCapture;

 

                await this.mediaCapture.StartPreviewAsync();

 

                this.isPreviewing = true;

 

                this.statusTextBlock.Text = "카메라 미리보기가 성공했습니다.";

 

                SetVideoButtonVisibility(Action.ENABLED);

 

                this.initializeAudioButton.IsEnabled = true;

            }

            catch(Exception exception)

            {

                this.statusTextBlock.Text = "비디오 기록을 위한 카메라 초기화를 할 수 없습니다 : " + exception.Message;             

            }

        }

 

        #endregion

        #region 오디오 초기화 버튼 클릭시 처리하기 - initializeAudioButton_Click(sender, e)

 

        /// <summary>

        /// 오디오 초기화 버튼 클릭시 처리하기

        /// </summary>

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

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

        private async void initializeAudioButton_Click(object sender, RoutedEventArgs e)

        {

            SetInitializeButtonVisibility(Action.DISABLED);

            SetVideoButtonVisibility(Action.DISABLED);

            SetAudioButtonVisibility(Action.DISABLED);

 

            try

            {

                if(this.mediaCapture != null)

                {

                    if(this.isPreviewing)

                    {

                        await this.mediaCapture.StopPreviewAsync();

 

                        this.lastCapturedPhotoImage.Source = null;

 

                        this.lastRecordedVideoMediaElement.Source = null;

 

                        this.isPreviewing = false;

                    }

 

                    if(this.isRecording)

                    {

                        await this.mediaCapture.StopRecordAsync();

 

                        this.isRecording = false;

 

                        this.recordVideoButton.Content = "비디오 기록 시작";

                        this.recordAudioButton.Content = "오디오 기록 시작";

                    }

 

                    this.mediaCapture.Dispose();

 

                    this.mediaCapture = null;

                }

 

                this.statusTextBlock.Text = "오디오 캡처를 위한 카메라 초기화 중...";

 

                this.mediaCapture = new MediaCapture();

 

                MediaCaptureInitializationSettings mediaCaptureInitializationSettings = new MediaCaptureInitializationSettings();

 

                mediaCaptureInitializationSettings.StreamingCaptureMode = StreamingCaptureMode.Audio;

                mediaCaptureInitializationSettings.MediaCategory        = MediaCategory.Other;

                mediaCaptureInitializationSettings.AudioProcessing      = AudioProcessing.Default;

 

                await this.mediaCapture.InitializeAsync(mediaCaptureInitializationSettings);

 

                this.statusTextBlock.Text = "오디오 기록을 위한 장치가 성공적으로 초기화되었습니다!" +

                    "\n기록을 시작하기 위해 \'오디오 기록 시작\' 버튼을 누르시기 바랍니다.";

 

                this.mediaCapture.Failed += mediaCapture_Failed;

 

                this.mediaCapture.RecordLimitationExceeded += mediaCapture_RecordLimitExceeded;

 

                SetAudioButtonVisibility(Action.ENABLED);

 

                this.initializeVideoButton.IsEnabled = true;

            }

            catch(Exception exception)

            {

                this.statusTextBlock.Text = "오디오 기록을 위한 카메라 초기화를 할 수 없습니다 : " + exception.Message;                      

            }

        }

 

        #endregion

        #region 카메라 닫기 버튼 클릭시 처리하기 - closeCameraButton_Click(sender, e)

 

        /// <summary>

        /// 카메라 닫기 버튼 클릭시 처리하기

        /// </summary>

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

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

        private void closeCameraButton_Click(object sender, RoutedEventArgs e)

        {

            SetInitializeButtonVisibility(Action.DISABLED);

            SetVideoButtonVisibility(Action.DISABLED);

            SetAudioButtonVisibility(Action.DISABLED);

 

            ProcessCloseCamera();            

        }

 

        #endregion

 

        #region 미디어 캡처 실패시 처리하기 - mediaCapture_Failed(mediaCapture, e)

 

        /// <summary>

        /// 미디어 캡처 실패시 처리하기

        /// </summary>

        /// <param name="mediaCapture">미디어 캡처</param>

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

        private async void mediaCapture_Failed(MediaCapture mediaCapture, MediaCaptureFailedEventArgs e)

        {

            await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>

            {

                try

                {

                    this.statusTextBlock.Text = "미디어 캡처 실패 : " + e.Message;

 

                    if(this.isRecording)

                    {

                        await this.mediaCapture.StopRecordAsync();

 

                        this.statusTextBlock.Text += "\n 기록이 중단되었습니다.";

                    }

                }

                catch(Exception)

                {

                }

                finally

                {

                    SetInitializeButtonVisibility(Action.DISABLED);

                    SetVideoButtonVisibility(Action.DISABLED);

                    SetAudioButtonVisibility(Action.DISABLED);

 

                    this.statusTextBlock.Text += "\n카메라가 연결되어 있는지 체크하시기 바랍니다. 앱을 다시 시작해 주시기 바랍니다.";

                }

            });

        }

 

        #endregion

        #region 미디어 캡처 기록 제한 초과시 처리하기 - mediaCapture_RecordLimitExceeded(mediaCapture)

 

        /// <summary>

        /// 미디어 캡처 기록 제한 초과시 처리하기

        /// </summary>

        /// <param name="mediaCapture">미디어 캡처</param>

        public async void mediaCapture_RecordLimitExceeded(MediaCapture mediaCapture)

        {

            try

            {

                if(this.isRecording)

                {

                    await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>

                    {

                        try

                        {

                            this.statusTextBlock.Text = "최대 기록 시간을 초과해 기록을 중단하고 있습니다.";

 

                            await this.mediaCapture.StopRecordAsync();

 

                            this.isRecording = false;

 

                            this.recordVideoButton.Content = "비디오 기록 시작";

                            this.recordAudioButton.Content = "오디오 기록 시작";

 

                            if(this.mediaCapture.MediaCaptureSettings.StreamingCaptureMode == StreamingCaptureMode.Audio)

                            {

                                this.statusTextBlock.Text = "최대 기록 시간을 초과해 기록을 중단했습니다 : " + this.audioStorageFile.Path;

                            }

                            else

                            {

                                this.statusTextBlock.Text = "최대 기록 시간을 초과해 기록을 중단했습니다 : " + this.videoStorageFile.Path;

                            }

                        }

                        catch(Exception exception)

                        {

                            this.statusTextBlock.Text = exception.Message;

                        }

                    });

                }

            }

            catch(Exception exception)

            {

                this.statusTextBlock.Text = exception.Message;

            }

        }

 

        #endregion

 

        #region 사진 찍기 버튼 클릭시 처리하기 - takePhotoButton_Click(sender, e)

 

        /// <summary>

        /// 사진 찍기 버튼 클릭시 처리하기

        /// </summary>

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

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

        private async void takePhotoButton_Click(object sender, RoutedEventArgs e)

        {

            try

            {

                this.takePhotoButton.IsEnabled   = false;

                this.recordVideoButton.IsEnabled = false;

 

                this.lastCapturedPhotoImage.Source = null;

 

                this.photoStorageFile = await KnownFolders.PicturesLibrary.CreateFileAsync(PHOTO_FILE_NAME,

                    CreationCollisionOption.GenerateUniqueName);

 

                ImageEncodingProperties imageEncodingProperties = ImageEncodingProperties.CreatePng();

 

                await this.mediaCapture.CapturePhotoToStorageFileAsync(imageEncodingProperties, this.photoStorageFile);

 

                this.takePhotoButton.IsEnabled = true;

 

                this.statusTextBlock.Text = "사진 찍기가 성공했습니다 : " + this.photoStorageFile.Path;

 

                IRandomAccessStream randomAccessStream = await this.photoStorageFile.OpenReadAsync();

 

                BitmapImage bitmapImage = new BitmapImage();

 

                bitmapImage.SetSource(randomAccessStream);

 

                this.lastCapturedPhotoImage.Source = bitmapImage;

            }

            catch(Exception exception)

            {

                this.statusTextBlock.Text = exception.Message;

 

                ProcessCloseCamera();

            }

            finally

            {

                this.takePhotoButton.IsEnabled   = true;

                this.recordVideoButton.IsEnabled = true;

            }

        }

 

        #endregion

        #region 비디오 기록 버튼 클릭시 처리하기 - recordVideoButton_Click(sender, e)

 

        /// <summary>

        /// 비디오 기록 버튼 클릭시 처리하기

        /// </summary>

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

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

        private async void recordVideoButton_Click(object sender, RoutedEventArgs e)

        {

            try

            {

                this.takePhotoButton.IsEnabled   = false;

                this.recordVideoButton.IsEnabled = false;

 

                this.lastRecordedVideoMediaElement.Source = null;

 

                if(this.recordVideoButton.Content.ToString() == "비디오 기록 시작")

                {

                    this.takePhotoButton.IsEnabled = false;

 

                    this.statusTextBlock.Text = "비디오 기록을 초기화 합니다.";

 

                    this.videoStorageFile = await KnownFolders.VideosLibrary.CreateFileAsync(VIDEO_FILE_NAME,

                        CreationCollisionOption.GenerateUniqueName);

 

                    this.statusTextBlock.Text = "비디오 저장소 파일이 성공적으로 준비되었습니다.";

 

                    MediaEncodingProfile mediaEncodingProfile = null;

 

                    mediaEncodingProfile = MediaEncodingProfile.CreateMp4(VideoEncodingQuality.Auto);

 

                    await this.mediaCapture.StartRecordToStorageFileAsync(mediaEncodingProfile, this.videoStorageFile);

 

                    this.recordVideoButton.IsEnabled = true;

 

                    this.recordVideoButton.Content = "비디오 기록 중단";

 

                    this.isRecording = true;

 

                    this.statusTextBlock.Text = "비디오 기록 진행중... 중단하기 위해 \'비디오 기록 중단\' 버튼을 누르시기 바랍니다.";

                }

                else

                {

                    this.takePhotoButton.IsEnabled = true;

 

                    this.statusTextBlock.Text = "비디오 기록 중단중...";

 

                    await this.mediaCapture.StopRecordAsync();

 

                    this.isRecording = false;

 

                    

                    IRandomAccessStreamWithContentType randomAccessStreamWithContentType = await this.videoStorageFile.OpenReadAsync();

 

                    this.lastRecordedVideoMediaElement.AutoPlay = true;

 

                    this.lastRecordedVideoMediaElement.SetSource(randomAccessStreamWithContentType, this.videoStorageFile.FileType);

 

                    this.lastRecordedVideoMediaElement.Play();

 

                    this.statusTextBlock.Text = "기록된 비디오 재생 : " + this.videoStorageFile.Path;

 

                    this.recordVideoButton.Content = "비디오 기록 시작";

                }

            }

            catch(Exception exception)

            {

                if(exception is UnauthorizedAccessException)

                {

                    this.statusTextBlock.Text = "기록된 비디오를 재생할 수 없습니다; \'" + this.videoStorageFile.Path +

                        "\'에 비디오가 성공적으로 기록되었습니다.";

 

                    this.recordVideoButton.Content = "비디오 기록 시작";

                }

                else

                {

                    this.statusTextBlock.Text = exception.Message;

 

                    ProcessCloseCamera();

                }                

            }

            finally

            {                

                this.recordVideoButton.IsEnabled = true;                

            }

        }

 

        #endregion

        #region 오디오 기록 버튼 클릭시 처리하기 - recordAudioButton_Click(sender, e)

 

        /// <summary>

        /// 오디오 기록 버튼 클릭시 처리하기

        /// </summary>

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

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

        private async void recordAudioButton_Click(object sender, RoutedEventArgs e)

        {

            this.recordAudioButton.IsEnabled = false;

 

            this.playbackMediaElement.Source = null;

 

            try

            {

                if(this.recordAudioButton.Content.ToString() == "오디오 기록 시작")

                {

                    this.audioStorageFile = await KnownFolders.VideosLibrary.CreateFileAsync(AUDIO_FILE_NAME,

                        CreationCollisionOption.GenerateUniqueName);

 

                    this.statusTextBlock.Text = "오디오 저장소 파일이 성공적으로 준비되었습니다.";

 

                    MediaEncodingProfile mediaEncodingProfile = MediaEncodingProfile.CreateM4a(AudioEncodingQuality.Auto);

 

                    await this.mediaCapture.StartRecordToStorageFileAsync(mediaEncodingProfile, this.audioStorageFile);

 

                    this.isRecording = true;

 

                    this.recordAudioButton.IsEnabled = true;

 

                    this.recordAudioButton.Content = "오디오 기록 중단";

 

                    this.statusTextBlock.Text = "오디오 기록 진행중... 중단하기 위해 \'오디오 기록 중단\' 버튼을 누르시기 바랍니다.";

                }

                else

                {

                    this.statusTextBlock.Text = "오디오 기록 중단중...";

 

                    await this.mediaCapture.StopRecordAsync();

 

                    this.isRecording = false;

 

                    this.recordAudioButton.IsEnabled = true;

                    this.recordAudioButton.Content   = "오디오 기록 시작";

 

                    IRandomAccessStream randomAccessStream = await this.audioStorageFile.OpenAsync(FileAccessMode.Read);

 

                    this.statusTextBlock.Text = "기록된 오디오 재생 : " + this.audioStorageFile.Path;

 

                    this.playbackMediaElement.AutoPlay = true;

 

                    this.playbackMediaElement.SetSource(randomAccessStream, this.audioStorageFile.FileType);

 

                    this.playbackMediaElement.Play();

                }

            }

            catch(Exception exception)

            {

                this.statusTextBlock.Text = exception.Message;

 

                ProcessCloseCamera();                

            }

            finally

            {

                this.recordAudioButton.IsEnabled = true;

            }

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////// Function

 

        #region 초기화 버튼 표시 여부 설정하기 - SetInitializeButtonVisibility(action)

 

        /// <summary>

        /// 초기화 버튼 표시 여부 설정하기

        /// </summary>

        /// <param name="action">액션</param>

        private void SetInitializeButtonVisibility(Action action)

        {

            if(action == Action.ENABLED)

            {

                this.initializeVideoButton.IsEnabled = true;

                this.initializeAudioButton.IsEnabled = true;

            }

            else

            {

                this.initializeVideoButton.IsEnabled = false;

                this.initializeAudioButton.IsEnabled = false;

            }

        }

 

        #endregion

        #region 비디오 버튼 표시 여부 설정하기 - SetVideoButtonVisibility(action)

 

        /// <summary>

        /// 비디오 버튼 표시 여부 설정하기

        /// </summary>

        /// <param name="action">액션</param>

        private void SetVideoButtonVisibility(Action action)

        {

            if(action == Action.ENABLED)

            {

                this.takePhotoButton.IsEnabled  = true;

                this.takePhotoButton.Visibility = Visibility.Visible;

 

                this.recordVideoButton.IsEnabled  = true;

                this.recordVideoButton.Visibility = Visibility.Visible;

            }

            else

            {

                this.takePhotoButton.IsEnabled  = false;

                this.takePhotoButton.Visibility = Visibility.Collapsed;

 

                this.recordVideoButton.IsEnabled  = false;

                this.recordVideoButton.Visibility = Visibility.Collapsed;

            }

        }

 

        #endregion

        #region 오디오 버튼 표시 여부 설정하기 - SetAudioButtonVisibility(action)

 

        /// <summary>

        /// 오디오 버튼 표시 여부 설정하기

        /// </summary>

        /// <param name="action">액션</param>

        private void SetAudioButtonVisibility(Action action)

        {

            if(action == Action.ENABLED)

            {

                this.recordAudioButton.IsEnabled  = true;

                this.recordAudioButton.Visibility = Visibility.Visible;

            }

            else

            {

                this.recordAudioButton.IsEnabled  = false;

                this.recordAudioButton.Visibility = Visibility.Collapsed;

            }

        }

 

        #endregion

 

        #region 카메라 닫기 처리하기 - ProcessCloseCamera()

 

        /// <summary>

        /// 카메라 닫기 처리하기

        /// </summary>

        private async void ProcessCloseCamera()

        {

            if(this.mediaCapture != null)

            {

                if(this.isPreviewing)

                {

                    await this.mediaCapture.StopPreviewAsync();

 

                    this.lastCapturedPhotoImage.Source = null;

 

                    this.lastRecordedVideoMediaElement.Source = null;

 

                    this.isPreviewing = false;

                }

 

                if(this.isRecording)

                {

                    await this.mediaCapture.StopRecordAsync();

 

                    this.isRecording = false;

 

                    this.recordVideoButton.Content = "비디오 기록 시작";

                    this.recordAudioButton.Content = "오디오 기록 시작";

                }

                

                this.mediaCapture.Dispose();

 

                this.mediaCapture = null;

            }

 

            SetInitializeButtonVisibility(Action.ENABLED);

        }

 

        #endregion

    }

}

 

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

Posted by 사용자 icodebroker
TAG

댓글을 달아 주세요