|
@@ -27,8 +27,6 @@ namespace CCDCount.DLL
|
|
|
|
|
|
private List<IDeviceInfo> CamList; // 相机列表
|
|
private List<IDeviceInfo> CamList; // 相机列表
|
|
private IDevice device = null; // 相机实例
|
|
private IDevice device = null; // 相机实例
|
|
- public bool IsGrabbing { get { return _isGrabbing; }}
|
|
|
|
- private bool _isGrabbing = false; // 是否正在取图
|
|
|
|
private Thread receiveThread = null; // 接收图像线程
|
|
private Thread receiveThread = null; // 接收图像线程
|
|
private ConcurrentQueue<IFrameOut> FrameOuts = new ConcurrentQueue<IFrameOut>();
|
|
private ConcurrentQueue<IFrameOut> FrameOuts = new ConcurrentQueue<IFrameOut>();
|
|
private ConcurrentQueue<byte[]> ImageBytes = new ConcurrentQueue<byte[]>(); //图像接受队列
|
|
private ConcurrentQueue<byte[]> ImageBytes = new ConcurrentQueue<byte[]>(); //图像接受队列
|
|
@@ -224,17 +222,7 @@ namespace CCDCount.DLL
|
|
/// </summary>
|
|
/// </summary>
|
|
public void StartCamera()
|
|
public void StartCamera()
|
|
{
|
|
{
|
|
- //StartEventGetImage();
|
|
|
|
StartReceiveFuntion();
|
|
StartReceiveFuntion();
|
|
- //StartImageRowsDataFuntion();
|
|
|
|
- // 开始采集
|
|
|
|
- if (!_isGrabbing)
|
|
|
|
- {
|
|
|
|
- _isGrabbing = false;
|
|
|
|
- receiveThread.Join();
|
|
|
|
- LOG.log(string.Format("{0}:采集线程开启失败", "CameraClass-StartCamera"));
|
|
|
|
- return;
|
|
|
|
- }
|
|
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// <summary>
|
|
@@ -242,7 +230,7 @@ namespace CCDCount.DLL
|
|
/// </summary>
|
|
/// </summary>
|
|
public void StopCamera()
|
|
public void StopCamera()
|
|
{
|
|
{
|
|
- StopReceiveAndImageRowsDataFuntion();
|
|
|
|
|
|
+ StopReceiveFuntion();
|
|
//StopEventGetImage();
|
|
//StopEventGetImage();
|
|
if(device!=null)
|
|
if(device!=null)
|
|
{
|
|
{
|
|
@@ -256,36 +244,28 @@ namespace CCDCount.DLL
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
}
|
|
-
|
|
|
|
- /// <summary>
|
|
|
|
- /// 获取第一张缓存中的图像,并把这个图像从缓存中清楚
|
|
|
|
- /// </summary>
|
|
|
|
- /// <param name="ImageData"></param>
|
|
|
|
- public bool GetOnceImage(out byte[] ImageData)
|
|
|
|
|
|
+ public bool GetOnceImage(out IFrameOut IFrameData)
|
|
{
|
|
{
|
|
bool result = false;
|
|
bool result = false;
|
|
- //初始化图像数据
|
|
|
|
- ImageData = null;
|
|
|
|
- //判断是否可以给图像数据赋值
|
|
|
|
- if(ImageBytes.Count()!=0)
|
|
|
|
|
|
+ int BRet = device.StreamGrabber.GetImageBuffer(1000, out IFrameData);
|
|
|
|
+ if (BRet == 0 && IFrameData != null) result = true;
|
|
|
|
+ else return result;
|
|
|
|
+ if (lastframeNum == -1)
|
|
{
|
|
{
|
|
- ImageData = new byte[ImageBytes.Last().Count()];
|
|
|
|
- ImageBytes.TryDequeue(out ImageData);
|
|
|
|
- result = true;
|
|
|
|
|
|
+ lastframeNum = IFrameData.FrameNum;
|
|
}
|
|
}
|
|
- return result;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- public bool GetOnceImage(out IFrameOut IFrameData)
|
|
|
|
- {
|
|
|
|
- bool result = false;
|
|
|
|
- //初始化图像数据
|
|
|
|
- IFrameData = null;
|
|
|
|
- //判断是否可以给图像数据赋值
|
|
|
|
- if (FrameOuts.Count() != 0)
|
|
|
|
|
|
+ else if (lastframeNum == IFrameData.FrameNum - 1)
|
|
{
|
|
{
|
|
- FrameOuts.TryDequeue(out IFrameData);
|
|
|
|
- result = true;
|
|
|
|
|
|
+ lastframeNum = IFrameData.FrameNum;
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ //丢帧记录
|
|
|
|
+ LOG.log(string.Format("lost frame: Width[{0}] , Height[{1}] , FrameNum[{2}] ,Frevous[{3}]",
|
|
|
|
+ IFrameData.Image.Width, IFrameData.Image.Height, IFrameData.FrameNum - 1, lastframeNum), 6);
|
|
|
|
+ Console.WriteLine("lost frame: Width[{0}] , Height[{1}] , FrameNum[{2}] ,Frevous[{3}]",
|
|
|
|
+ IFrameData.Image.Width, IFrameData.Image.Height, IFrameData.FrameNum - 1, lastframeNum);
|
|
|
|
+ lastframeNum = IFrameData.FrameNum;
|
|
}
|
|
}
|
|
return result;
|
|
return result;
|
|
}
|
|
}
|
|
@@ -363,63 +343,6 @@ namespace CCDCount.DLL
|
|
|
|
|
|
#region 私有方法
|
|
#region 私有方法
|
|
|
|
|
|
- /// <summary>
|
|
|
|
- /// 图像分割线程
|
|
|
|
- /// </summary>
|
|
|
|
- private void ImageRowsDataProcess()
|
|
|
|
- {
|
|
|
|
- IFrameOut frameOut;
|
|
|
|
- Queue<byte[]> frameDatas = null;
|
|
|
|
- byte[] data = null;
|
|
|
|
- while (_isGrabbing)
|
|
|
|
- {
|
|
|
|
- if(FrameOuts.Count == 0) Thread.Sleep(5);
|
|
|
|
- bool BRet = FrameOuts.TryDequeue(out frameOut);
|
|
|
|
- if (BRet)
|
|
|
|
- {
|
|
|
|
- frameDatas = GetImageRowsData(frameOut);
|
|
|
|
- try
|
|
|
|
- {
|
|
|
|
- while (frameDatas!= null && frameDatas.Count > 0)
|
|
|
|
- {
|
|
|
|
- data = frameDatas.Dequeue();
|
|
|
|
- ImageBytes.Enqueue(data.Clone() as byte[]);
|
|
|
|
- if (ImageBytes.Count > 500)
|
|
|
|
- Console.WriteLine("ImageBytes.Count:" + ImageBytes.Count);
|
|
|
|
- data = null;
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- catch (Exception e)
|
|
|
|
- {
|
|
|
|
- LOG.error("CameraClass_ImageRowsDataProcess " + e.Message);
|
|
|
|
- return;
|
|
|
|
- }
|
|
|
|
- device.StreamGrabber.FreeImageBuffer(frameOut);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /// <summary>
|
|
|
|
- /// 开启图像分割
|
|
|
|
- /// </summary>
|
|
|
|
- private void StartImageRowsDataFuntion()
|
|
|
|
- {
|
|
|
|
- try
|
|
|
|
- {
|
|
|
|
- // 开启线程
|
|
|
|
- if(_isGrabbing)
|
|
|
|
- {
|
|
|
|
- receiveThread = new Thread(ImageRowsDataProcess);
|
|
|
|
- receiveThread.Start();
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- catch (Exception ex)
|
|
|
|
- {
|
|
|
|
- LOG.error("Start thread failed!, " + ex.Message);
|
|
|
|
- throw;
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
/// <summary>
|
|
/// <summary>
|
|
/// 开启采集
|
|
/// 开启采集
|
|
/// </summary>
|
|
/// </summary>
|
|
@@ -427,17 +350,8 @@ namespace CCDCount.DLL
|
|
{
|
|
{
|
|
try
|
|
try
|
|
{
|
|
{
|
|
- // 标志位置位true
|
|
|
|
- _isGrabbing = true;
|
|
|
|
device.StreamGrabber.SetImageNodeNum(100);
|
|
device.StreamGrabber.SetImageNodeNum(100);
|
|
device.StreamGrabber.StartGrabbing();
|
|
device.StreamGrabber.StartGrabbing();
|
|
-
|
|
|
|
- // 开启线程
|
|
|
|
- receiveThread = new Thread(ReceiveThreadProcess)
|
|
|
|
- {
|
|
|
|
- Priority = ThreadPriority.Highest
|
|
|
|
- };
|
|
|
|
- receiveThread.Start();
|
|
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
catch (Exception ex)
|
|
{
|
|
{
|
|
@@ -447,14 +361,12 @@ namespace CCDCount.DLL
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// <summary>
|
|
- /// 关闭采集线程
|
|
|
|
|
|
+ /// 关闭采集
|
|
/// </summary>
|
|
/// </summary>
|
|
- private void StopReceiveAndImageRowsDataFuntion()
|
|
|
|
|
|
+ private void StopReceiveFuntion()
|
|
{
|
|
{
|
|
try
|
|
try
|
|
{
|
|
{
|
|
- // 标志位设为false
|
|
|
|
- _isGrabbing = false;
|
|
|
|
if (device == null) return;
|
|
if (device == null) return;
|
|
int ret = device.StreamGrabber.StopGrabbing();
|
|
int ret = device.StreamGrabber.StopGrabbing();
|
|
if (ret != MvError.MV_OK)
|
|
if (ret != MvError.MV_OK)
|
|
@@ -462,8 +374,6 @@ namespace CCDCount.DLL
|
|
LOG.error(("Stop grabbing failed:{0:x8}", ret));
|
|
LOG.error(("Stop grabbing failed:{0:x8}", ret));
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
- if (receiveThread != null&&receiveThread.IsAlive)
|
|
|
|
- receiveThread.Join();
|
|
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
catch (Exception ex)
|
|
{
|
|
{
|
|
@@ -472,40 +382,6 @@ namespace CCDCount.DLL
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- /// <summary>
|
|
|
|
- /// 采集线程
|
|
|
|
- /// </summary>
|
|
|
|
- private void ReceiveThreadProcess()
|
|
|
|
- {
|
|
|
|
- while (_isGrabbing)
|
|
|
|
- {
|
|
|
|
- IFrameOut frameOut;
|
|
|
|
- int BRet = device.StreamGrabber.GetImageBuffer(100, out frameOut);
|
|
|
|
- if (BRet == MvError.MV_OK)
|
|
|
|
- {
|
|
|
|
- FrameOuts.Enqueue(frameOut.Clone() as IFrameOut);
|
|
|
|
- if (lastframeNum == -1)
|
|
|
|
- {
|
|
|
|
- lastframeNum = frameOut.FrameNum;
|
|
|
|
- }
|
|
|
|
- else if (lastframeNum == frameOut.FrameNum - 1)
|
|
|
|
- {
|
|
|
|
- lastframeNum = frameOut.FrameNum;
|
|
|
|
- }
|
|
|
|
- else
|
|
|
|
- {
|
|
|
|
- //丢帧记录
|
|
|
|
- LOG.log(string.Format("lost frame: Width[{0}] , Height[{1}] , FrameNum[{2}] ,Frevous[{3}]",
|
|
|
|
- frameOut.Image.Width, frameOut.Image.Height, frameOut.FrameNum - 1, lastframeNum), 5);
|
|
|
|
- Console.WriteLine("lost frame: Width[{0}] , Height[{1}] , FrameNum[{2}] ,Frevous[{3}]",
|
|
|
|
- frameOut.Image.Width, frameOut.Image.Height, frameOut.FrameNum - 1, lastframeNum);
|
|
|
|
- lastframeNum = frameOut.FrameNum;
|
|
|
|
- }
|
|
|
|
- device.StreamGrabber.FreeImageBuffer(frameOut);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
/// <summary>
|
|
/// <summary>
|
|
/// 读取图像像素信息(单行)
|
|
/// 读取图像像素信息(单行)
|
|
/// </summary>
|
|
/// </summary>
|
|
@@ -539,75 +415,6 @@ namespace CCDCount.DLL
|
|
}
|
|
}
|
|
return PixelDatas;
|
|
return PixelDatas;
|
|
}
|
|
}
|
|
-
|
|
|
|
- /// <summary>
|
|
|
|
- /// 帧获取回调事件
|
|
|
|
- /// </summary>
|
|
|
|
- /// <param name="sender"></param>
|
|
|
|
- /// <param name="e"></param>
|
|
|
|
- private void FrameGrabedEventHandler(object sender, FrameGrabbedEventArgs e)
|
|
|
|
- {
|
|
|
|
- if (e.FrameOut.Image.PixelData.Count() == 0)
|
|
|
|
- {
|
|
|
|
- LOG.log("FrameGrabedEventHandler: PixelData is null", 0);
|
|
|
|
- return;
|
|
|
|
- }
|
|
|
|
- if (!_isGrabbing) return;
|
|
|
|
- FrameOuts.Enqueue(e.FrameOut.Clone() as IFrameOut);
|
|
|
|
- if (lastframeNum == -1)
|
|
|
|
- {
|
|
|
|
- lastframeNum = e.FrameOut.FrameNum;
|
|
|
|
- }
|
|
|
|
- else if (lastframeNum == e.FrameOut.FrameNum - 1)
|
|
|
|
- {
|
|
|
|
- lastframeNum = e.FrameOut.FrameNum;
|
|
|
|
- }
|
|
|
|
- else
|
|
|
|
- {
|
|
|
|
- //丢帧记录
|
|
|
|
- LOG.log(string.Format("lost frame: Width[{0}] , Height[{1}] , FrameNum[{2}] ,Frevous[{3}]",
|
|
|
|
- e.FrameOut.Image.Width, e.FrameOut.Image.Height, e.FrameOut.FrameNum - 1, lastframeNum), 5);
|
|
|
|
- lastframeNum = e.FrameOut.FrameNum;
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /// <summary>
|
|
|
|
- /// 开启事件获取图像
|
|
|
|
- /// </summary>
|
|
|
|
- private void StartEventGetImage()
|
|
|
|
- {
|
|
|
|
- //ch: 设置合适的缓存节点数量 | en: Setting the appropriate number of image nodes
|
|
|
|
- device.StreamGrabber.SetImageNodeNum(50);
|
|
|
|
-
|
|
|
|
- // ch:注册回调函数 | en:Register image callback
|
|
|
|
- device.StreamGrabber.FrameGrabedEvent += FrameGrabedEventHandler;
|
|
|
|
-
|
|
|
|
- // ch:开启抓图 || en: start grab image
|
|
|
|
- int ret = device.StreamGrabber.StartGrabbing();
|
|
|
|
- if (ret != MvError.MV_OK)
|
|
|
|
- {
|
|
|
|
- LOG.error(string.Format("Start grabbing failed:{0}", ret));
|
|
|
|
- return;
|
|
|
|
- }
|
|
|
|
- Console.ReadLine();
|
|
|
|
-
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /// <summary>
|
|
|
|
- /// 停止事件获取图像
|
|
|
|
- /// </summary>
|
|
|
|
- private void StopEventGetImage()
|
|
|
|
- {
|
|
|
|
- if (device == null) return;
|
|
|
|
- int ret = device.StreamGrabber.StopGrabbing();
|
|
|
|
- if (ret != MvError.MV_OK)
|
|
|
|
- {
|
|
|
|
- Console.WriteLine("Stop grabbing failed:{0:x8}", ret);
|
|
|
|
- return;
|
|
|
|
- }
|
|
|
|
- // ch:注册回调函数 | en:Register image callback
|
|
|
|
- device.StreamGrabber.FrameGrabedEvent -= FrameGrabedEventHandler;
|
|
|
|
- }
|
|
|
|
#endregion
|
|
#endregion
|
|
}
|
|
}
|
|
}
|
|
}
|