如何使用C#检测相机是否在工作 [英] How to detect if a camera is working using c#

查看:54
本文介绍了如何使用C#检测相机是否在工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试开发一种相机解决方案,该解决方案将拍摄事件快照并使用c#将其发送到远程服务器.我想发送警报,如果相机没有捕获,禁用或故障.

我用Aforge拍摄了这样的快照

#region AFORGE ORIGINAL

///< summary>
///本节通过初始化来启动相机软件
///</summary>
私有void StartCameras()
{
InitializeCameras();

//创建第一个视频源
VideoCaptureDevice videoSource1 =新的VideoCaptureDevice(videoDevices [int.Parse(System.Configuration.ConfigurationManager.AppSettings ["VideoSourceIndex"].ToString())].MonikerString);
videoSource1.DesiredFrameRate = 10;
videoSourcePlayer1.VideoSource = videoSource1;

videoSourcePlayer1.Start();

}

///< summary>
///此方法用于停止摄像头
///</summary>
私有void StopCameras()
{
videoSourcePlayer1.SignalToStop();
//videoSourcePlayer1.WaitForStop();
}

///< summary>
///相机初始化
///</summary>
私有void InitializeCameras()
{
cameraNames = new ArrayList();

试试
{
videoDevices =新的FilterInfoCollection(FilterCategory.VideoInputDevice);
如果(videoDevices.Count == 0)
{
抛出新的Exception();
}

for(int i = 1,n = videoDevices.Count; i< = n; i ++)
{
字符串cameraName = i +:" + videoDevices [i-1] .Name;

cameraNames.Add(cameraName);

System.IO.File.AppendAllText(Application.StartupPath +"\\ Cams.txt",DateTime.Now.ToString()+" + cameraName +"\ r \ n");
}

//检查摄像头数量
如果(videoDevices.Count == 1)
{
cameraNames.Add(仅找到一台摄像机");

}
其他
{

}


}
赶上
{
cameraNames.Add(找不到照相机");
}
}

///< summary>
///图像的帧捕获
///</summary>
///< param name ="sender"></param>
///< param name ="image"></param>
私有void videoSourcePlayer1_NewFrame(对象发送者,参考位图图像)
{
位图_image =图片;

日期= DateTime.Now.Day.ToString("D2")+ DateTime.Now.Month.ToString("D2")+ DateTime.Now.Year.ToString();
字符串ImageNameAndPath;
试试
{
frameCount ++;

如果(frameCount> int.Parse(ConfigurationManager.AppSettings ["FrameCount"].ToString()))
{
字符串strYear =",strMonth =",strDay =";
strYear = DateTime.Now.Year.ToString().Substring(2);
strMonth = DateTime.Now.Month.ToString().PadLeft(2,``0'');
strDay = DateTime.Now.Day.ToString().PadLeft(2,``0'');

字符串buildPath = strYear +"\\" + strMonth +"\\" + strDay;

如果(!Directory.Exists(ImagePath +"\\" + buildPath))
Directory.CreateDirectory(ImagePath +"\\" + buildPath);

ImageNameAndPath = ImagePath +"\\" + buildPath +"\\" + m_ge​​tIP +"_" +平移+"_DB PLC" + ConfigurationManager.AppSettings ["ATMID"].ToString()+".jpg";

StopCameras();

string [] splitPan = pan.Split(''_'');

如果(splitPan.Length> = 4)
{
//将文字写入图片
使用(Graphics g = Graphics.FromImage(_image))
{
string [] colors = ConfigurationManager.AppSettings ["RGBWaterMarkColour"].ToString().Split('','');
SolidBrush solidBrush =新的SolidBrush(Color.FromArgb(int.Parse(colours [0]),int.Parse(colours [1]),int.Parse(colours [2])));
FontFamily家族=新的FontFamily("Verdana");
字体=新字体(Font.FontFamily,10,FontStyle.Bold,GraphicsUnit.Pixel);
PointF位置=新的PointF(3,3);
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
g.DrawString(splitPan [0] +" + splitPan [1] +"DB PLC" +"\ r \ n" + splitPan [2] +" + splitPan [3] +" + ConfigurationManager.AppSettings ["ATMName].ToString(),字体,solidBrush,位置);
ImageNameAndPath = ImageNameAndPath.Replace(:",");
//image.Save(ImageNameAndPath,ImageFormat.Jpeg);
image.Save(ImagePath +"\\" + buildPath +"\\" + ImageNameAndPath.Substring(ImageNameAndPath.LastIndexOf("\\")+1),ImageFormat.Jpeg);
}
日志+ = FileSystem.LogBuilder("CR2CAM",string.Format("{0}成功地捕获并保存.\ r \ n",ImageNameAndPath.Substring(ImageNameAndPath.LastIndexOf("\\")+ 1))));
如果(!string.IsNullOrEmpty(logPath))
FileSystem.Save(logPath,string.Format("cr2camLog {0} .log",Date),log);
}

frameCount = 0;
}
}
catch(ex ex例外)
{
WriteToEventLog(例如消息+"\ r \ n" +例如StackTrace +"\ r \ n" +捕获事件");
log + = FileSystem.LogBuilder("CR2CAM",string.Format(由于{0}而引起的相机错误.\ r \ n",例如Message));
如果(!string.IsNullOrEmpty(logPath))
FileSystem.Save(logPath,string.Format("cr2camLog {0} .log",Date),log);
connection.Close();
//Application.Restart();
}
}

#endregion

I am trying develop a camera solution that will take snap shots of events and send them to a remote server using c#. I want to send an alert if the camera is not capturing, or disabled or mal-functioning.

I used Aforge to take the snap shots like this

#region AFORGE ORIGINAL

/// <summary>
/// This section starts the camera software by way of initialization
/// </summary>
private void StartCameras()
{
InitializeCameras();

//create first video source
VideoCaptureDevice videoSource1 = new VideoCaptureDevice(videoDevices[int.Parse(System.Configuration.ConfigurationManager.AppSettings["VideoSourceIndex"].ToString())].MonikerString);
videoSource1.DesiredFrameRate = 10;
videoSourcePlayer1.VideoSource = videoSource1;

videoSourcePlayer1.Start();

}

/// <summary>
/// this method is used to stop the camera
/// </summary>
private void StopCameras()
{
videoSourcePlayer1.SignalToStop();
//videoSourcePlayer1.WaitForStop();
}

/// <summary>
/// Camera initialisations
/// </summary>
private void InitializeCameras()
{
cameraNames = new ArrayList();

try
{
videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
if (videoDevices.Count == 0)
{
throw new Exception();
}

for (int i = 1, n = videoDevices.Count; i <= n; i++)
{
string cameraName = i + " : " + videoDevices[i - 1].Name;

cameraNames.Add(cameraName);

System.IO.File.AppendAllText(Application.StartupPath + "\\Cams.txt", DateTime.Now.ToString() + " " + cameraName + "\r\n");
}

//check cameras count
if (videoDevices.Count == 1)
{
cameraNames.Add("Only one camera found");

}
else
{

}


}
catch
{
cameraNames.Add("No camers found");
}
}

/// <summary>
/// Frame capture of images
/// </summary>
/// <param name="sender"></param>
/// <param name="image"></param>
private void videoSourcePlayer1_NewFrame(object sender, ref Bitmap image)
{
Bitmap _image = image;

Date = DateTime.Now.Day.ToString("D2") + DateTime.Now.Month.ToString("D2") + DateTime.Now.Year.ToString();
string ImageNameAndPath;
try
{
frameCount++;

if (frameCount > int.Parse(ConfigurationManager.AppSettings["FrameCount"].ToString()))
{
string strYear = "", strMonth = "", strDay = "";
strYear = DateTime.Now.Year.ToString().Substring(2);
strMonth = DateTime.Now.Month.ToString().PadLeft(2, ''0'');
strDay = DateTime.Now.Day.ToString().PadLeft(2, ''0'');

string buildPath = strYear + "\\" + strMonth + "\\" + strDay;

if (!Directory.Exists(ImagePath + "\\" + buildPath))
Directory.CreateDirectory(ImagePath + "\\" + buildPath);

ImageNameAndPath = ImagePath + "\\" + buildPath + "\\" + m_getIP + "_" + pan + "_DB PLC " + ConfigurationManager.AppSettings["ATMID"].ToString() + ".jpg";

StopCameras();

string[] splitPan = pan.Split(''_'');

if (splitPan.Length >= 4)
{
//Write text to the Image
using (Graphics g = Graphics.FromImage(_image))
{
string[] colours = ConfigurationManager.AppSettings["RGBWaterMarkColour"].ToString().Split('','');
SolidBrush solidBrush = new SolidBrush(Color.FromArgb(int.Parse(colours[0]), int.Parse(colours[1]), int.Parse(colours[2])));
FontFamily family = new FontFamily("Verdana");
Font font = new Font(Font.FontFamily, 10, FontStyle.Bold, GraphicsUnit.Pixel);
PointF location = new PointF(3, 3);
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
g.DrawString(splitPan[0] + " " + splitPan[1] + " DB PLC" + "\r\n" + splitPan[2] + " " + splitPan[3] + " " + ConfigurationManager.AppSettings["ATMName"].ToString(), font, solidBrush, location);
ImageNameAndPath = ImageNameAndPath.Replace(":", "");
//image.Save(ImageNameAndPath, ImageFormat.Jpeg);
image.Save(ImagePath + "\\" + buildPath + "\\" + ImageNameAndPath.Substring(ImageNameAndPath.LastIndexOf("\\") + 1), ImageFormat.Jpeg);
}
log += FileSystem.LogBuilder("CR2CAM", string.Format("{0} successfully snapped and saved.\r\n", ImageNameAndPath.Substring(ImageNameAndPath.LastIndexOf("\\") + 1)));
if (!string.IsNullOrEmpty(logPath))
FileSystem.Save(logPath, string.Format("cr2camLog{0}.log", Date), log);
}

frameCount = 0;
}
}
catch (Exception ex)
{
WriteToEventLog(ex.Message + "\r\n" + ex.StackTrace + "\r\n" + "Capture Event");
log += FileSystem.LogBuilder("CR2CAM", string.Format("Camera Error due to {0}.\r\n", ex.Message));
if (!string.IsNullOrEmpty(logPath))
FileSystem.Save(logPath, string.Format("cr2camLog{0}.log", Date), log);
connection.Close();
//Application.Restart();
}
}

#endregion

推荐答案

为什么不问AForge.NET的作者?

参见此处 http://www.aforgenet.com [ http://www .codeproject.com/Members/Andrew-Kirillov [ ^ ]),文章( http://www.codeproject.com/script/Articles/MemberArticles.aspx?amid = 1181072 [ ^ ]),并在发表文章的页面上与他联系.

—SA
Why not asking the author of AForge.NET?

See here http://www.aforgenet.com[^] or look at Andrews'' profile (http://www.codeproject.com/Members/Andrew-Kirillov[^]), articles (http://www.codeproject.com/script/Articles/MemberArticles.aspx?amid=1181072[^]) and contact him at the page where an article is published.

—SA


这篇关于如何使用C#检测相机是否在工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