using OTSModelSharp.ServiceCenter; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using OTSMeasureApp.ServiceCenter; using OTSCLRINTERFACE; using OxfordExtenderWrapper; using System.Drawing; using static OxfordExtenderWrapper.ExtenderIpcUI; using OpenCvSharp; using System.Runtime.InteropServices.WindowsRuntime; using System.Threading; using OpenCvSharp.Extensions; using System.Drawing.Imaging; using System.IO; namespace OTSMeasureApp.ServiceCenter { public class PicoSartScanController : IScanController { public PicoSmartApi_cshape m_api; private int imageWidth = 640; private int imageHeight = 480; double dwelltime = 10; int port = 7321; string ip = "127.0.0.1"; /// /// //se 0;bse 1;se_bse 2; /// int scan_chanel = 1; /// /// //red 0; fast 1; slow 2; slow2 3 /// int scan_speed = 1; int get_image_type() { if (imageWidth == 640) { return 0; } else if (imageWidth == 1280) { return 1; } else if (imageWidth == 2560) { return 2; } return 0; } int get_scan_time() { if (imageWidth == 640) { return 17000; } else if (imageWidth == 1280) { return 20000; } else if (imageWidth == 2560) { return 30000; } return 20000; } ImageInputSourceType imagesourceType = new ImageInputSourceType(); public void set_scan_chanel(int scan_chanel = 1) { if (m_api.isStart() == 1) { m_api.SET_SCANMODE((uint)(scan_chanel)); } } public void set_scan_speed(int scan_speed = 1) { if (m_api.isStart() == 1) { m_api.SET_SCANSPEED((uint)(scan_speed)); } } CBSEImgClr IScanController.AcquireBSEImage() { try { Rectangle r = new Rectangle(); CBSEImgClr bse = new CBSEImgClr(r); bse.InitImageData(imageWidth, imageHeight); ImageAquistionParam p = new ImageAquistionParam(); p.width = imageWidth; p.height = imageHeight; p.DwellTime = dwelltime; p.sourceType = imagesourceType; //byte[] ImageData = new byte[imageWidth * imageHeight * 3]; byte[] ImageData; NLog.LogManager.GetCurrentClassLogger().Info("Begin to acquire BSE image!"); //get image Mat image = new Mat(); int imagtype = get_image_type(); m_api.GET_IMAGE((uint)scan_chanel, imagtype); Thread.Sleep(50); int waittime = get_scan_time(); if (m_api.GET_IMAGE_MSGDATA(ref image, waittime) == 1) { if (!image.Empty()) { Bitmap bitmap = image.ToBitmap(); using (MemoryStream stream = new MemoryStream()) { bitmap.Save(stream, ImageFormat.Png); // 你可以选择其它的格式,比如Jpeg ImageData = stream.ToArray(); bse.SetImageData(ImageData, imageWidth, imageHeight); NLog.LogManager.GetCurrentClassLogger().Info("End acquiring BSE image!"); } } } else { NLog.LogManager.GetCurrentClassLogger().Info("acquiring BSE image timeout!"); } //bse.SetImageData(p.ImageData, imageWidth, imageHeight); return bse; } catch (Exception e) { NLog.LogManager.GetCurrentClassLogger().Warn(e.Message); } return null; } bool IScanController.Init() { m_api = new PicoSmartApi_cshape(); m_api.set_ip(ip); m_api.set_port(port); if (m_api.start() == 1) { return true; } return false; } bool IScanController.SetDwellTime(double val) { scan_chanel = (int)val; return true; } bool IScanController.SetImageSize(int nWidth, int nHeight) { this.imageWidth = nWidth; this.imageWidth = nHeight; return true; } } }