using HalconDotNet;
|
using System;
|
using System.Collections.Generic;
|
using System.Diagnostics;
|
using System.Drawing;
|
using System.Drawing.Imaging;
|
using System.Linq;
|
using System.Text;
|
using System.Threading;
|
using System.Threading.Tasks;
|
|
namespace Bro.Device.HikCamera
|
{
|
public class HDevEngineTool
|
{
|
#region 常量
|
|
// path of external procedures
|
public virtual string ProcedurePath { get; set; } = Environment.CurrentDirectory + "\\Vision\\";
|
|
#endregion
|
|
#region 成员变量
|
|
/// <summary>
|
/// 处理过程名
|
/// </summary>
|
public virtual string ProcedureName { get; set; } = "";
|
|
/// <summary>
|
/// hdev程序启动引擎
|
/// </summary>
|
private HalconDotNet.HDevEngine MyEngine = null;
|
|
/// <summary>
|
/// 程序载入工具 .hdev
|
/// </summary>
|
//private HDevProgramCall ProgramCall;
|
|
//HDevProcedure Procedure = null;
|
//private static Dictionary<string, HDevProcedure> ProcedureDict = new Dictionary<string, HDevProcedure>();
|
/// <summary>
|
/// 过程载入工具 .hdvp
|
/// </summary>
|
private HDevProcedureCall ProcedureCall = null;
|
|
//private static Dictionary<string, HDevProcedureCall> ProcedureCallDict = new Dictionary<string, HDevProcedureCall>();
|
|
/// <summary>
|
/// 控制参数字典
|
/// </summary>
|
public Dictionary<string, HTuple> TupleDictionary { get; set; }
|
|
/// <summary>
|
/// 图形参数字典
|
/// </summary>
|
public Dictionary<string, HObject> ImageDictionary { get; set; }
|
|
#endregion
|
|
#region 初始化
|
/// <summary>
|
/// 实例化 默认搜索路径为: 启动路径//vision//
|
/// </summary>
|
public HDevEngineTool()
|
{
|
////if (MyEngine == null)
|
//{
|
// //ProcedureName = "";
|
// MyEngine = new HDevEngine();
|
// //MyEngine.SetEngineAttribute("execute_procedures_jit_compiled", "true");
|
|
// MyEngine.SetProcedurePath(ProcedurePath);
|
//}
|
|
//ImageDictionary = new Dictionary<string, HObject>();
|
//TupleDictionary = new Dictionary<string, HTuple>();
|
InitialEngine(ProcedurePath);
|
}
|
|
/// <summary>
|
/// 实例化
|
/// </summary>
|
/// <param name="path">外部函数搜索路径</param>
|
public HDevEngineTool(string path)
|
{
|
ProcedurePath = path;
|
//if (MyEngine == null)
|
InitialEngine(path);
|
}
|
|
private void InitialEngine(string path)
|
{
|
//MyEngine?.Dispose();
|
//MyEngine = null;
|
|
//MyEngine = new HalconDotNet.HDevEngine();
|
////MyEngine.SetEngineAttribute("execute_procedures_jit_compiled", "true");
|
|
//MyEngine.SetProcedurePath(ProcedurePath);
|
|
//if (MyEngine == null)
|
{
|
MyEngine = new HalconDotNet.HDevEngine();
|
MyEngine.SetProcedurePath(ProcedurePath);
|
}
|
|
InitialParaDict();
|
|
ImageDictionary = new Dictionary<string, HObject>();
|
TupleDictionary = new Dictionary<string, HTuple>();
|
}
|
#endregion
|
|
/// <summary>
|
/// 设置函数运行所需参数
|
/// </summary>
|
/// <param name="_tupleDictionary">控制参数</param>
|
/// <param name="_imageDictionary">图形参数</param>
|
public void SetDictionary(Dictionary<string, HTuple> _tupleDictionary, Dictionary<string, HObject> _imageDictionary)
|
{
|
//InitialParaDict();
|
|
this.TupleDictionary = _tupleDictionary;
|
this.ImageDictionary = _imageDictionary;
|
}
|
|
private void InitialParaDict()
|
{
|
try
|
{
|
if (TupleDictionary != null)
|
{
|
List<string> list = new List<string>(TupleDictionary.Keys);
|
foreach (string s in list)
|
{
|
TupleDictionary[s] = null;
|
}
|
this.TupleDictionary.Clear();
|
}
|
|
if (ImageDictionary != null)
|
{
|
List<string> list = new List<string>(ImageDictionary.Keys);
|
foreach (string s in list)
|
{
|
ImageDictionary[s].Dispose();
|
ImageDictionary[s] = null;
|
}
|
this.ImageDictionary.Clear();
|
}
|
}
|
catch (Exception)
|
{
|
}
|
}
|
|
/// <summary>
|
/// 载入过程 .hdvp
|
/// </summary>
|
/// <param name="procedureName">过程名</param>
|
/// <param name="hwin"></param>
|
//public void LoadProcedure(string procedureName, HDisplayControl.HDisplayControl hwin)
|
//{
|
// ProcedureName = procedureName;
|
|
// MyEngine.SetHDevOperators(new HDevOpMultiWindowImpl(hwin.HalconWindow));
|
|
// try
|
// {
|
// HDevProcedure procedure = new HDevProcedure(procedureName);
|
// ProcedureCall = new HDevProcedureCall(procedure);
|
// }
|
// catch (HDevEngineException Ex)
|
// {
|
// Trace.TraceInformation("HDevProgram Load fail, ex:" + Ex.Message);
|
// return;
|
// }
|
//}
|
|
/// <summary>
|
/// 载入过程 .hdvp
|
/// </summary>
|
/// <param name="procedureName">过程名</param>
|
public void LoadProcedure(string procedureName)
|
{
|
ProcedureName = procedureName;
|
try
|
{
|
//if (!ProcedureDict.Keys.Contains(ProcedureName) || ProcedureDict[ProcedureName] == null)
|
//{
|
// ProcedureDict[ProcedureName] = new HDevProcedure(procedureName);
|
//}
|
////ProcedureCall = new HDevProcedureCall(Procedure);
|
//ProcedureCallDict[procedureName] = new HDevProcedureCall(ProcedureDict[ProcedureName]);
|
|
HDevProcedure Procedure = new HDevProcedure(ProcedureName);
|
ProcedureCall = new HDevProcedureCall(Procedure);
|
}
|
catch (HDevEngineException Ex)
|
{
|
Trace.TraceInformation("HDevProgram {0} Run fail ,Error Line : {1}, Line number: {2}, Halcon error number : {3}", Ex.ProcedureName, Ex.LineText, Ex.LineNumber, Ex.HalconError);
|
return;
|
}
|
}
|
|
/// <summary>
|
/// 执行过程
|
/// </summary>
|
public void RunProcedure()
|
{
|
try
|
{
|
//_resumeHandle.WaitOne();
|
|
foreach (KeyValuePair<string, HTuple> pair in TupleDictionary)
|
{
|
if (pair.Key.ToUpper().StartsWith("INPUT"))
|
{
|
ProcedureCall.SetInputCtrlParamTuple(pair.Key, pair.Value);
|
//ProcedureCallDict[ProcedureName].SetInputCtrlParamTuple(pair.Key, pair.Value);
|
}
|
}
|
|
foreach (KeyValuePair<string, HObject> pair in ImageDictionary)
|
{
|
if (pair.Key.ToUpper().StartsWith("INPUT"))
|
{
|
ProcedureCall.SetInputIconicParamObject(pair.Key, pair.Value);
|
//ProcedureCallDict[ProcedureName].SetInputIconicParamObject(pair.Key, pair.Value);
|
}
|
}
|
|
ProcedureCall.Execute();
|
//ProcedureCallDict[ProcedureName].Execute();
|
|
string[] tupleKeys = TupleDictionary.Keys.ToArray<string>();
|
string[] imageKeys = ImageDictionary.Keys.ToArray<string>();
|
|
for (int i = 0; i < TupleDictionary.Count; i++)
|
{
|
if (tupleKeys[i].ToUpper().StartsWith("OUTPUT"))
|
{
|
TupleDictionary[tupleKeys[i]] = ProcedureCall.GetOutputCtrlParamTuple(tupleKeys[i]);
|
//TupleDictionary[tupleKeys[i]] = ProcedureCallDict[ProcedureName].GetOutputCtrlParamTuple(tupleKeys[i]);
|
}
|
}
|
|
for (int i = 0; i < ImageDictionary.Count; i++)
|
{
|
if (imageKeys[i].ToUpper().StartsWith("OUTPUTOBJ"))
|
{
|
ImageDictionary[imageKeys[i]] = ProcedureCall.GetOutputIconicParamObject(imageKeys[i]);
|
//ImageDictionary[imageKeys[i]] = ProcedureCallDict[ProcedureName].GetOutputIconicParamObject(imageKeys[i]);
|
}
|
if (imageKeys[i].ToUpper().StartsWith("OUTPUTIMG"))
|
{
|
ImageDictionary[imageKeys[i]] = ProcedureCall.GetOutputIconicParamImage(imageKeys[i]);
|
//ImageDictionary[imageKeys[i]] = ProcedureCallDict[ProcedureName].GetOutputIconicParamImage(imageKeys[i]);
|
}
|
if (imageKeys[i].ToUpper().StartsWith("OUTPUTREG"))
|
{
|
ImageDictionary[imageKeys[i]] = ProcedureCall.GetOutputIconicParamRegion(imageKeys[i]);
|
//ImageDictionary[imageKeys[i]] = ProcedureCallDict[ProcedureName].GetOutputIconicParamRegion(imageKeys[i]);
|
}
|
}
|
|
//ProcedureCall?.Dispose();
|
//InitialParaDict();
|
|
GC.Collect();
|
GC.WaitForPendingFinalizers();
|
|
//ProcedureCall = new HDevProcedureCall(Procedure);
|
|
//Task.Run(() =>
|
//{
|
// //ProcedureDict[ProcedureName].Dispose();
|
// //ProcedureDict[ProcedureName] = null;
|
|
// //ProcedureCallDict[ProcedureName].Dispose();
|
// //ProcedureCallDict[ProcedureName] = null;
|
|
// //LoadProcedure(ProcedureName);
|
|
// //Resume();
|
|
// _resumeHandle.Reset();
|
// LoadProcedure(ProcedureName);
|
// _resumeHandle.Set();
|
//}
|
//);
|
}
|
catch (HDevEngineException ex)
|
{
|
//Trace.TraceInformation("HDevProgram {0} Run fail ,Error Line : {1}, Line number: {2}, Halcon error number : {3}", Ex.ProcedureName, Ex.LineText, Ex.LineNumber, Ex.HalconError);
|
throw ex;
|
//throw new HalconRunException("", ex);
|
}
|
}
|
|
ManualResetEvent _resumeHandle = new ManualResetEvent(true);
|
public void Resume()
|
{
|
_resumeHandle.Reset();
|
//InitialEngine(ProcedurePath);
|
LoadProcedure(ProcedureName);
|
_resumeHandle.Set();
|
}
|
//public void DisposeObjs()
|
//{
|
// MyEngine?.Dispose();
|
// MyEngine = null;
|
|
// InitialParaDict();
|
// TupleDictionary = null;
|
// ImageDictionary = null;
|
|
// ProcedureCall?.Dispose();
|
// ProcedureCall = null;
|
|
// Procedure?.Dispose();
|
// Procedure = null;
|
|
// GC.Collect();
|
// GC.WaitForPendingFinalizers();
|
//}
|
}
|
|
public static class HalconHelper
|
{
|
public static HObject ConvertBitmapToHObject(this Bitmap bmp)
|
{
|
//HObject ho_Image;
|
//HOperatorSet.GenEmptyObj(out ho_Image);
|
//unsafe
|
//{
|
// System.Drawing.Imaging.BitmapData bmpData = map.LockBits(new System.Drawing.Rectangle(0, 0, map.Width, map.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
|
// unsafe
|
// {
|
// HOperatorSet.GenImageInterleaved(out ho_Image, bmpData.Scan0, "bgrx", map.Width, map.Height, -1, "byte", map.Width, map.Height, 0, 0, -1, 0);
|
// }
|
|
// map.UnlockBits(bmpData);
|
//}
|
|
//GC.Collect();
|
|
//return ho_Image;
|
|
HObject himage = new HObject();
|
try
|
{
|
//判断输入图像不为null
|
if (bmp == null)
|
{
|
//MessageDispatcher.Instance.NewMessage("Vision:(BitmapToHImage) error," + "bitmap shouldn't be null!");
|
return null;
|
}
|
|
//lock (ObjeceToLock)
|
{
|
//重绘himage
|
HImage curImage = new HImage();
|
BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, PixelFormat.Format8bppIndexed);
|
curImage.GenImage1("byte", bmp.Width, bmp.Height, bmpData.Scan0);
|
bmp.UnlockBits(bmpData);
|
// HOperatorSet.MedianImage(curImage, out curImage, "circle", 4, "mirrored");
|
//curImage= curImage.MedianImage("circle", 4, "mirrored");
|
himage = curImage;
|
}
|
return himage;
|
}
|
catch (Exception e)
|
{
|
//MessageDispatcher.Instance.NewMessage("Vision:(BitmapToHImage) error," + e.Message);
|
return null;
|
}
|
}
|
|
public static List<double> HTupleToDouble(this HTuple tuple)
|
{
|
List<double> list = new List<double>();
|
|
for (int i = 0; i < tuple.Length; i++)
|
{
|
list.Add(tuple[i]);
|
}
|
|
return list;
|
}
|
}
|
}
|