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 成员变量
///
/// 处理过程名
///
public virtual string ProcedureName { get; set; } = "";
///
/// hdev程序启动引擎
///
private HalconDotNet.HDevEngine MyEngine = null;
///
/// 程序载入工具 .hdev
///
//private HDevProgramCall ProgramCall;
//HDevProcedure Procedure = null;
//private static Dictionary ProcedureDict = new Dictionary();
///
/// 过程载入工具 .hdvp
///
private HDevProcedureCall ProcedureCall = null;
//private static Dictionary ProcedureCallDict = new Dictionary();
///
/// 控制参数字典
///
public Dictionary TupleDictionary { get; set; }
///
/// 图形参数字典
///
public Dictionary ImageDictionary { get; set; }
#endregion
#region 初始化
///
/// 实例化 默认搜索路径为: 启动路径//vision//
///
public HDevEngineTool()
{
////if (MyEngine == null)
//{
// //ProcedureName = "";
// MyEngine = new HDevEngine();
// //MyEngine.SetEngineAttribute("execute_procedures_jit_compiled", "true");
// MyEngine.SetProcedurePath(ProcedurePath);
//}
//ImageDictionary = new Dictionary();
//TupleDictionary = new Dictionary();
InitialEngine(ProcedurePath);
}
///
/// 实例化
///
/// 外部函数搜索路径
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();
TupleDictionary = new Dictionary();
}
#endregion
///
/// 设置函数运行所需参数
///
/// 控制参数
/// 图形参数
public void SetDictionary(Dictionary _tupleDictionary, Dictionary _imageDictionary)
{
//InitialParaDict();
this.TupleDictionary = _tupleDictionary;
this.ImageDictionary = _imageDictionary;
}
private void InitialParaDict()
{
try
{
if (TupleDictionary != null)
{
List list = new List(TupleDictionary.Keys);
foreach (string s in list)
{
TupleDictionary[s] = null;
}
this.TupleDictionary.Clear();
}
if (ImageDictionary != null)
{
List list = new List(ImageDictionary.Keys);
foreach (string s in list)
{
ImageDictionary[s].Dispose();
ImageDictionary[s] = null;
}
this.ImageDictionary.Clear();
}
}
catch (Exception)
{
}
}
///
/// 载入过程 .hdvp
///
/// 过程名
///
//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;
// }
//}
///
/// 载入过程 .hdvp
///
/// 过程名
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;
}
}
///
/// 执行过程
///
public void RunProcedure()
{
try
{
//_resumeHandle.WaitOne();
foreach (KeyValuePair pair in TupleDictionary)
{
if (pair.Key.ToUpper().StartsWith("INPUT"))
{
ProcedureCall.SetInputCtrlParamTuple(pair.Key, pair.Value);
//ProcedureCallDict[ProcedureName].SetInputCtrlParamTuple(pair.Key, pair.Value);
}
}
foreach (KeyValuePair 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[] imageKeys = ImageDictionary.Keys.ToArray();
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 HTupleToDouble(this HTuple tuple)
{
List list = new List();
for (int i = 0; i < tuple.Length; i++)
{
list.Add(tuple[i]);
}
return list;
}
}
}