【问题标题】:How to capture video using front camera in windows phone using c#如何使用 c# 在 windows phone 中使用前置摄像头捕获视频
【发布时间】:2015-01-13 05:48:46
【问题描述】:

我正在开发一个 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();
        }
    }
}

【问题讨论】:

    标签: c# .net xaml windows-phone-8 camera


    【解决方案1】:

    CaptureDeviceConfiguration.GetAvailableVideoCaptureDevices()通过返回ReadOnlyCollection&lt;VideoCaptureDevice&gt;显示支持的摄像机列表

    VideoCaptureDevice 继承自 CaptureDeviceCaptureDevice 具有属性 FriendlyNameIsDefaultDevice

    对于我的诺基亚FriendlyName 可能有值:

    • "Self portrait camera"
    • "Primary camera"

    对于我的诺基亚IsDefaultDevice 始终适用于Primary camera

    所以最终有助于找到前置摄像头的代码如下所示:

    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
    

    【讨论】:

      【解决方案2】:

      使用CaptureDeviceConfiguration.GetAvailableVideoCaptureDevices() 获取设备上所有可用摄像头的列表,然后选择前置摄像头并将其分配给您的 mydevice 变量。

      在访问前置摄像头时,不要忘记在清单中设置 ID_REQ_FRONTCAMERA 权限。

      【讨论】:

      • 我使用了这个,但仍然无法完成我需要的任务 System.Collections.ObjectModel.ReadOnlyCollection supportedcams = CaptureDeviceConfiguration.GetAvailableVideoCaptureDevices(); mydevice = supportedcams.ElementAt(1);
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-06
      • 1970-01-01
      • 2021-10-21
      • 1970-01-01
      • 1970-01-01
      • 2017-04-27
      相关资源
      最近更新 更多