领胜LDS 键盘AOI检测项目
xcd
2020-11-20 fccc60d8897642ef5c39176b8c872b63fe819ae2
src/Bro.Device.Gocator/GocatorDriver.cs
@@ -1,11 +1,13 @@
using Bro.Common.Base;
using Bro.Common.Helper;
using Bro.Common.Interface;
using Bro.Common.Model;
using HalconDotNet;
using Lmi3d.GoSdk;
using Lmi3d.GoSdk.Messages;
using Lmi3d.Zen;
using Lmi3d.Zen.Io;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Drawing;
@@ -64,6 +66,8 @@
            }
            HandleGoData(dataSet);
            dataSet.Dispose();
        }
        private void HandleGoData(GoDataSet dataSet, GoImageSet imgSet = null)
@@ -86,19 +90,123 @@
                            long bufferSize = width * height;
                            IntPtr bufferPointer = surfaceMsg.Data;
                            float zoomFactor = (float)((double)surfaceMsg.YResolution / (double)surfaceMsg.XResolution);
                            int zoomHeight = (int)(zoomFactor * height);
                            IntPtr zoomPtr = Marshal.AllocHGlobal(zoomHeight * (int)width * 2);
                            //IntPtr zoomPtr = Marshal.AllocHGlobal(zoomHeight * (int)width * 4);
                            //没有插值,默认0
                            //Parallel.For(0, height, h =>
                            //  {
                            //      CopyMemory((IntPtr)((long)zoomPtr + width * 2 * Math.Floor(h * zoomFactor)), (IntPtr)((long)bufferPointer + width * 2 * h), width * 2);
                            //  });
                            //使用上一行原有数据
                            Parallel.For(0, zoomHeight, h =>
                            {
                                int originHeightIndex = (int)Math.Floor((double)height * h / (double)zoomHeight);
                                CopyMemory((IntPtr)((long)zoomPtr + width * 2 * h), (IntPtr)((long)bufferPointer + width * 2 * originHeightIndex), width * 2);
                            });
                            ////使用上一行原有数据
                            //Parallel.For(0, zoomHeight, h =>
                            //{
                            //    int originHeightIndex = (int)Math.Floor((double)height * h / (double)zoomHeight);
                            //    byte[] rowBuffer = new byte[width * 4];
                            //    Parallel.For(0, width, w =>
                            //    {
                            //        rowBuffer[w * 4 + 2] = Marshal.ReadByte(bufferPointer, (int)(width * 2 * originHeightIndex + w * 2));
                            //        rowBuffer[w * 4 + 3] = Marshal.ReadByte(bufferPointer, (int)(width * 2 * originHeightIndex + w * 2 + 1));
                            //    });
                            //    Marshal.Copy(rowBuffer, 0, (IntPtr)((long)zoomPtr + width * 2 * h), rowBuffer.Length);
                            //});
                            if (imgSet != null)
                            {
                                imgSet.HImage = new HImage();
                                imgSet.HImage.GenImage1("uint2", (int)width, (int)height, bufferPointer);
                                imgSet.HImage.GenImage1("uint2", (int)width, zoomHeight, zoomPtr);
                                //imgSet.HImage.GenImage1("int4", (int)width, zoomHeight, zoomPtr);
                                imgSet.HImage_2 = new HImage();
                                imgSet.HImage_2.GenImage1("uint2", (int)width, (int)height, bufferPointer);
                                imgSet.HImage_2.GenImage1("uint2", (int)width, zoomHeight, zoomPtr);
                                //imgSet.HImage_2.GenImage1("int4", (int)width, zoomHeight, zoomPtr);
                                LaserScanParam para = new LaserScanParam()
                                {
                                    Resolution_X = surfaceMsg.XResolution,
                                    Resolution_Y = surfaceMsg.YResolution,
                                    Resolution_Z = surfaceMsg.ZResolution,
                                    Offset_X = surfaceMsg.XOffset,
                                    Offset_Y = surfaceMsg.YOffset,
                                    Offset_Z = surfaceMsg.ZOffset,
                                    Angle_Z = surfaceMsg.ZAngle,
                                };
                                imgSet.ImageData = JsonConvert.SerializeObject(para);
                                //imgSet.HImage_2 = imgSet.HImage_2.ZoomImageSize((int)width, zoomHeight, "constant");
                            }
                            Generate16GrayImageByPointer((int)width, (int)height, bufferPointer, imgSet?.Id);
                            Generate16GrayImageByPointer((int)width, zoomHeight, zoomPtr, imgSet?.Id).ContinueWith(t =>
                            {
                                Marshal.FreeHGlobal(zoomPtr);
                            });
                            //int zoomHeight = (int)(((double)surfaceMsg.YResolution / (double)surfaceMsg.XResolution) * (double)surfaceMsg.Length);
                            //if (imgSet != null)
                            //{
                            //    imgSet.HImage = new HImage();
                            //    imgSet.HImage.GenImage1("uint2", (int)width, (int)height, bufferPointer);
                            //    imgSet.HImage = imgSet.HImage.ZoomImageSize((int)width, zoomHeight, "constant");
                            //    imgSet.HImage_2 = new HImage();
                            //    imgSet.HImage_2.GenImage1("uint2", (int)width, (int)height, bufferPointer);
                            //    imgSet.HImage_2 = imgSet.HImage_2.ZoomImageSize((int)width, zoomHeight, "constant");
                            //}
                            //Generate16GrayImageByPointer((int)width, (int)height, bufferPointer, imgSet?.Id);
                        }
                        break;
                        //case GoDataMessageType.SurfaceIntensity:
                        //    {
                        //        GoSurfaceIntensityMsg surfaceMsg = (GoSurfaceIntensityMsg)dataObj;
                        //        long width = surfaceMsg.Width;
                        //        long height = surfaceMsg.Length;
                        //        long bufferSize = width * height;
                        //        IntPtr bufferPointer = surfaceMsg.Data;
                        //        //int zoomHeight = (surfaceMsg.YResolution * (int)surfaceMsg.Length * 10) / surfaceMsg.XResolution;
                        //        int zoomHeight = (int)(((double)surfaceMsg.YResolution / (double)surfaceMsg.XResolution) * (double)surfaceMsg.Length);
                        //        if (imgSet != null)
                        //        {
                        //            imgSet.HImage = new HImage();
                        //            imgSet.HImage.GenImage1("uint2", (int)width, (int)height, bufferPointer);
                        //            imgSet.HImage = imgSet.HImage.ZoomImageSize((int)width, zoomHeight, "constant");
                        //            imgSet.HImage_2 = new HImage();
                        //            imgSet.HImage_2.GenImage1("uint2", (int)width, (int)height, bufferPointer);
                        //            imgSet.HImage_2 = imgSet.HImage_2.ZoomImageSize((int)width, zoomHeight, "constant");
                        //        }
                        //        Generate16GrayImageByPointer((int)width, (int)height, bufferPointer, imgSet?.Id);
                        //    }
                        //    break;
                }
                dataObj.Dispose();
            }
        }
