| | |
| | | using Newtonsoft.Json; |
| | | using HalconDotNet; |
| | | using Newtonsoft.Json; |
| | | using Newtonsoft.Json.Serialization; |
| | | using OpenCvSharp; |
| | | using OpenCvSharp.Extensions; |
| | | using System; |
| | | using System.Collections.Concurrent; |
| | | using System.Diagnostics; |
| | | using System.Text; |
| | |
| | | |
| | | Params.Inputs.Add("超时时间", 1000); |
| | | Params.Inputs.Add("是否失败重新取图", false); |
| | | |
| | | // 2D轮胎计数配置(默认为1,表示3D模式;2D相机设置为n) |
| | | Params.Inputs.Add("ImagesPerTyre", 1); |
| | | Params.Inputs.Add("当前轮胎ID", 1); |
| | | Params.Inputs.Add("当前图像序号", 0); |
| | | } |
| | | |
| | | /// <summary> |
| | |
| | | |
| | | Mat src = Cv2.ImRead(SN); |
| | | if (src != null && !src.Empty()) |
| | | OutputImage = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(src); |
| | | OutputImage = src.ToBitmap(); |
| | | } |
| | | } |
| | | } |
| | |
| | | return Result; |
| | | } |
| | | |
| | | if (Camera == null || Camera.SN != SN) |
| | | // 始终检查缓存的Camera对象是否与全局字典中的一致 |
| | | // 防止相机被删除并重建后(SN相同), 仍然引用旧的已销毁对象 |
| | | if (dicCameras.ContainsKey(SN)) |
| | | { |
| | | if (dicCameras.ContainsKey(SN)) |
| | | Camera = dicCameras[SN]; |
| | | else |
| | | Camera = null; |
| | | var currentCam = dicCameras[SN]; |
| | | if (Camera != currentCam) |
| | | { |
| | | Camera = currentCam; |
| | | // 如果切换了相机实例,且未开始采集,尝试开启采集 |
| | | if (Camera != null && !Camera.isGrabbing) |
| | | { |
| | | try { Camera.StartGrabbing(); } catch { } |
| | | } |
| | | } |
| | | } |
| | | else |
| | | { |
| | | Camera = null; |
| | | } |
| | | |
| | | if (Camera == null) |
| | |
| | | { |
| | | times--; |
| | | if (TriggerSource == TriggerSource.Software) |
| | | { |
| | | Camera.GetImageWithSoftTrigger(out bitmap, timeout); |
| | | } |
| | | else |
| | | { |
| | | Camera.GetImage(out bitmap, timeout); |
| | | |
| | | } |
| | | if (bitmap != null) |
| | | { |
| | | break; |
| | | } |
| | | else if (isRegrab) |
| | | { |
| | | Debug.WriteLine($"取图失败,重新取图,剩余次数:{times}"); |
| | |
| | | } while (times > 0 && isRegrab); |
| | | |
| | | if (isUpParams) |
| | | { |
| | | Camera.SetCamConfig(oriConfig); |
| | | |
| | | } |
| | | OutputImage = bitmap; |
| | | } |
| | | |
| | |
| | | { |
| | | Result = true; |
| | | Msg = ""; |
| | | OutputImage = null; |
| | | if (OutputImage != null) |
| | | { |
| | | if (OutputImage is HObject) |
| | | ((HObject)OutputImage).Dispose(); |
| | | else if (OutputImage is Mat) |
| | | ((Mat)OutputImage).Dispose(); |
| | | else if (OutputImage is Bitmap) |
| | | ((Bitmap)OutputImage).Dispose(); |
| | | |
| | | OutputImage = null; |
| | | } |
| | | if (Record != null) |
| | | { |
| | | Record.Dispose(); |
| | | Record = null; |
| | | } |
| | | } |
| | | |
| | | public override void Dispose() |
| | | { |
| | | if (InputImage != null) |
| | | { |
| | | if (InputImage is HObject) |
| | | ((HObject)InputImage).Dispose(); |
| | | else if (InputImage is Mat) |
| | | ((Mat)InputImage).Dispose(); |
| | | else if (InputImage is Bitmap) |
| | | ((Bitmap)InputImage).Dispose(); |
| | | |
| | | InputImage = null; |
| | | } |
| | | |
| | | if (OutputImage != null) |
| | | { |
| | | if (OutputImage is HObject) |
| | | ((HObject)OutputImage).Dispose(); |
| | | else if (OutputImage is Mat) |
| | | ((Mat)OutputImage).Dispose(); |
| | | else if (OutputImage is Bitmap) |
| | | ((Bitmap)OutputImage).Dispose(); |
| | | |
| | | OutputImage = null; |
| | | } |
| | | return; |
| | | } |
| | | |
| | |
| | | if (OutputImage != null) |
| | | { |
| | | if (OutputImage is Bitmap) |
| | | { |
| | | obj.OutputImage = ((Bitmap)OutputImage).Clone(); |
| | | } |
| | | } |
| | | |
| | | return obj; |