基本信息
源码名称:C# 调用摄像头录制视频(含录制桌面功能)源码下载
源码大小:7.61M
文件格式:.rar
开发语言:C#
更新时间:2015-08-24
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍

项目中 引用的 视频相关dll为 傲瑞相关组件,最近有用户反馈dll已过期,如需相关功能,请购买相关授权,官网:http://www.oraycn.com/


 public partial class Form1 : Form
    {
        private ICapturer audioCapturer;
        private ICapturer videoCapturer;
        private VideoFileMaker videoFileMaker;

        public Form1()
        {
            InitializeComponent();
            this.comboBox1.SelectedIndex = 0;
            this.comboBox2.SelectedIndex = 0;

            Oraycn.MCapture.GlobalUtil.SetAuthorizedUser("FreeUser", "");
            Oraycn.MFile.GlobalUtil.SetAuthorizedUser("FreeUser", "");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                int audioSampleRate = 16000;
                int channelCount = 1;
                
                if (this.comboBox1.SelectedIndex == 0) //麦克风
                {
                    this.audioCapturer = CapturerFactory.CreateMicrophoneCapturer(0);
                    ((IMicrophoneCapturer)this.audioCapturer).AudioCaptured  = new ESBasic.CbGeneric<byte[]>(Form1_AudioCaptured);
                }
                else //声卡
                {
                    this.audioCapturer = CapturerFactory.CreateSoundcardCapturer();
                    ((ISoundcardCapturer)this.audioCapturer).AudioCaptured  = new ESBasic.CbGeneric<byte[]>(Form1_AudioCaptured);
                    audioSampleRate = ((ISoundcardCapturer)this.audioCapturer).SampleRate;
                    channelCount = ((ISoundcardCapturer)this.audioCapturer).ChannelCount;
                }

                int frameRate = 15 ;
                System.Drawing.Size videoSize = new Size(int.Parse(this.textBox_width.Text), int.Parse(this.textBox_height.Text));            
                if (this.comboBox2.SelectedIndex == 0) //摄像头
                {
                    this.videoCapturer = CapturerFactory.CreateCameraCapturer(0, videoSize, frameRate);
                    ((ICameraCapturer)this.videoCapturer).ImageCaptured  = new ESBasic.CbGeneric<Bitmap>(Form1_ImageCaptured);
                }
                else // 桌面
                {
                    this.videoCapturer = CapturerFactory.CreateDesktopCapturer(frameRate, false);
                    ((IDesktopCapturer)this.videoCapturer).ImageCaptured  = new ESBasic.CbGeneric<Bitmap>(Form1_ImageCaptured);
                    videoSize = Screen.PrimaryScreen.Bounds.Size;
                }                   
                
                this.videoFileMaker = new VideoFileMaker();
                this.videoFileMaker.AutoDisposeVideoFrame = true;
                this.videoFileMaker.Initialize("test.mp4", VideoCodecType.H264, videoSize.Width, videoSize.Height, frameRate, AudioCodecType.AAC, audioSampleRate, channelCount, true);
                
                this.button1.Enabled = false;
                this.button2.Enabled = true;
                this.comboBox1.Enabled = false;
                this.comboBox2.Enabled = false;
                this.panel_camera.Enabled = false;
                this.label_tip.Visible = true;
               
                //开始采集
                this.audioCapturer.Start();
                this.videoCapturer.Start();
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.Message);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //停止采集
            this.audioCapturer.Stop();
            this.videoCapturer.Stop();
            this.videoFileMaker.Close(true);
            this.button2.Enabled = false;
            this.button1.Enabled = true;
            this.comboBox1.Enabled = true;
            this.comboBox2.Enabled = true;
            this.panel_camera.Enabled = true;
            this.label_tip.Visible = false;

            //清除最后一帧的显示
            CbGeneric cb = new CbGeneric(this.AfterStop);
            cb.BeginInvoke(null, null);
        }

        private void AfterStop()
        {            
            if (this.InvokeRequired)
            {
                System.Threading.Thread.Sleep(1000);
                this.BeginInvoke(new CbGeneric(this.AfterStop));
            }
            else
            {
                //清除最后一帧的显示
                this.pictureBox1.BackgroundImage = null;
                this.pictureBox1.Invalidate();
            }
        }
              
        void Form1_AudioCaptured(byte[] audioData) //采集到的声音数据
        {
            this.videoFileMaker.AddAudioFrame(audioData);
        }
        
        //采集到的视频或桌面图像
        void Form1_ImageCaptured(Bitmap img)
        {
            this.DisplayVideo((Bitmap)img.Clone());
            this.videoFileMaker.AddVideoFrame(img);
        }

        private void DisplayVideo(Bitmap img)
        {
            if (this.InvokeRequired)
            {
                this.BeginInvoke(new ESBasic.CbGeneric<Bitmap>(this.DisplayVideo), img);
            }
            else
            {
                Bitmap old = (Bitmap)this.pictureBox1.BackgroundImage;
                this.pictureBox1.BackgroundImage = img;
                if (old != null)
                {
                    old.Dispose();
                }
            }
        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.panel_camera.Visible = this.comboBox2.SelectedIndex == 0;            
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.comboBox1.SelectedIndex == 1)
            {
                MessageBox.Show("声卡采集进目前仅支持windows vista/7 及以上版本的系统!");
            }
        }
         
    }