简体   繁体   English

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

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

I am working on a windows phone application which requires video capture through front facing camera using c#, I am able to capture video with the help of back camera but I need to capture with the help of front camera. 我正在开发一个Windows Phone应用程序,该应用程序需要使用c#通过前置摄像头捕获视频,我能够在后置摄像头的帮助下捕获视频,但是我需要在前置摄像头的帮助下捕获视频。 I have searched a lot on this but couldn't find relevant answer. 我对此进行了很多搜索,但找不到相关的答案。 Your help will be appreciated. 您的帮助将不胜感激。

    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() show list supported cameras by returning ReadOnlyCollection<VideoCaptureDevice> CaptureDeviceConfiguration.GetAvailableVideoCaptureDevices()通过返回ReadOnlyCollection<VideoCaptureDevice>显示支持列表的摄像机

VideoCaptureDevice inheritance from CaptureDevice and CaptureDevice have property FriendlyName or IsDefaultDevice CaptureDeviceCaptureDevice继承的VideoCaptureDevice继承具有属性FriendlyNameIsDefaultDevice

For my Nokia FriendlyName may have values: 对于我的Nokia FriendlyName可能具有值:

  • "Self portrait camera"
  • "Primary camera"

For my Nokia IsDefaultDevice always true for Primary camera 对于我的诺基亚IsDefaultDevice对于Primary camera始终为true

so finally code which helps to find Front-face camera looks like this: 因此,最终有助于查找正面相机的代码如下所示:

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

use CaptureDeviceConfiguration.GetAvailableVideoCaptureDevices() to get the list of all the cameras available on the device and choose the front facing one and assign it to your mydevice variable. 使用CaptureDeviceConfiguration.GetAvailableVideoCaptureDevices()获取设备上所有可用摄像机的列表,并选择正面摄像机并将其分配给mydevice变量。

Do not forget to set the ID_REQ_FRONTCAMERA permission in your manifest, when accessing the front camera. 访问前置摄像头时,请不要忘记在清单中设置ID_REQ_FRONTCAMERA权限。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM