/// <summary> /// 创建VisionOperation新实例 /// </summary> public VisionOperation() { RunningWindow = new HSmartWindowControlWPF(); ConfigWindow = new ConfigWindow(); Inputs = new ItemCollection { new ItemBase("Custom", false, "自定义参数") }; Outputs = new ItemCollection { new ItemBase("ImageWidth", typeof(int), "图像宽度"), new ItemBase("ImageHeight", typeof(int), "图像高度"), new ItemBase("ImageType", typeof(string), "图像类型"), new ItemBase("Custom", typeof(bool), "自定义参数"), new ItemBase("Random", typeof(int), "随机数,范围为0-100") }; }
/// <summary> /// 初始化 /// </summary> /// <param name="windowHandle">窗口句柄</param> public void Execute(object image, out ItemCollection outputs) { outputs = null; stopwatch.Restart(); HObject thresholdImage = null; try { //初始化 if ((!isRunningWindowInit) && (HRunningWindowHande != IntPtr.Zero)) { HOperatorSet.SetDraw(HRunningWindowHande, "margin"); HOperatorSet.SetLineWidth(HRunningWindowHande, 2); HOperatorSet.SetColor(HRunningWindowHande, "red"); isRunningWindowInit = true; } //初始化 if ((!isConfigWindowInit) && (HConfigWindowHande != IntPtr.Zero)) { HOperatorSet.SetDraw(HConfigWindowHande, "margin"); HOperatorSet.SetLineWidth(HConfigWindowHande, 2); HOperatorSet.SetColor(HConfigWindowHande, "red"); isConfigWindowInit = true; } var random = new Random(); HTuple width; HTuple height; HTuple type; hImage?.Dispose(); hImage = image as HObject; HOperatorSet.GetImageSize(hImage, out width, out height); HOperatorSet.GetImageType(hImage, out type); HOperatorSet.Threshold(hImage, out thresholdImage, 0, 150); if (HRunningWindowHande != IntPtr.Zero) { SetWindowPart((RunningWindow as HSmartWindowControlWPF)?.HalconWindow, width, height); HOperatorSet.ClearWindow(HRunningWindowHande); HOperatorSet.DispObj(hImage, HRunningWindowHande); } if (HConfigWindowHande != IntPtr.Zero) { SetWindowPart((ConfigWindow as ConfigWindow)?.HSmartWindowControlWPF.HalconWindow, width, height); HOperatorSet.ClearWindow(HConfigWindowHande); HOperatorSet.DispObj(hImage, HConfigWindowHande); HOperatorSet.DispObj(thresholdImage, HConfigWindowHande); } Outputs["ImageWidth"].Value = width.I; Outputs["ImageHeight"].Value = height.I; Outputs["ImageType"].Value = type.S; Outputs["Custom"].Value = Inputs["Custom"].Value; Outputs["Random"].Value = random.Next(0, 100); outputs = new ItemCollection(Outputs); stopwatch.Stop(); RunStatus = new RunStatus(stopwatch.Elapsed.TotalMilliseconds); } catch (Exception ex) { stopwatch.Stop(); RunStatus = new RunStatus(stopwatch.Elapsed.TotalMilliseconds, EResult.Error, ex.Message, ex); throw; } finally { thresholdImage?.Dispose(); } }