using HalconDotNet;
|
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
using System;
|
using System.Collections.Generic;
|
using System.Diagnostics;
|
using System.Drawing;
|
using System.Drawing.Imaging;
|
using System.Threading;
|
|
namespace LB_VisionProcesses.Cameras
|
{
|
public abstract class BaseCamera : ICamera
|
{
|
protected BaseCamera() { }
|
|
#region Parm
|
public string SN { get; set; } = string.Empty;
|
public CameraBrand Brand { get; set; } = CameraBrand.UNSUPPORTED;
|
|
public bool isGrabbing = false;
|
|
//public EventHandler<CameraEventArgs> ImageGrabbed = delegate { };
|
|
private EventHandler<CameraEventArgs> _imageGrabbed;
|
public EventHandler<CameraEventArgs> ImageGrabbed
|
{
|
get => _imageGrabbed;
|
set
|
{
|
Debug.WriteLine($"ImageGrabbed 被设置,新值: {value?.Method}");
|
Debug.WriteLine($"调用堆栈: {Environment.StackTrace}");
|
_imageGrabbed = value;
|
}
|
}
|
|
protected static Bitmap CallBackImg { get; set; }
|
|
private Dictionary<string, List<Bitmap>> _collectedImages;
|
public Dictionary<string, List<Bitmap>> CollectedImages
|
{
|
get => _collectedImages;
|
set
|
{
|
_collectedImages = value;
|
}
|
}
|
|
public Bitmap Bitmap;
|
#endregion
|
|
#region operate
|
|
public virtual void Dispose()
|
{
|
try
|
{
|
CloseDevice();
|
}
|
catch { }
|
}
|
public abstract bool CloseDevice();
|
|
public abstract List<string> GetListEnum();
|
|
public abstract bool InitDevice(string SN, object Handle = null);
|
|
public bool StartWith_SoftTriggerModel()
|
{
|
SetTriggerMode(TriggerMode.Off, TriggerSource.Software);
|
return StartGrabbing();
|
}
|
|
public bool StartWith_HardTriggerModel(TriggerSource hardtriggeritem = TriggerSource.Line0)
|
{
|
if (hardtriggeritem == TriggerSource.Software) hardtriggeritem = TriggerSource.Line0;
|
SetTriggerMode(TriggerMode.On, hardtriggeritem);
|
return StartGrabbing();
|
}
|
|
/// <summary>
|
/// 等待硬触发获取图像
|
/// </summary>
|
/// <param name="bitmap"></param>
|
/// <param name="outtime"></param>
|
/// <returns></returns>
|
public bool GetImage(out Bitmap bitmap, int outtime = 3000)
|
{
|
bitmap = null;
|
|
try
|
{
|
// 设置超时时间
|
DateTime lastTime = DateTime.Now.AddMilliseconds(outtime);
|
// 判断是否超时
|
while (lastTime > DateTime.Now)// 设置超时时间为 3 秒
|
{
|
if (CallBackImg != null)
|
{
|
lock (CallBackImg)
|
{
|
// 保存旧 Bitmap 并释放
|
bitmap = CallBackImg.Clone() as Bitmap; // 创建副本
|
}
|
|
// 释放旧资源
|
CallBackImg.Dispose();
|
CallBackImg = null;
|
return true;
|
}
|
}
|
|
return false;
|
}
|
catch { return bitmap == null ? false : true; }
|
}
|
|
/// <summary>
|
/// 软触发获取图像
|
/// </summary>
|
/// <param name="bitmap"></param>
|
/// <param name="outtime"></param>
|
/// <returns></returns>
|
public bool GetImageWithSoftTrigger(out Bitmap bitmap, int outtime = 3000)
|
{
|
if (!isGrabbing)
|
StartGrabbing();
|
|
GetTriggerMode(out TriggerMode triggerMode, out TriggerSource triggerSource);
|
|
if (triggerMode != TriggerMode.On && triggerSource != TriggerSource.Software)
|
SetTriggerMode(TriggerMode.On, TriggerSource.Software);
|
|
bitmap = null;
|
CallBackImg = null;
|
|
if (!SoftTrigger())
|
return false;
|
|
// 开始时间
|
DateTime startTime = DateTime.Now; // 当前时间
|
|
// 判断是否超时
|
while (DateTime.Now < startTime.AddMilliseconds(outtime))// 设置超时时间为 3 秒
|
{
|
GetImage(out bitmap, 50);
|
if (bitmap != null)
|
break;
|
|
Thread.Sleep(10);
|
}
|
|
if (triggerMode != TriggerMode.On)
|
SetTriggerMode(TriggerMode.On, triggerSource);
|
|
return (bitmap != null);
|
}
|
|
/// <summary>
|
/// 软触发
|
/// </summary>
|
/// <returns></returns>
|
public abstract bool SoftTrigger();
|
|
public void Bitmap2HObject(Bitmap bmp, out HObject image)
|
{
|
try
|
{
|
if (bmp == null)
|
{
|
image = null;
|
return;
|
}
|
BitmapData srcBmpData;
|
switch (bmp.PixelFormat)
|
{
|
case PixelFormat.Format24bppRgb:
|
srcBmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
|
HOperatorSet.GenImageInterleaved(out image, srcBmpData.Scan0, "bgr", bmp.Width, bmp.Height, 0, "byte", 0, 0, 0, 0, -1, 0);
|
bmp.UnlockBits(srcBmpData);
|
break;
|
default:
|
srcBmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, PixelFormat.Format8bppIndexed);
|
|
HOperatorSet.GenImage1(out image, "byte", bmp.Width, bmp.Height, srcBmpData.Scan0);
|
bmp.UnlockBits(srcBmpData);
|
break;
|
}
|
}
|
catch (Exception ex)
|
{
|
image = null;
|
}
|
}
|
|
#endregion
|
|
#region SettingConfig
|
public void SetCamConfig(CameraConfig config)
|
{
|
if (Enum.TryParse(config.Params.Inputs["触发模式"].ToString(), out TriggerMode TriggerMode)
|
&& Enum.TryParse(config.Params.Inputs["触发方式"].ToString(), out TriggerSource TriggerSource)
|
&& Enum.TryParse(config.Params.Inputs["触发极性"].ToString(), out TriggerPolarity TriggerPolarity)
|
)
|
{
|
SetTriggerMode(TriggerMode, TriggerSource);
|
SetTriggerPolarity(TriggerPolarity);
|
SetTriggerFliter(Convert.ToDouble(config.Params.Inputs["触发消抖"].ToString()));
|
SetTriggerDelay(Convert.ToDouble(config.Params.Inputs["触发延时"].ToString()));
|
SetExpouseTime(Convert.ToDouble(config.Params.Inputs["曝光时间"].ToString()));
|
SetGain(Convert.ToDouble(config.Params.Inputs["增益"].ToString()));
|
}
|
}
|
|
public void GetCamConfig(out CameraConfig config)
|
{
|
GetTriggerMode(out TriggerMode triggerMode, out TriggerSource triggerSource);
|
GetTriggerPolarity(out TriggerPolarity triggerPolarity);
|
GetTriggerFliter(out double triggerfilter);
|
GetTriggerDelay(out double triggerdelay);
|
GetExpouseTime(out double expouseTime);
|
GetGain(out double gain);
|
|
config = new CameraConfig(null);
|
config.Params.Inputs.Add("触发模式", triggerMode);
|
config.Params.Inputs.Add("触发方式", triggerSource);
|
config.Params.Inputs.Add("触发极性", triggerPolarity);
|
config.Params.Inputs.Add("触发消抖", triggerfilter);
|
config.Params.Inputs.Add("触发延时", triggerdelay);
|
config.Params.Inputs.Add("曝光时间", expouseTime);
|
config.Params.Inputs.Add("增益", gain);
|
}
|
|
/// <summary>
|
/// 设置触发模式及触发源
|
/// </summary>
|
/// <param name="mode"></param>
|
/// <param name="triggerEnum"></param>
|
/// <returns></returns>
|
public abstract bool SetTriggerMode(TriggerMode mode, TriggerSource triggerEnum = TriggerSource.Line0);
|
|
public abstract bool GetTriggerMode(out TriggerMode mode, out TriggerSource source);
|
|
public abstract bool SetExpouseTime(double value);
|
|
public abstract bool GetExpouseTime(out double value);
|
|
public abstract bool SetTriggerPolarity(TriggerPolarity polarity);
|
|
public abstract bool GetTriggerPolarity(out TriggerPolarity polarity);
|
|
/// <summary>
|
/// 设置触发滤波时间 (us)
|
/// </summary>
|
/// <param name="flitertime"></param>
|
/// <returns></returns>
|
public abstract bool SetTriggerFliter(double flitertime);
|
|
/// <summary>
|
/// 获取触发参数时间 (us)
|
/// </summary>
|
/// <param name="flitertime"></param>
|
/// <returns></returns>
|
public abstract bool GetTriggerFliter(out double flitertime);
|
|
public abstract bool SetTriggerDelay(double delay);
|
|
public abstract bool GetTriggerDelay(out double delay);
|
|
public abstract bool SetGain(double gain);
|
|
public abstract bool GetGain(out double gain);
|
|
public abstract bool SetLineMode(IOLines line, LineMode mode);
|
|
public abstract bool SetLineStatus(IOLines line, LineStatus linestatus);
|
|
public abstract bool GetLineStatus(IOLines line, out LineStatus lineStatus);
|
|
public abstract bool AutoBalanceWhite();
|
|
#endregion
|
|
#region protected abstract
|
/// <summary>
|
/// 开始采图
|
/// </summary>
|
/// <returns></returns>
|
public abstract bool StartGrabbing();
|
|
/// <summary>
|
/// 停止采图
|
/// </summary>
|
/// <returns></returns>
|
public abstract bool StopGrabbing();
|
#endregion
|
}
|
}
|