using System; using System.Collections.Generic; using System.Linq; using System.Text; using HalconDotNet; using System.Drawing; using System.Drawing.Imaging; using FlyCapture2Managed; using System.Threading; using System.Net; using System.Net.NetworkInformation; using System.Configuration; namespace M423project { /// ///图象处理类 /// public class ImageProcess { private object _barcodeLockObj = new object(); private object _sizeLockObj = new object(); private string GocatorIPAddress = "192.168.1.10"; private string UploadCameraSerialNum = "16511960"; private string BarcodeCameraSerialNum = "706359"; private string SizeCameraSerialNum = "1399882"; private HTuple acqHandleMessageZ; private FlyCaptureCamera flyCameraProductNo; private FlyCaptureCamera flyCameraXY; private Queue barcodeImageList = new Queue(); private Queue sizeImageList = new Queue(); // private double BarcodeCameraExposure = 12; private double BarcodeCameraGain = 2.5; private double SizeCameraExposure = 12; private double SizeCameraGain = 2.5; private double SizeCameraExposureStandard; private double SizeCameraGainStandard; private PhysicalAddress ProductNoCameraMACAddress { get; set; } private IPAddress ProductNoCameraIPAddress { get; set; } private IPAddress ProductNoCameraSubnetMask { get; set; } private IPAddress ProductNoCameraDefaultGateway { get; set; } private PhysicalAddress SizeCameraMACAddress { get; set; } private IPAddress SizeCameraIPAddress { get; set; } private IPAddress SizeCameraSubnetMask { get; set; } private IPAddress SizeCameraDefaultGateway { get; set; } public ImageProcess() { GocatorIPAddress = ConfigurationManager.AppSettings["GocatorIPAddress"]; UploadCameraSerialNum = ConfigurationManager.AppSettings["UploadCameraSerialNum"]; BarcodeCameraSerialNum = ConfigurationManager.AppSettings["BarcodeCameraSerialNum"]; SizeCameraSerialNum = ConfigurationManager.AppSettings["SizeCameraSerialNum"]; double d = 0; if (double.TryParse(ConfigurationManager.AppSettings["BarcodeCameraExposure"], out d)) BarcodeCameraExposure = d; if (double.TryParse(ConfigurationManager.AppSettings["BarcodeCameraGain"], out d)) BarcodeCameraGain = d; if (double.TryParse(ConfigurationManager.AppSettings["SizeCameraExposure"], out d)) SizeCameraExposure = d; if (double.TryParse(ConfigurationManager.AppSettings["SizeCameraGain"], out d)) SizeCameraGain = d; if (double.TryParse(ConfigurationManager.AppSettings["SizeCameraExposureStandard"], out d)) SizeCameraExposureStandard = d; if (double.TryParse(ConfigurationManager.AppSettings["SizeCameraGainStandard"], out d)) SizeCameraGainStandard = d; /* ProductNoCameraMACAddress = PhysicalAddress.Parse(ConfigurationManager.AppSettings["ProductNoCameraMACAddress"]); ProductNoCameraIPAddress = IPAddress.Parse(ConfigurationManager.AppSettings["ProductNoCameraIPAddress"]); ProductNoCameraSubnetMask = IPAddress.Parse(ConfigurationManager.AppSettings["ProductNoCameraSubnetMask"]); ProductNoCameraDefaultGateway = IPAddress.Parse(ConfigurationManager.AppSettings["ProductNoCameraDefaultGateway"]); SizeCameraMACAddress = PhysicalAddress.Parse(ConfigurationManager.AppSettings["SizeCameraMACAddress"]); SizeCameraIPAddress = IPAddress.Parse(ConfigurationManager.AppSettings["SizeCameraIPAddress"]); SizeCameraSubnetMask = IPAddress.Parse(ConfigurationManager.AppSettings["SizeCameraSubnetMask"]); SizeCameraDefaultGateway = IPAddress.Parse(ConfigurationManager.AppSettings["SizeCameraDefaultGateway"]); */ } public void EnqueueBarcodeImage(HImage hImage) { lock (_barcodeLockObj) { barcodeImageList.Enqueue(hImage); } } public HImage DequeueBarcodeImage() { lock (_barcodeLockObj) { if (barcodeImageList.Count > 0) return (HImage)barcodeImageList.Dequeue(); return null; } } public int BarcodeImageCount { get { lock (_barcodeLockObj) { return barcodeImageList.Count; } } } public void ClearBarcodeImage() { lock (_barcodeLockObj) { HImage hi; while (barcodeImageList.Count > 0) { hi = (HImage)barcodeImageList.Dequeue(); hi.Dispose(); } barcodeImageList.Clear(); } } public void EnqueueSizeImage(HImage hImage) { lock (_sizeLockObj) { sizeImageList.Enqueue(hImage); } } public HImage DequeueSizeImage() { lock (_sizeLockObj) { if (sizeImageList.Count > 0) return (HImage)sizeImageList.Dequeue(); return null; } } public int SizeImageCount { get { lock (_sizeLockObj) { return sizeImageList.Count; } } } public void ClearSizeImage() { lock (_sizeLockObj) { HImage hi; while (sizeImageList.Count > 0) { hi = (HImage)sizeImageList.Dequeue(); hi.Dispose(); } sizeImageList.Clear(); } } public bool OpenBarcodeCamera() { try { flyCameraProductNo = new FlyCaptureCamera(BarcodeCameraExposure, BarcodeCameraGain); flyCameraProductNo.SerialNum = BarcodeCameraSerialNum; flyCameraProductNo.ImageEvent += BarcodeImageCaptured; flyCameraProductNo.Init(); if (flyCameraProductNo.Open()) return true; } catch (Exception) { } CommonUtil.WriteLog(LogType.Inf, "产品条码相机开启失败"); return false; } public bool OpenSizeCamera(bool doProduct) { try { flyCameraXY = new FlyCaptureCamera(doProduct ? SizeCameraExposure : SizeCameraExposureStandard, doProduct ? SizeCameraGain : SizeCameraGainStandard); flyCameraXY.SerialNum = SizeCameraSerialNum; flyCameraXY.ImageEvent += SizeImageCaptured; flyCameraXY.Init(); if (flyCameraXY.Open()) return true; } catch (Exception) { } CommonUtil.WriteLog(LogType.Inf, "产品尺寸相机开启失败"); return false; } public bool OpenHeightCamera() { if (!TryConnectGocator()) { CommonUtil.WriteLog(LogType.Inf, "产品高度相机开启失败"); return false; } try { HOperatorSet.OpenFramegrabber("GenICamTL", 0, 0, 0, 0, 0, 0, "progressive", -1, "default", -1, "false", "default", GocatorIPAddress, 0, -1, out acqHandleMessageZ); #region 添加Gocator采图超时设置 HOperatorSet.SetFramegrabberParam(acqHandleMessageZ, "grab_timeout", 1000); #endregion //HOperatorSet.OpenFramegrabber("GenICamTL", 0, 0, 0, 0, 0, 0, "progressive", -1, "default", -1, "false", "default", @"192.168.1.10 producer:C:\Gocator4.5.4.120GenTL\x86\Go2GenTL.cti interface:0:XX::GenTL vendor:LMI model:Gocator 2320", 0, -1, out acqHandleMessageZ); // 'GenICamTL', 0, 0, 0, 0, 0, 0, 'progressive', -1, 'default', -1, 'false', 'default', '192.168.1.10 producer:C:\\GenTL\\x86\\Go2GenTL.cti interface:0:XX::GenTL vendor:LMI model:Gocator', 0, -1, AcqHandle) HOperatorSet.GrabImageStart(acqHandleMessageZ, -1); return true; } catch (Exception e) { CommonUtil.WriteLog(LogType.Inf, string.Format("Gocator开启失败: {0}", e.Message)); } CommonUtil.WriteLog(LogType.Inf, "产品高度相机开启失败"); return false; } public void CloseBarcodeCamera() { flyCameraProductNo.Close(); flyCameraProductNo = null; } public void CloseSizeCamera() { flyCameraXY.ImageEvent -= SizeImageCaptured; flyCameraXY.Close(); flyCameraXY = null; GC.Collect(); } public void CloseHeightCamera() { HOperatorSet.CloseAllFramegrabbers(); } //开启相机 public bool OpenCameras() { CommonUtil.WriteLog(LogType.Inf, "开启相机..."); if (!OpenHeightCamera()) return false; if (!OpenBarcodeCamera()) return false; if (!OpenSizeCamera(true)) return false; CommonUtil.WriteLog(LogType.Inf, "所有相机已开启"); return true; } //开启GrayPoint相机 public bool OpenGrayPointCameras() { CommonUtil.WriteLog(LogType.Inf, "开启GrayPoint相机..."); try { if (!OpenBarcodeCamera()) return false; if (!OpenSizeCamera(true)) return false; CommonUtil.WriteLog(LogType.Inf, "所有GrayPoint相机已开启"); return true; } catch (Exception) { return false; } } //关闭相机 public void CloseCameras() { try { CloseSizeCamera(); CloseBarcodeCamera(); CloseHeightCamera(); GC.Collect(); } catch (Exception ee) { CommonUtil.WriteLog(LogType.Err, string.Format("关闭相机出错:{0}", ee.Message)); } } //关闭GrayPoint相机 public void CloseGrayPointCameras() { try { CloseSizeCamera(); CloseBarcodeCamera(); GC.Collect(); } catch (Exception ee) { CommonUtil.WriteLog(LogType.Err, string.Format("关闭GrayPoint相机出错:{0}", ee.Message)); } } public void BarcodeImageCaptured(object image) { ManagedImage mi = (ManagedImage)image; Bitmap bmp = null; BitmapData bmpData = null; try { bmp = (Bitmap)mi.bitmap; HImage curImage = new HImage(); bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb); curImage.GenImageInterleaved(bmpData.Scan0, "bgr", bmp.Width, bmp.Height, -1, "byte", bmp.Width, bmp.Height, 0, 0, -1, 0); lock (_barcodeLockObj) { barcodeImageList.Enqueue(curImage); } } finally { bmp?.UnlockBits(bmpData); bmp?.Dispose(); bmp = null; mi.Dispose(); GC.Collect(); } } public void SizeImageCaptured(object image) { ManagedImage mi = (ManagedImage)image; Bitmap bmp = null; BitmapData bmpData = null; try { bmp = (Bitmap)mi.bitmap; HImage curImage = new HImage(); bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb); curImage.GenImageInterleaved(bmpData.Scan0, "bgr", bmp.Width, bmp.Height, -1, "byte", bmp.Width, bmp.Height, 0, 0, -1, 0); lock (_barcodeLockObj) { sizeImageList.Enqueue(curImage); } } catch (Exception ex) { } finally { bmp?.UnlockBits(bmpData); bmp?.Dispose(); bmp = null; mi.Dispose(); GC.Collect(); } } //抓取产品编码图象 public void GrapProductNoAsync() { flyCameraProductNo.Snapshot(); } //抓取产品尺寸图象 public void GrapSizeAsync() { flyCameraXY.Snapshot(); } //获取图象特定区域 public void GetImageIntrest(HObject OriImage, out HObject IntrestImage) { HTuple RectRow1 = new HTuple(), RectColumn1 = new HTuple(), RectRow2 = new HTuple(), RectColumn2 = new HTuple(); HObject ROI = new HObject(); HOperatorSet.GenEmptyObj(out IntrestImage); RectRow1 = 300; RectColumn1 = 5.5; RectRow2 = 750.5; RectColumn2 = 1500; ROI.Dispose(); HOperatorSet.GenRectangle1(out ROI, RectRow1, RectColumn1, RectRow2, RectColumn2); IntrestImage.Dispose(); HOperatorSet.ReduceDomain(OriImage, ROI, out IntrestImage); } public bool GrapHeightImage(ref HObject hImage) { if (hImage != null) hImage.Dispose(); try { VisionDetect.GrabImageAsync(acqHandleMessageZ, -1, out hImage); if (hImage != null) return true; } catch (Exception e) { CommonUtil.WriteLog(LogType.Exc, string.Format("Gocator 抓取图像出现异常:{0}", e.Message)); //CloseHeightCamera(); //OpenHeightCamera(); return false; } /* Thread.Sleep(2000); if (OpenHeightCamera()) { try { VisionDetect.GrabImageAsync(acqHandleMessageZ, -1, out hImage); if (hImage != null) return true; } catch (Exception e) { CommonUtil.WriteLog(LogType.Exc, string.Format("Gocator 抓取图像出现异常:{0}", e.Message)); } }*/ return false; } public bool TryConnectGocator() { return true; //int times = 1; //bool result = false; //while (times <= 1 && !result) //{ // string uri = @"http://192.168.1.10"; // try // { // HttpWebRequest request = HttpWebRequest.Create(uri) as HttpWebRequest; // request.Method = "GET"; //请求方法 // request.ProtocolVersion = new Version(1, 1); //Http/1.1版本 // request.Timeout = 1000 * 5; // HttpWebResponse response = request.GetResponse() as HttpWebResponse; // CommonUtil.WriteLog(LogType.Err, string.Format("第{0}次尝试连接Gocator成功", times)); // result = true; // } // catch (Exception e) // { // CommonUtil.WriteLog(LogType.Err, string.Format("第{0}次尝试连接Gocator失败:{1}", times, e.Message)); // Thread.Sleep(500); // } // times++; //} //return result; } } }