用c#捕捉摄像头的图像
我需要一些帮助来完成我的项目的一部分。 我需要网络摄像头以运动检测器触发的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=ULTRASLANSQLEXPRESS; 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();
}
}
}
链接地址: http://www.djcxy.com/p/81041.html