基於Aforge的視頻錄像


最近做了一個基於Aforge,MMPEG 的 基於攝像頭的,視頻錄像小軟件

軟件實現的工能如下:

1.自動開機錄像。這里自動把攝像頭的攝像大小設置成 320 * 320

 try
            {

                Common.Setting set = new Common.Setting();
                XmlSerialize xml = new XmlSerialize();
                set = xml.DeSerialize();
                this.Path = set.Path;
                this.maxSize = set.MaxSize;
                this.frameRate = set.FrameRate;
                try
                {
                    string dic = System.DateTime.Now.AddMonths(-2).ToString("yyyyMMdd") + "\\";
                    string mypath = @Path + dic;
                    System.IO.Directory.Delete(mypath,true);
                }
                catch (Exception ex) { }

                // enumerate video devices
                videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);

                if (videoDevices.Count == 0)
                    throw new ApplicationException();

               
                device = videoDevices[0].MonikerString;

                // create video source
                VideoCaptureDevice videoSource = new VideoCaptureDevice(this.VideoDevice);
                //videoSource.DesiredFrameRate = this.frameRate;
                videoSource.DesiredFrameSize = new System.Drawing.Size(320, 240);
                this.videoSourcePlayer.Size = new System.Drawing.Size(320, 240);

                // open it
                OpenVideoSource(videoSource);


            }
            catch (ApplicationException ex)
            {
                MessageBox.Show(ex.ToString());
                this.Close();
            }

2. 錄像過程

bool StopREC = true;
        bool CreateNewFile = true;
        string myfile = "";
        int FrameCountSame = 0;
        private void videoSourcePlayer_NewFrame(object sender, ref Bitmap image)
        {
            count++;
           
           
            System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(image);
           
            SolidBrush drawBrush = new SolidBrush(Color.Red);
           
            Font drawFont = new Font("Arial", 6, FontStyle.Bold, GraphicsUnit.Millimeter);

            int xPos = image.Height - (image.Height - 25);

            int yPos = 3;

            string date = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");//得到寫到屏上的時間
            string dic = System.DateTime.Now.ToString("yyyyMMdd")+"\\";
            string dateFile = System.DateTime.Now.ToString("HH.mm.ss") + ".avi";
           
          

            string mypath = @Path+dic;
           
            System.IO.Directory.CreateDirectory(mypath);

            gr.DrawString(date, drawFont, drawBrush, xPos, yPos);

#region 這里使用 wmv3的格式
 //if (StopREC)
            //{
            //    StopREC = false;

            //    writer = new AVIWriter("wmv3");
            //    writer.FrameRate = 8;
            //    writer.Open(@"d:\log" + System.DateTime.Now.ToString("yyyyMMddhhmmss") + ".avi", 640, 480);
            //    writer.AddFrame(image);
            //}
            //else
            //{
            //    writer.AddFrame(image);
               
               
            //}
#endregion
            if (count > this.frameRate) { return; }
            if (ifCreateFilePath(myfile)) { CreateNewFile = true; myfile = mypath + dateFile; }
           
            if (StopREC)
            {
                StopREC = false;
                this.CreateNewFile = false;

                int width = 320;
                int height = 240;
                writer1 = new VideoFileWriter();
                writer1.Open(@myfile, width, height, this.frameRate, VideoCodec.MPEG4);
                //writer1.Open(@myfile, width, height);
                try
                {
                
                    writer1.WriteVideoFrame(image);
                }
                catch (Exception ex) {
                    LogErr(ex.ToString());
                }
            
               
            }
            else {
               
                if (this.CreateNewFile)
                {
                    this.CreateNewFile = false;

                    if (writer1 != null)
                    {
                    
                        writer1.Close();
                        writer1.Dispose();
                    }
                    int width = 320;
                    int height = 240;
                    writer1 = new VideoFileWriter();

                    writer1.Open(@myfile, width, height, this.frameRate, VideoCodec.MPEG4);
                    //writer1.Open(@myfile, width, height);
                    writer1.WriteVideoFrame(image);
                   
                }
                else
                {
                   
                    writer1.WriteVideoFrame(image);
                   
                }

               
            }

基本上蠻足需要。

1.通過Aforge 錄像時,如果突然關機。不能接上一文件繼續錄像(我沒找到相應辦法)。

2.frameRate的問題,我看網上說,要達到 30 時才是最好的,但我的攝像頭,在筆記本上就是達不到,只能到 10。所以我在錄像時必須設置 這個值為 8 ,否則錄像時,不是像快進,就是在放慢動作,而且超過8的幀數不要了(感覺這樣不是很對,但先只能這樣了)。

上面兩個問題記錄下來,以后找找資料再處理。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM