C# 在c语言中使用网络摄像头捕获图像#
我的项目有一部分需要一些帮助。我需要网络摄像头在2秒钟的时间间隔内捕捉由运动检测器触发的3幅图像。这是我的密码。问题是我有三次相同的图像(第一次)C# 在c语言中使用网络摄像头捕获图像#,c#,webcam,image-capture,C#,Webcam,Image Capture,我的项目有一部分需要一些帮助。我需要网络摄像头在2秒钟的时间间隔内捕捉由运动检测器触发的3幅图像。这是我的密码。问题是我有三次相同的图像(第一次) 使用系统; 使用System.Collections.Generic; 使用System.Linq; 使用系统文本; 使用System.IO.Ports; 使用System.IO; 使用系统组件模型; 使用系统数据; 使用System.Threading.Tasks; 使用大屏幕视频; 使用forge.Video.DirectShow; 使用Syst
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.IO.Ports;
使用System.IO;
使用系统组件模型;
使用系统数据;
使用System.Threading.Tasks;
使用大屏幕视频;
使用forge.Video.DirectShow;
使用System.Data.SqlClient;
使用系统图;
使用系统线程;
名称空间专业
{
班级计划
{
静态SerialPort SerialPort=新的SerialPort(“COM4”,9600);
静态过滤器收集网络摄像机;
静态视频捕获设备;
静态int no=1;
静态void Main(字符串[]参数)
{
如果(!serialPort.IsOpen)
serialPort.Open();
控制台。写线(“”);
serialPort.DataReceived+=新的SerialDataReceiveDevenHandler(serialPort_DataReceived);
Console.ReadKey();
}
静态无效serialPort_DataReceived(对象发送方,SerialDataReceivedEventArgs e)
{
字符串_read=serialPort.ReadExisting().ToString();
如果(_read==“ERROR”)
{
WebcamColl=新的FilterInfoCollection(FilterCategory.VideoInputDevice);
设备=新的VideoCaptureDevice(WebcamColl[0]。监视器字符串);
Device.Start();
Device.NewFrame+=新的NewFrameEventHandler(Device\u NewFrame);
Console.ReadLine();
}
}
静态无效设备\u NewFrame(对象发送方,NewFrameEventArgs eventArgs)
{
对于(int i=0;i<3;i++)
{
System.Drawing.Image img=(位图)eventArgs.Frame.Clone();
字符串图像路径;
字符串fileName=“Image”;
fileName=fileName+no.ToString()+“.jpg”;
img.Save(“D:\\aaaa\\”+文件名);
《睡眠》(2000年);
Console.WriteLine(“快照已保存”);
imagePath=“D:\\aaaa\\”+文件名;
FileStream fs=newfilestream(imagePath,FileMode.Open,FileAccess.Read);
BinaryReader br=新的BinaryReader(fs);
字节[]_image=br.ReadBytes((int)fs.Length);
br.Close();
fs.Close();
SqlConnection conn=new SqlConnection(“数据源=UltraLAN\\SQLEXPRESS;初始目录=veritabani;集成安全性=true”);
SqlCommand kmt=新的SqlCommand(“插入到_imageler(_image,kullanici_id,tarih)值(@image,22,getdate())”,conn);
Parameters.Add(“@image”,SqlDbType.image,_image.Length).Value=_image;
尝试
{
conn.Open();
kmt.ExecuteOnQuery();
Console.WriteLine(“将图像保存到数据库成功…”);
否++;
}
捕获(例外情况除外)
{
Console.WriteLine(例如Message.ToString());
}
最后
{
康涅狄格州关闭();
}
}
Device.SignalToStop();
}
}
}
我不完全理解您的代码,但只要我能看到您正在使用.Clone方法获取新图片。这可能是因为所有的图片都和第一张一样。您在哪里为img对象分配新帧图片?有关类似问题,请查看
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();
}
}
}