@@ -156,6 +264,9 @@
            }
            HandleGoData(dataSet, imgSet);
            dataSet.Dispose();
            if (imgSet.HImage == null)
            {
                LogAsync(DateTime.Now, $"{Name}未能获取HImage图像", "");
@@ -330,17 +441,19 @@
                    _snapHandle.Set();
                }
            }
            dataSet.Dispose();
        }
        #region 重写图片保存操作
        public override async void SaveOriginImage(Bitmap map, Bitmap showImage, string imgSetId)
        {
            Task.Run(() =>
            {
                OnImageUpdated?.Invoke(this, showImage, imgSetId);
            });
             {
                 OnImageUpdated?.Invoke(this, showImage, imgSetId);
             });
            GoImageSet set = _imageSetList.FirstOrDefault(u => u.Id == imgSetId) as GoImageSet;
            GoImageSet set = _imageSetList[imgSetId] as GoImageSet;
            if (set == null)
                return;
@@ -381,7 +494,7 @@
        {
            await Task.Run(() =>
            {
                GoImageSet set = _imageSetList.FirstOrDefault(u => u.Id == imgSetId) as GoImageSet;
                GoImageSet set = _imageSetList[imgSetId] as GoImageSet;
                if (set == null)
                    return;
@@ -411,19 +524,22 @@
                }
            });
        }
        public override void ClearImageSet(IImageSet set)
        {
            if (set.IsOriginSaved && set.IsFitSaved && set.IsAddtionalSaved)
            {
                (set as GoImageSet).HImage_2?.Dispose();
                (set as GoImageSet).HImage_2 = null;
            }
            base.ClearImageSet(set);
        }
        #endregion
    }
    public class GoImageSet : ImageSet
    {
        public HImage HImage_2 { get; set; }
        public override void Dispose()
        {
            base.Dispose();
            HImage_2?.Dispose();
            HImage_2 = null;
        }
    }
}