在好例子网,分享、交流、成长!
您当前所在位置:首页C# 开发实例C#语言基础 → 录制画中画(桌面+摄像头+麦克风声卡)

录制画中画(桌面+摄像头+麦克风声卡)

C#语言基础

下载此实例
  • 开发语言:C#
  • 实例大小:7.92M
  • 下载次数:31
  • 浏览次数:295
  • 发布时间:2020-02-21
  • 实例类别:C#语言基础
  • 发 布 人:duyuanbin
  • 文件格式:.rar
  • 所需积分:10
 相关标签: 摄像头 麦克风 摄像 桌面 录制

实例介绍

【实例简介】使用此实例,需要购买Oraycn.的授权

【实例截图】

from clipboard

【核心代码】

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ESBasic;
using Oraycn.MCapture;
using Oraycn.MFile;

namespace Oraycn.RecordPictureCompositionDemo
{
    /*
    * 本demo采用的是 语音视频采集组件MCapture 和 语音视频录制组件MFile 的免费版本。若想获取MCapture、MFile的其它版本,请联系 www.oraycn.com ,客服qq:168757008。
    * 
    */
    public partial class Form1 : Form
    {
        private ISoundcardCapturer soundcardCapturer;
        private IMicrophoneCapturer microphoneCapturer;
        private IDesktopCapturer desktopCapturer;
        private ICameraCapturer cameraCapturer;
        private IAudioMixter audioMixter;
        private VideoFileMaker videoFileMaker;
        private SilenceVideoFileMaker silenceVideoFileMaker;
        //private AudioFileMaker audioFileMaker;
        private int frameRate = 10; // 采集视频的帧频
        private bool sizeRevised = false;// 是否需要将图像帧的长宽裁剪为4的整数倍
        private  bool isRecording = false;
        private bool isParsing = false;
        private Timer timer;
        private int seconds = 0;
        private bool justRecordVideo = false;
        

        public Form1()
        {
            InitializeComponent();
            Oraycn.MCapture.GlobalUtil.SetAuthorizedUser("FreeUser", "");
            Oraycn.MFile.GlobalUtil.SetAuthorizedUser("FreeUser", "");

            this.timer = new Timer();
            this.timer.Interval = 1000;
            this.timer.Tick  = new EventHandler(timer_Tick);
        }

        void timer_Tick(object sender, EventArgs e)
        {
            if (this.isRecording && !this.isParsing)
            {
                var ts = new TimeSpan(0, 0,   seconds);
                this.label_time.Text = ts.Hours.ToString("00")   ":"   ts.Minutes.ToString("00")   ":"   ts.Seconds.ToString("00");
                if (this.videoFileMaker != null)
                {
                    this.label_info.Text = this.videoFileMaker.ToBeProcessedVideoFrameCount.ToString() ;
                }
            }
        }

        #region 开始录制
        private void button_start_Click(object sender, EventArgs e)
        {
            //TODO 开始录制桌面,依据 声音复选框 来选择使用 声卡 麦克风 还是混合录制, 图像复选框来选择 图像的采集器
            try
            {
                int audioSampleRate = 16000;
                int channelCount = 1;
                seconds = 0;

                System.Drawing.Size videoSize_desktop ;
                
                

                #region 设置采集器
                
                    //桌面采集器
                    
                    //如果需要录制鼠标的操作,第二个参数请设置为true
                    this.desktopCapturer = CapturerFactory.CreateDesktopCapturer(frameRate, false);
                    this.desktopCapturer.ImageCaptured  = new CbGeneric<Bitmap>(this.DesktopImageCaptured);
                    videoSize_desktop = this.desktopCapturer.VideoSize; 
                
                    //摄像头采集器
                    
                    this.cameraCapturer = CapturerFactory.CreateCameraCapturer(0, new System.Drawing.Size(640, 480), frameRate);
                    this.cameraCapturer.ImageCaptured  = new CbGeneric<Bitmap>(this.VideoImageCaptured);
               

                if (this.checkBox_micro.Checked)
                {
                    //麦克风采集器
                    this.microphoneCapturer = CapturerFactory.CreateMicrophoneCapturer(0);
                    this.microphoneCapturer.CaptureError  = new CbGeneric<Exception>(this.CaptureError);
                }

                if (this.checkBox_soundCard.Checked)
                {
                    //声卡采集器 【目前声卡采集仅支持vista以及以上系统】
                    this.soundcardCapturer = CapturerFactory.CreateSoundcardCapturer();
                    this.soundcardCapturer.CaptureError  = this.CaptureError;
                    audioSampleRate = this.soundcardCapturer.SampleRate;
                    channelCount = this.soundcardCapturer.ChannelCount;
                }

                if (this.checkBox_micro.Checked && this.checkBox_soundCard.Checked)
                {
                    //混音器
                    this.audioMixter = CapturerFactory.CreateAudioMixter(this.microphoneCapturer, this.soundcardCapturer,
                        SoundcardMode4Mix.DoubleChannel, true);
                    this.audioMixter.AudioMixed  = audioMixter_AudioMixed;
                    audioSampleRate = this.audioMixter.SampleRate;
                    channelCount = this.audioMixter.ChannelCount;
                }
                else if (this.checkBox_micro.Checked)
                {
                    this.microphoneCapturer.AudioCaptured  = audioMixter_AudioMixed;
                }
                else if (this.checkBox_soundCard.Checked)
                {
                    this.soundcardCapturer.AudioCaptured  = audioMixter_AudioMixed;
                }
                #endregion


                #region //开始采集
                if (this.checkBox_micro.Checked)
                {
                    this.microphoneCapturer.Start();
                }
                if (this.checkBox_soundCard.Checked)
                {
                    this.soundcardCapturer.Start();
                }

                
                    this.cameraCapturer.Start();
                
               
                    this.desktopCapturer.Start();
                
                #endregion


                #region //录制组件


                    this.sizeRevised = (videoSize_desktop.Width % 4 != 0) || (videoSize_desktop.Height % 4 != 0);
                    if (this.sizeRevised)
                    {
                        videoSize_desktop = new System.Drawing.Size(videoSize_desktop.Width / 4 * 4, videoSize_desktop.Height / 4 * 4);
                    }

                    if (!this.checkBox_micro.Checked && !this.checkBox_soundCard.Checked)
                    {
                        //只录制 图像
                        this.justRecordVideo = true;
                        this.silenceVideoFileMaker = new SilenceVideoFileMaker();
                        this.silenceVideoFileMaker.Initialize("test.mp4", VideoCodecType.H264, videoSize_desktop.Width, videoSize_desktop.Height, frameRate, VideoQuality.Middle);
                    }
                    else
                    {
                        // 录制声音和图像
                        this.justRecordVideo = false;
                        this.videoFileMaker = new VideoFileMaker();
                        this.videoFileMaker.Initialize("test.mp4", VideoCodecType.H264, videoSize_desktop.Width, videoSize_desktop.Height, frameRate, VideoQuality.High,
                            AudioCodecType.AAC, audioSampleRate, channelCount, true);

                    }
                
                #endregion



                this.isRecording = true;
                this.isParsing = false;

                this.timer.Start();

                this.checkBox_micro.Enabled = false;
                this.checkBox_soundCard.Enabled = false;
                

                this.button_start.Enabled = false;


            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.Message);
            }
        }
        #region CaptureError
        void CaptureError(Exception obj)
        {

        } 
        #endregion

