在c#中使用网络摄像头捕获图像

时间:2014-12-29 14:58:54

标签: c# webcam image-capture

我的项目的一部分需要一些帮助。我需要网络摄像头以2秒的间隔捕获由运动检测器触发的3张图像。这是我的代码。问题是我有三次相同的图像(第一个)。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
using System.IO;
using System.ComponentModel;
using System.Data;
using System.Threading.Tasks;
using AForge.Video;
using AForge.Video.DirectShow;
using System.Data.SqlClient;
using System.Drawing;
using System.Threading;

namespace pro
{
    class Program
    {
        static SerialPort serialPort = new SerialPort("COM4", 9600);
        static FilterInfoCollection WebcamColl;
        static VideoCaptureDevice Device;
        static int no = 1;

        static void Main(string[] args)
        {
             if (!serialPort.IsOpen)
                serialPort.Open();   
             Console.WriteLine("");             
             serialPort.DataReceived += new SerialDataReceivedEventHandler(serialPort_DataReceived);
             Console.ReadKey();            
       }
       static void serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
       {          
             string _read =  serialPort.ReadExisting().ToString();
             if (_read == "ERROR" )
             {
                 WebcamColl = new FilterInfoCollection(FilterCategory.VideoInputDevice);            
                 Device = new VideoCaptureDevice(WebcamColl[0].MonikerString);
                 Device.Start();
                 Device.NewFrame += new NewFrameEventHandler( Device_NewFrame);            
                 Console.ReadLine();        
             }
        }
        static void Device_NewFrame(object sender, NewFrameEventArgs eventArgs)
        {           
             for (int i = 0; i < 3; i++)
             {
                 System.Drawing.Image img = (Bitmap)eventArgs.Frame.Clone();
                 string imagePath;
                 string fileName = "Image";
                 fileName = fileName + no.ToString() + ".jpg";
                 img.Save("D:\\aaaa\\" + fileName);
                 Thread.Sleep(2000);
                 Console.WriteLine("Snapshot Saved.");
                 imagePath = "D:\\aaaa\\" + fileName;           
                 FileStream fs = new FileStream(imagePath, FileMode.Open, FileAccess.Read);               
                 BinaryReader br = new BinaryReader(fs);
                 byte[] _image = br.ReadBytes((int)fs.Length);
                 br.Close();
                 fs.Close();         
                 SqlConnection conn = new SqlConnection("Data source=ULTRASLAN\\SQLEXPRESS; Initial Catalog=veritabani; Integrated Security=true");
                 SqlCommand kmt = new SqlCommand("insert into _imageler(_image,kullanici_id,tarih) Values (@image,22,getdate())", conn);
                 kmt.Parameters.Add("@image", SqlDbType.Image, _image.Length).Value = _image;

                 try
                 {
                     conn.Open();
                     kmt.ExecuteNonQuery();
                     Console.WriteLine(" Saving image to the database successfull..");
                     no++;
                 }

                 catch (Exception ex)
                {
                     Console.WriteLine(ex.Message.ToString());
                }

               finally
               {
                     conn.Close();
               }

           }
           Device.SignalToStop();
       }

    }
}

0 个答案:

没有答案