加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

如何使用c#在Windows Phone中使用前置摄像头捕获视频

发布时间:2020-12-15 22:10:40 所属栏目:百科 来源:网络整理
导读:我正在开发一个 Windows手机应用程序,它需要使用c#通过前置摄像头捕获视频,我能够在后置摄像头的帮助下捕获视频,但我需要在前置摄像头的帮助下捕获.我对此进行了大量搜索但未找到相关答案.我们将不胜感激. public partial class Movies : PhoneApplicationPa
我正在开发一个 Windows手机应用程序,它需要使用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>来显示列表支持的摄像机.

CaptureDevice和CaptureDevice的VideoCaptureDevice继承具有属性FriendlyName或IsDefaultDevice

对于我的Nokia FriendlyName可能有值:

>“自拍相机”
>“主摄像头”

对于我的诺基亚IsDefaultDevice始终适用于主摄像头

所以最终代码有助于找到前脸相机看起来像这样:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读