        #region VideoImageCaptured
        private Bitmap videoImgRecorded;
        //采集到的视频图像
        void VideoImageCaptured(Bitmap img)
        {
            if (this.isRecording && !this.isParsing)
            {
               
                this.videoImgRecorded = img;
                
            }
        } 
        #endregion

        
        #region DesktopImageCaptured
        
        //采集到的桌面图像
        void DesktopImageCaptured(Bitmap img)
        {
            if (this.isRecording && !this.isParsing)
            {
                if (this.videoImgRecorded != null)
                {
                    //这里要裁剪
                    Bitmap desktopImgRecorded = img;
                    if (this.sizeRevised) // 对图像进行裁剪,  MFile要求录制的视频帧的长和宽必须是4的整数倍。
                    {
                        desktopImgRecorded = ESBasic.Helpers.ImageHelper.RoundSizeByNumber(img, 4);
                        img.Dispose();
                    }
                   
                    //合并图像
                    Graphics g = Graphics.FromImage(desktopImgRecorded);
                    g.DrawImage(this.videoImgRecorded, new Rectangle(new Point((desktopImgRecorded.Width - this.videoImgRecorded.Width), (desktopImgRecorded.Height - this.videoImgRecorded.Height)),
                        desktopImgRecorded.Size));
                    g.Dispose();

                    if (!this.justRecordVideo)
                    {
                        this.videoFileMaker.AddVideoFrame(desktopImgRecorded);
                    }
                    else
                    {
                        this.silenceVideoFileMaker.AddVideoFrame(desktopImgRecorded);
                    }
                }
            }
        }
        #endregion

        #region audioMixter_AudioMixed
        void audioMixter_AudioMixed(byte[] audioData)
        {
            if (this.isRecording && !this.isParsing)
            {

                if (!this.justRecordVideo)
                {
                    this.videoFileMaker.AddAudioFrame(audioData);
                }


            }
        } 
        #endregion
        #endregion

        #region 暂停
        private void button_parse_Click(object sender, EventArgs e)
        {
            //TODO 暂停当前录制或恢复录制
            //TODO label 中显示实际录制的时间,需要考虑暂停和恢复这种情况。 格式为 hh:mm:ss
            if (this.isParsing)
            {
                this.isParsing = false;
            }
            else
            {
                this.isParsing = true;
            }
            this.button_parse.Text = (!this.isParsing ? "暂停" : "恢复");

        }
        
        #endregion


        #region 结束录制
        private void button_stop_Click(object sender, EventArgs e)
        {
            this.checkBox_micro.Enabled = true;
            this.checkBox_soundCard.Enabled = true;


            this.button_start.Enabled = true;
            this.button_parse.Text = "暂停";

            if (this.checkBox_micro.Checked) // 麦克风
            {
                this.microphoneCapturer.Stop();
            }
            if (this.checkBox_soundCard.Checked) //声卡
            {
                this.soundcardCapturer.Stop();
            }

            this.cameraCapturer.Stop();


            this.desktopCapturer.Stop();


            if (!this.justRecordVideo)
            {
                this.videoFileMaker.Close(true);
            }
            else
            {
                this.silenceVideoFileMaker.Close(true);
            }


            this.isRecording = false;
            MessageBox.Show("录制完成!");
        }
        #endregion

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (this.isRecording)
            {
                MessageBox.Show("正在录制中,退出前请先停止录制!");
                e.Cancel = true;
                return;
            }

            e.Cancel = false;
        }

       






        
    }
}

实例下载地址

录制画中画(桌面+摄像头+麦克风声卡)

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警