如何使用 EmguCV c# 将 IP 摄像机快照保存在内容文件夹中
How to save IP camera snapshot in Content folder using EmguCV c#
我正在使用 EmguCV 3.4。我需要定期(5 分钟)存储 IP 摄像机的快照。我尝试将快照存储在 Content 文件夹 (ASP.NET MVC) 中。我得到了访问冲突异常。帮我。
我的代码,
private VideoCapture _capture = null;
private Mat _frame;
public void GetSnapshot(CameraDTO cameraDTO)
{
CvInvoke.UseOpenCL = false;
try
{
_capture = new VideoCapture(cameraDTO.CameraAccessURL);
_capture.ImageGrabbed += ProcessFrame;
if (StartCapture())
{
while (_frame == null)
{
//wait untill camera ready
}
if (_frame != null && _capture != null)
{
Image<Bgr, Byte> imgeOrigenal = _frame.ToImage<Bgr, Byte>();
imgeOrigenal.Save(@ImageSavepath + @"\ImageFromCamera" + cameraDTO.camID + ".jpg");
}
}
}
catch (Exception excpt)
{
Console.WriteLine(excpt.Message);
}
}
private bool StartCapture()
{
if (_capture != null)
{
_capture.Start();
return true;
}
return false;
}
private void ProcessFrame(object sender, EventArgs e)
{
try
{
if (_capture != null && _capture.IsOpened && _capture.Ptr != IntPtr.Zero && _frame != null)
{
_frame = new Mat();
_capture.Retrieve(_frame, 0);
}
}
catch (AccessViolationException exv)
{
Console.WriteLine("ERROR: {0}", exv.Message);
return;
}
catch (Exception ex)
{
Console.WriteLine("ERROR: {0}", ex.Message);
return;
}
}
@ImageSavepath = 内容文件夹,随机生成问题
问题是您在另一个线程中使用了 _frame
(跨线程操作)。您应该将图像保存在 FrameProcess()
.
中
private void FrameProcess(object sender, EventArgs e)
{
try
{
if (_capture != null && _capture.IsOpened && _capture.Ptr != IntPtr.Zero && _frame != null)
{
_frame = new Mat();
_capture.Retrieve(_frame, 0);
Image<Bgr, Byte> imgeOrigenal = _frame.ToImage<Bgr, Byte>();
imgeOrigenal.Save(@ImageSavepath + @"\ImageFromCamera" + cameraDTO.camID + ".jpg");
}
}
catch (AccessViolationException exv)
{
Console.WriteLine("ERROR: {0}", exv.Message);
return;
}
catch (Exception ex)
{
Console.WriteLine("ERROR: {0}", ex.Message);
return;
}
}
我正在使用 EmguCV 3.4。我需要定期(5 分钟)存储 IP 摄像机的快照。我尝试将快照存储在 Content 文件夹 (ASP.NET MVC) 中。我得到了访问冲突异常。帮我。 我的代码,
private VideoCapture _capture = null;
private Mat _frame;
public void GetSnapshot(CameraDTO cameraDTO)
{
CvInvoke.UseOpenCL = false;
try
{
_capture = new VideoCapture(cameraDTO.CameraAccessURL);
_capture.ImageGrabbed += ProcessFrame;
if (StartCapture())
{
while (_frame == null)
{
//wait untill camera ready
}
if (_frame != null && _capture != null)
{
Image<Bgr, Byte> imgeOrigenal = _frame.ToImage<Bgr, Byte>();
imgeOrigenal.Save(@ImageSavepath + @"\ImageFromCamera" + cameraDTO.camID + ".jpg");
}
}
}
catch (Exception excpt)
{
Console.WriteLine(excpt.Message);
}
}
private bool StartCapture()
{
if (_capture != null)
{
_capture.Start();
return true;
}
return false;
}
private void ProcessFrame(object sender, EventArgs e)
{
try
{
if (_capture != null && _capture.IsOpened && _capture.Ptr != IntPtr.Zero && _frame != null)
{
_frame = new Mat();
_capture.Retrieve(_frame, 0);
}
}
catch (AccessViolationException exv)
{
Console.WriteLine("ERROR: {0}", exv.Message);
return;
}
catch (Exception ex)
{
Console.WriteLine("ERROR: {0}", ex.Message);
return;
}
}
@ImageSavepath = 内容文件夹,随机生成问题
问题是您在另一个线程中使用了 _frame
(跨线程操作)。您应该将图像保存在 FrameProcess()
.
private void FrameProcess(object sender, EventArgs e)
{
try
{
if (_capture != null && _capture.IsOpened && _capture.Ptr != IntPtr.Zero && _frame != null)
{
_frame = new Mat();
_capture.Retrieve(_frame, 0);
Image<Bgr, Byte> imgeOrigenal = _frame.ToImage<Bgr, Byte>();
imgeOrigenal.Save(@ImageSavepath + @"\ImageFromCamera" + cameraDTO.camID + ".jpg");
}
}
catch (AccessViolationException exv)
{
Console.WriteLine("ERROR: {0}", exv.Message);
return;
}
catch (Exception ex)
{
Console.WriteLine("ERROR: {0}", ex.Message);
return;
}
}