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


最近使用aforge.net拍照錄像功能實現

記錄一下以便以后好學習,哈哈,直接上代碼

連接攝像頭設備,這里需要引入

AForge.Video;

AForge.Video.DirectShow;

AForge.Video.FFMPEG;

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

在工具箱中還需要添加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() { //連接//開啟攝像頭
    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, "提示"); } }

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

 


免責聲明!

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



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