AForge.net 使用之錄影拍照功能實現

來源:互聯網
上載者:User

標籤:

原文地址:http://blog.csdn.net/chenhongwu666/article/details/41965801

串連網路攝影機裝置,這裡需要引入

AForge.Video;

AForge.Video.DirectShow;

AForge.Video.FFMPEG;

還需要添加引用,aforge.dll,aforge.video,aforge.video.directshow,aforge.control,aforge.video.ffmpeg,

在工具箱中還需要添加AForge.Control,然後找到VideoSourcePlayer這個控制項添加到介面上

然後定義變數

private FilterInfoCollection videoDevices;
private VideoCaptureDevice videoSource;

private bool stopREC = true;
private bool createNewFile = true;

private string videoFileFullPath = string.Empty; //視頻檔案全路徑
private string imageFileFullPath = string.Empty; //影像檔全路徑
private string videoPath = @"E:\video\"; //視頻檔案路徑
private string imagePath = @"E:\video\images\"; //影像檔路徑
private string videoFileName = string.Empty; //視頻檔案名稱
private string imageFileName = string.Empty; //影像檔名
private string drawDate = string.Empty;
private VideoFileWriter videoWriter = null;

public delegate void MyInvoke(); //定義一個委託方法

string g_s_AutoSavePath = AppDomain.CurrentDomain.BaseDirectory + "Capture\\";
object objLock = new object(); //定義一個對象的鎖
int frameRate = 20; //預設幀率
private Stopwatch stopWatch = null;
IVideoSource iVideoSource = null;

private void InitUI(){    //串連    if (msc.OpenPort())    {        msc.ConnectMicroScopeControl();        //開啟網路攝影機        videoDevices = vh.GetDevices();        if (videoDevices != null && videoDevices.Count > 0)        {            videoSource = vh.VideoConnect();        }        videoSourcePlayer1.VideoSource = videoSource;        videoSourcePlayer1.Start();    }}

開始錄影

 private void btnStartVideotape_Click(object sender, EventArgs e)        {            //開始錄影            if (btnStartVideotape.Text == "開始錄影")            {                stopREC = false;                frameRate = Convert.ToInt32(txtFrameRate.Text.Trim());                btnStartVideotape.Text = "停止錄影";            }            else if (btnStartVideotape.Text == "停止錄影")            {                stopREC = true;                btnStartVideotape.Text = "開始錄影";            }        }

添加aforge.net的一個VideoSourcePlayer控制項之後找到NewFrame事件,代碼如下:

下面是控制項的一個事件,是真正錄影的代碼

private void videoSourcePlayer1_NewFrame(object sender, ref Bitmap image)        {            //錄影            Graphics g = Graphics.FromImage(image);            SolidBrush drawBrush = new SolidBrush(Color.Yellow);            Font drawFont = new Font("Arial", 6, FontStyle.Bold, GraphicsUnit.Millimeter);            int xPos = image.Width - (image.Width - 15);            int yPos = 10;            //寫到螢幕上的時間            drawDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");            g.DrawString(drawDate, drawFont, drawBrush, xPos, yPos);            if (!Directory.Exists(videoPath))                Directory.CreateDirectory(videoPath);            //建立檔案路徑            //fileFullPath = path + fileName;            if (stopREC)            {                stopREC = true;                createNewFile = true;  //這裡要設定為true表示要建立新檔案                if (videoWriter != null)                    videoWriter.Close();            }            else            {                //開始錄影                if (createNewFile)                {                    videoFileName = DateTime.Now.ToString("yyyy.MM.dd HH.mm.ss") + ".avi";                    videoFileFullPath = videoPath + videoFileName;                    createNewFile = false;                    if (videoWriter != null)                    {                        videoWriter.Close();                        videoWriter.Dispose();                    }                    videoWriter = new VideoFileWriter();                    //這裡必須是全路徑,否則會預設儲存到程式運行根據錄下了                    videoWriter.Open(videoFileFullPath, image.Width, image.Height, frameRate, VideoCodec.MPEG4);                    videoWriter.WriteVideoFrame(image);                }                else                {                    videoWriter.WriteVideoFrame(image);                }            }        }

拍照代碼

/// <summary>/// 手動拍照或抓圖/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void btnCapture_Click(object sender, EventArgs e){    try    {        int number=0;        number++;        string fileImageName = g_s_RequestNo + "-" + number + ".bmp";        string fileCapturePath = g_s_AutoSavePath + g_s_RequestNo + "\\";        if (!Directory.Exists(fileCapturePath))            Directory.CreateDirectory(fileCapturePath);        //抓到圖儲存到指定路徑        Bitmap bmp = null;        bmp = videoSourcePlayer1.GetCurrentVideoFrame();        if (bmp == null)        {            MessageBox.Show("捕獲映像失敗!", "提示");            return;        }                bmp.Save(fileCapturePath + fileImageName, ImageFormat.Bmp);                }    catch (Exception ex)    {        MessageBox.Show("捕獲映像失敗!" + ex.Message, "提示");    }}

最基本的工作就完成了,如果要更加完美的話還需要細化,最佳化等等,就先記錄到此,以便記錄自己的點點滴滴的進步

AForge.net 使用之錄影拍照功能實現

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.