簡體   English   中英

如何使用C#在Windows Phone中使用前置攝像頭捕獲視頻

[英]How to capture video using front camera in windows phone using c#

我正在開發一個Windows Phone應用程序,該應用程序需要使用c#通過前置攝像頭捕獲視頻,我能夠在后置攝像頭的幫助下捕獲視頻,但是我需要在前置攝像頭的幫助下捕獲視頻。 我對此進行了很多搜索,但找不到相關的答案。 您的幫助將不勝感激。

    public partial class Movies : PhoneApplicationPage
    {

        VideoBrush myvideobrush;      //for capturing video.
        CaptureSource myvideosource;  //source for capturing video.
        VideoCaptureDevice mydevice;  //device for capturing video.
        FileSink myfilesink;          //for storing the video.
        private string isoVideoFileName = "CameraMovie.mp4";
        private IsolatedStorageFileStream isoVideoFile;


        public Movies()
        {

            InitializeComponent();
            if (myvideosource == null)
            {
                myvideosource = new CaptureSource();
                myfilesink = new FileSink();
                mydevice = CaptureDeviceConfiguration.GetDefaultVideoCaptureDevice();

                //System.Collections.ObjectModel.ReadOnlyCollection<System.Windows.Media.VideoCaptureDevice> supportedcams = CaptureDeviceConfiguration.GetAvailableVideoCaptureDevices();
                //mydevice = supportedcams.ElementAt(0);
            }
            if (mydevice != null)
            {
                myvideobrush = new VideoBrush();

                myvideobrush.SetSource(myvideosource);
                viewFinderRectangle.Fill = myvideobrush;
                stop_btn.IsEnabled = false;
                myvideosource.Start();
            }

        }
        public void startReccording()
        {
            start_btn.IsEnabled = false;
            stop_btn.IsEnabled = true;


            if (myvideosource.VideoCaptureDevice != null && myvideosource.State == CaptureState.Started)
            {
                myvideosource.Stop();
                myfilesink.CaptureSource = myvideosource;
                myfilesink.IsolatedStorageFileName = isoVideoFileName;
            }
            if (myvideosource.VideoCaptureDevice != null && myvideosource.State == CaptureState.Stopped)
            {

                myvideosource.Start();
            }
        }
        public void stopRecording()
        {


            if (myvideosource.VideoCaptureDevice != null && myvideosource.State == CaptureState.Started)
            {
                myvideosource.Stop();

                myfilesink.CaptureSource = null;
                myfilesink.IsolatedStorageFileName = null;
                videoPriview();
            }



        }
        public void videoPriview()
        {

            if (isoVideoFile != null)
            {
                videoPlayer.Play();
            }
            else
            {
                myvideosource.Stop();
                viewFinderRectangle.Fill = null;
                isoVideoFile = new IsolatedStorageFileStream(isoVideoFileName, FileMode.Open, FileAccess.Read, IsolatedStorageFile.GetUserStoreForApplication());
                videoPlayer.SetSource(isoVideoFile);
                videoPlayer.Play();
            }
            start_btn.IsEnabled = true;
            stop_btn.IsEnabled = false;

        }


        private void movies_goback_btn_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.GoBack();
        }

        private void start_btn_Click(object sender, RoutedEventArgs e)
        {
            startReccording();
        }

        private void stop_btn_Click(object sender, RoutedEventArgs e)
        {
            stopRecording();
        }
    }
}

CaptureDeviceConfiguration.GetAvailableVideoCaptureDevices()通過返回ReadOnlyCollection<VideoCaptureDevice>顯示支持列表的攝像機

CaptureDeviceCaptureDevice繼承的VideoCaptureDevice繼承具有屬性FriendlyNameIsDefaultDevice

對於我的Nokia FriendlyName可能具有值:

  • "Self portrait camera"
  • "Primary camera"

對於我的諾基亞IsDefaultDevice對於Primary camera始終為true

因此,最終有助於查找正面相機的代碼如下所示:

VideoCaptureDevice frontDevice = null;
ReadOnlyCollection<VideoCaptureDevice> devices = CaptureDeviceConfiguration.GetAvailableVideoCaptureDevices();

foreach (VideoCaptureDevice dev in devices)
{
    if (!dev.IsDefaultDevice)
    {
        device = dev;
    }
}

// for now device contains front-face camera

使用CaptureDeviceConfiguration.GetAvailableVideoCaptureDevices()獲取設備上所有可用攝像機的列表,並選擇正面攝像機並將其分配給mydevice變量。

訪問前置攝像頭時,請不要忘記在清單中設置ID_REQ_FRONTCAMERA權限。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM