当前位置:网站首页>C # joint Halcon application - Dahua camera acquisition class
C # joint Halcon application - Dahua camera acquisition class
2022-07-01 19:58:00 【123 mengye】
Dahua camera acquisition class
class Camera // Dahua cameras
{
private IDevice m_dev; /* Device objects */
List<IGrabbedRawData> m_frameList = new List<IGrabbedRawData>(); /* Image cache list */
Thread renderThread = null; /* Show threads */
bool m_bShowLoop = true; /* Thread control variables */
Mutex m_mutex = new Mutex(); /* lock , Ensure multithreading safety */
// public event Action<HObject> NewImage; // The image shows the event
public Camera()
{
if (null == renderThread)
{
renderThread = new Thread(new ThreadStart(ShowThread));
renderThread.IsBackground = true;
renderThread.Start();
}
m_stopWatch.Start();// The timing starts
}
/* Transcoding display thread */
private void ShowThread()
{
while (m_bShowLoop)// The thread control variable is True
{
if (m_frameList.Count == 0) // Image cache list
{
Thread.Sleep(10);
continue;
}
/* The image queue takes the latest frame */
m_mutex.WaitOne();// Block current thread , Until the current signal is received
IGrabbedRawData frame = m_frameList.ElementAt(0);// Returns the element at the specified index in the queue
m_frameList.RemoveAt(0);
m_frameList.Clear();
m_mutex.ReleaseMutex();
/* Actively call garbage collection */
GC.Collect();
/* The maximum frame rate of control display is 25FPS */
if (false == isTimeToDisplay())
{
continue;
}
try
{
/* Image transcoding bitmap Images */
var bitmap = frame.ToBitmap(false);
// Use the collected image here
//if (NewImage != null)
//{
// NewImage(image);
//}
}
catch
{
}
}
}
const int DEFAULT_INTERVAL = 40;// Default interval
Stopwatch m_stopWatch = new Stopwatch(); /* Time counter */
/* Judge whether the display operation should be done */
private bool isTimeToDisplay()
{
m_stopWatch.Stop();
long m_lDisplayInterval = m_stopWatch.ElapsedMilliseconds;// Get the total time measured by the instance
if (m_lDisplayInterval <= DEFAULT_INTERVAL)
{
m_stopWatch.Start();
return false;
}
else
{
m_stopWatch.Reset();
m_stopWatch.Start();
return true;
}
}
/* Camera open callback */
private void OnCameraOpen(object sender, EventArgs e)
{
// Record here that the camera has been turned on
}
/* Camera off callback */
private void OnCameraClose(object sender, EventArgs e)
{
// Record here that the camera is off
}
/* Camera lost callback */
private void OnConnectLoss(object sender, EventArgs e)
{
m_dev.ShutdownGrab();
m_dev.Dispose();
m_dev = null;
}
/* Bitstream data callback */
private void OnImageGrabbed(Object sender, GrabbedEventArgs e)
{
m_mutex.WaitOne();// m_mutex lock , Ensure multithreading safety
m_frameList.Add(e.GrabResult.Clone());/* Image cache list */
m_mutex.ReleaseMutex();
}
/// <summary>
/// Turn on camera
/// </summary>
/// <returns> If the return string is empty, it means that the opening is successful , If it is not empty, it means that the opening fails , The string content is exception information </returns>
/// <param name="triggerSource"> Trigger source selection Soft trigger or external trigger </param>
/// <returns></returns>
public string Open(string triggerSource)
{
string sRet = string.Empty;
try
{
/* Device search */
List<IDeviceInfo> li = Enumerator.EnumerateDevices();
if (li.Count > 0)
{
/* Get the first device searched */
m_dev = Enumerator.GetDeviceByIndex(0);
/* Register link events */
m_dev.CameraOpened += OnCameraOpen;
m_dev.ConnectionLost += OnConnectLoss;
m_dev.CameraClosed += OnCameraClose;
/* Turn on the device */
if (!m_dev.Open())
sRet = " Failed to open camera ";
/* Set up TriggerSource Soft trigger or external trigger */
m_dev.TriggerSet.Open(triggerSource);
/* Format image */
using (IEnumParameter p = m_dev.ParameterCollection[ParametrizeNameSet.ImagePixelFormat])
{
p.SetValue("Mono8");
}
/* Set the number of caches to 8( The default value is 16) */
m_dev.StreamGrabber.SetBufferCount(8);
/* Register the bitstream callback event */
m_dev.StreamGrabber.ImageGrabbed += OnImageGrabbed;
/* Start bitstream */
if (!m_dev.GrabUsingGrabLoopThread())
sRet = " Failed to start bitstream ";
}
else
{
sRet = " Camera device not found ";
}
}
catch (Exception e)
{
sRet = e.Message;
}
return sRet;
}
/// <summary>
/// Turn off camera
/// </summary>
/// <returns> If the return string is empty, it means that the shutdown is successful , If it is not empty, it means closing failed , The string content is exception information </returns>
public string Close()
{
string sRet = String.Empty;
1 try
{
if (m_dev == null)
{
sRet = "Device is invalid";
}
m_dev.StreamGrabber.ImageGrabbed -= OnImageGrabbed; /* Unregister callback */
m_dev.ShutdownGrab(); /* Stop the bitstream */
m_dev.Close(); /* Turn off camera */
}
catch (Exception e)
{
sRet = e.Message;
}
return sRet;
}
/* The window closed */
public void OnClosed(EventArgs e)
{
if (m_dev != null)
{
m_dev.Dispose();
m_dev = null;
}
m_bShowLoop = false;
renderThread.Join();
}
/// <summary>
/// Set the trigger mode Set under soft trigger On Post free pull flow ( Continuous trigger )Off( Single trigger )
/// </summary>
/// <param name="value"></param>
public string SetTriggerMode(string value = "On")
{
if (m_dev == null)
{
return "Device is invalid";
}
if (!m_dev.IsOpen)
{
return " The camera is not turned on ";
}
using (IEnumParameter p = m_dev.ParameterCollection[ParametrizeNameSet.TriggerMode])
{
p.SetValue(value);
}
return "";
}
/// <summary>
/// Set the exposure
/// </summary>
/// <param name="value"></param>
public string SetExposureTime(float value)
{
if (m_dev == null)
{
return "Device is invalid";
}
if (!m_dev.IsOpen)
{
return " The camera is not turned on ";
}
double min = GetMinExposureTime();
double max = GetMaxExposureTime();
if (value < min || value > max)
{
return string.Format(" Parameter value is out of range ,min:{0},max:{1}", min, max);
}
using (IFloatParameter p = m_dev.ParameterCollection[ParametrizeNameSet.ExposureTime])
{
p.SetValue(value);
}
return "";
}
/// <summary>
/// Set the gain
/// </summary>
/// <param name="value"></param>
public string SetGainRaw(float value)
{
if (m_dev == null)
{
return "Device is invalid";
}
if (!m_dev.IsOpen)
{
return " The camera is not turned on ";
}
double min = GetMinGainRaw();
double max = GetMaxGainRaw();
if (value < min || value > max)
{
return string.Format(" Parameter value is out of range ,min:{0},max:{1}", min, max);
}
using (IFloatParameter p = m_dev.ParameterCollection[ParametrizeNameSet.GainRaw])
{
p.SetValue(value);
}
return "";
}
/// <summary>
/// Set the width of the image
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public string SetImageWidth(int value)
{
if (m_dev == null)
{
return "Device is invalid";
}
if (!m_dev.IsOpen)
{
return " The camera is not turned on ";
}
double min = GetMinImageWidth();
double max = GetMaxImageWidth();
if (value < min || value > max)
{
return string.Format(" Parameter value is out of range ,min:{0},max:{1}", min, max);
}
using (IIntegraParameter p = m_dev.ParameterCollection[ParametrizeNameSet.ImageWidth])
{
p.SetValue(value);
}
return "";
}
/// <summary>
/// Set the width of the image
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public string SetImageHeight(int value)
{
if (m_dev == null)
{
return "Device is invalid";
}
if (!m_dev.IsOpen)
{
return " The camera is not turned on ";
}
double min = GetMinImageHeight();
double max = GetMaxImageHeight();
if (value < min || value > max)
{
return string.Format(" Parameter value is out of range ,min:{0},max:{1}", min, max);
}
using (IIntegraParameter p = m_dev.ParameterCollection[ParametrizeNameSet.ImageHeight])
{
p.SetValue(value);
}
return "";
}
/// <summary>
/// Set image offset X
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public string SetImageOffsetX(int value)
{
if (m_dev == null)
{
return "Device is invalid";
}
if (!m_dev.IsOpen)
{
return " The camera is not turned on ";
}
double min = GetMinImageOffsetX();
double max = GetMaxImageOffsetX();
if (value < min || value > max)
{
return string.Format(" Parameter value is out of range ,min:{0},max:{1}", min, max);
}
using (IIntegraParameter p = m_dev.ParameterCollection[ParametrizeNameSet.ImageOffsetX])
{
p.SetValue(value);
}
return "";
}
/// <summary>
/// Set image offset Y
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public string SetImageOffsetY(int value)
{
if (m_dev == null)
{
return "Device is invalid";
}
if (!m_dev.IsOpen)
{
return " The camera is not turned on ";
}
double min = GetMinImageOffsetY();
double max = GetMaxImageOffsetY();
if (value < min || value > max)
{
return string.Format(" Parameter value is out of range ,min:{0},max:{1}", min, max);
}
using (IIntegraParameter p = m_dev.ParameterCollection[ParametrizeNameSet.ImageOffsetY])
{
p.SetValue(value);
}
return "";
}
#region Get the value range of camera parameters
public double GetMinExposureTime()
{
using (IFloatParameter p = m_dev.ParameterCollection[ParametrizeNameSet.ExposureTime])
{
return p.GetMinimum();
}
}
public double GetMaxExposureTime()
{
using (IFloatParameter p = m_dev.ParameterCollection[ParametrizeNameSet.ExposureTime])
{
return p.GetMaximum();
}
}
public double GetMinGainRaw()
{
using (IFloatParameter p = m_dev.ParameterCollection[ParametrizeNameSet.GainRaw])
{
return p.GetMinimum();
}
}
public double GetMaxGainRaw()
{
using (IFloatParameter p = m_dev.ParameterCollection[ParametrizeNameSet.GainRaw])
{
return p.GetMaximum();
}
}
public long GetMinImageWidth()
{
using (IIntegraParameter p = m_dev.ParameterCollection[ParametrizeNameSet.ImageWidth])
{
return p.GetMinimum();
}
}
public long GetMaxImageWidth()
{
using (IIntegraParameter p = m_dev.ParameterCollection[ParametrizeNameSet.ImageWidth])
{
return p.GetMaximum();
}
}
public long GetMinImageHeight()
{
using (IIntegraParameter p = m_dev.ParameterCollection[ParametrizeNameSet.ImageHeight])
{
return p.GetMinimum();
}
}
public long GetMaxImageHeight()
{
using (IIntegraParameter p = m_dev.ParameterCollection[ParametrizeNameSet.ImageHeight])
{
return p.GetMaximum();
}
}
public long GetMinImageOffsetX()
{
using (IIntegraParameter p = m_dev.ParameterCollection[ParametrizeNameSet.ImageOffsetX])
{
return p.GetMinimum();
}
}
public long GetMaxImageOffsetX()
{
using (IIntegraParameter p = m_dev.ParameterCollection[ParametrizeNameSet.ImageOffsetX])
{
return p.GetMaximum();
}
}
public long GetMinImageOffsetY()
{
using (IIntegraParameter p = m_dev.ParameterCollection[ParametrizeNameSet.ImageOffsetY])
{
return p.GetMinimum();
}
}
public long GetMaxImageOffsetY()
{
using (IIntegraParameter p = m_dev.ParameterCollection[ParametrizeNameSet.ImageOffsetY])
{
return p.GetMaximum();
}
}
#endregion
/// <summary>
/// bitmap Image to HObject
/// </summary>
/// <param name="bmp"></param>
/// <returns></returns>
//private HObject Bitmap2HObjectBpp24(Bitmap bmp)
//{
// HObject Hobj;
// HOperatorSet.GenEmptyObj(out Hobj);
// try
// {
// Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
// BitmapData srcBmpData = bmp.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
// HOperatorSet.GenImageInterleaved(out Hobj, srcBmpData.Scan0, "bgr", bmp.Width, bmp.Height, 0, "byte", 0, 0, 0, 0, -1, 0);
// bmp.UnlockBits(srcBmpData);
// }
// catch (Exception e)
// {
// }
// return Hobj;
//}
}
边栏推荐
猜你喜欢

HLS4ML报错The board_part definition was not found for tul.com.tw:pynq-z2:part0:1.0.

GC garbage collection

Easycvr accesses the equipment through the national standard gb28181 protocol. What is the reason for the automatic streaming of the equipment?
![[research materials] iResearch tide Watching: seven major trends in the clothing industry - Download attached](/img/c8/a205ddc2835c87efa38808cf31f59e.jpg)
[research materials] iResearch tide Watching: seven major trends in the clothing industry - Download attached

SQL 入门计划-1-选择

Optimization of video streaming with repeated requests in the case of unstable easygbs network

由浅入深学会白盒测试用例设计

JVM内存模型

面试题篇一

简单但现代的服务器仪表板Dashdot
随机推荐
Tensorflow reports an error, could not load dynamic library 'libcudnn so. eight
DS Transunet:用于医学图像分割的双Swin-Transformer U-Net
面试题篇一
Time series analysis using kibana timelion
Interview questions shared in today's group
list大集合等比分割成多个小list集合
How can a programmer grow rapidly
Example explanation: move graph explorer to jupyterlab
MySQL signale une erreur can 't create table' demo01. TB Étudiant '(errno: 150)
有意思了!数据库也搞Serverless!
Class loading mechanism
Interview question 1
【C语言】详解 memset() 函数用法
AAAI2020: Real-time Scene Text Detection with Differentiable Binarization
PowerDesigner设计Name和Comment 替换
qobject_ Cast usage
HLS4ML/vivado HLS 报错解决方案
Leetcode 1380 lucky numbers in matrix [array] the leetcode path of heroding
Detailed configuration of network security "Splunk" in national vocational college skills competition
上大学后明白了哪些坑人的事?