领胜LDS 键盘AOI检测项目
xcd
2020-06-30 aaae1139f2bb3a55910fff0aa907b3ba6395deea
src/Bro.Common.Device/DeviceBase/CameraBase.cs
@@ -13,6 +13,7 @@
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using static Bro.Common.Helper.EnumHelper;
@@ -483,6 +484,110 @@
        private System.Threading.Timer clearImageTimer = null;
        #endregion
        #region 图片转换
        [DllImport("kernel32.dll", EntryPoint = "CopyMemory", SetLastError = false)]
        public static extern void CopyMemory(IntPtr dest, IntPtr src, long count);
        protected async void Generate8GrayImageByPointer(int width, int height, IntPtr dataPtr, string imgSetId)
        {
            await Task.Run(() =>
            {
                //************************Mono8 转 Bitmap*******************************
                Bitmap bmp = new Bitmap(width, height, PixelFormat.Format8bppIndexed);
                Bitmap showImage = new Bitmap(width, height, PixelFormat.Format8bppIndexed);
                ColorPalette cp = bmp.Palette;
                for (int i = 0; i < 256; i++)
                {
                    cp.Entries[i] = Color.FromArgb(i, i, i);
                }
                bmp.Palette = cp;
                ColorPalette cp1 = showImage.Palette;
                for (int i = 0; i < 256; i++)
                {
                    cp1.Entries[i] = Color.FromArgb(i, i, i);
                }
                showImage.Palette = cp1;
                long[] ptr = new long[3];
                Rectangle rect = new Rectangle(0, 0, width, height);
                BitmapData bitmapData = bmp.LockBits(rect, ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed);
                BitmapData showImageData = showImage.LockBits(rect, ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed);
                int PixelSize = Bitmap.GetPixelFormatSize(bitmapData.PixelFormat) / 8;
                if (width % 4 == 0)
                {
                    CopyMemory(bitmapData.Scan0, dataPtr, width * height * PixelSize);
                    CopyMemory(showImageData.Scan0, dataPtr, width * height * PixelSize);
                }
                else
                {
                    ptr[0] = bitmapData.Scan0.ToInt64();
                    ptr[1] = showImageData.Scan0.ToInt64();
                    ptr[2] = (long)dataPtr;
                    for (int i = 0; i < height - 1; i++)
                    {
                        ptr[2] += width;
                        CopyMemory((IntPtr)ptr[0], (IntPtr)ptr[2], width * PixelSize);
                        CopyMemory((IntPtr)ptr[1], (IntPtr)ptr[2], width * PixelSize);
                        ptr[0] += bitmapData.Stride;
                        ptr[1] += showImageData.Stride;
                    }
                }
                bmp.UnlockBits(bitmapData);
                showImage.UnlockBits(showImageData);
                SaveOriginImage(bmp, showImage, imgSetId);
            });
        }
        protected async void Generate16GrayImageByPointer(int width, int height, IntPtr dataPtr, string imgSetId)
        {
            await Task.Run(() =>
            {
                int widthIn4 = (int)Math.Ceiling(width / 4.0) * 4;
                Bitmap bmp = new Bitmap(widthIn4, height, PixelFormat.Format48bppRgb);
                Bitmap showImage = new Bitmap(widthIn4, height, PixelFormat.Format48bppRgb);
                Rectangle rect = new Rectangle(0, 0, widthIn4, height);
                BitmapData bitmapData = bmp.LockBits(rect, ImageLockMode.WriteOnly, PixelFormat.Format48bppRgb);
                BitmapData showImageData = showImage.LockBits(rect, ImageLockMode.WriteOnly, PixelFormat.Format48bppRgb);
                unsafe
                {
                    byte* data = (byte*)dataPtr;
                    byte* bitmapBuffer = (byte*)bitmapData.Scan0;
                    byte* showBitmapBuffer = (byte*)showImageData.Scan0;
                    //Parallel.For(0, height, i =>
                    //      {
                    //          Parallel.For(0, width, j =>
                    //            {
                    //                showBitmapBuffer[(i * widthIn4 + j) * 6] = bitmapBuffer[(i * widthIn4 + j) * 6] = data[(i * width + j) * 2];
                    //                showBitmapBuffer[(i * widthIn4 + j) * 6 + 1] = bitmapBuffer[(i * widthIn4 + j) * 6 + 1] = data[(i * width + j) * 2 + 1];
                    //            });
                    //      });
                    Parallel.For(0, width * height, i =>
                      {
                          int index = (i + 1) % width + widthIn4 * ((i + 1) / width) - 1;
                          showBitmapBuffer[index * 6] = data[i * 2];
                          showBitmapBuffer[index * 6 + 1] = data[i * 2 + 1];
                      });
                }
                bmp.UnlockBits(bitmapData);
                showImage.UnlockBits(showImageData);
                //showImage.Save(@"D:\1.bmp", ImageFormat.Bmp);
                SaveOriginImage(bmp, showImage, imgSetId);
            });
        }
        #endregion
    }
    #region Config
@@ -496,7 +601,7 @@
        [Category("取像配置")]
        [Description("曝光")]
        [DisplayName("曝光")]
        public float Exposure
        public virtual float Exposure
        {
            get => exposure;
            set
@@ -515,17 +620,17 @@
        [Category("取像配置")]
        [Description("增益")]
        [DisplayName("增益")]
        public float Gain { get; set; }
        public virtual float Gain { get; set; }
        [Category("取像配置")]
        [Description("曝光修改后等待时间,单位ms。针对部分场景确保曝光修改及时生效。")]
        [DisplayName("曝光等待")]
        public int ExposureWaitTime { get; set; }
        public virtual int ExposureWaitTime { get; set; }
        [Category("取像配置")]
        [Description("拍摄后曝光值。可在一次拍摄后将曝光修改为下次拍摄的曝光值,节约曝光生效时间。")]
        [DisplayName("拍摄后曝光")]
        public float ExposureAfterSnap { get; set; }
        public virtual float ExposureAfterSnap { get; set; }
        [Category("算法配置")]
        [Description("算法路径")]
@@ -581,35 +686,35 @@
        [Category("相机设置")]
        [Description("相机IP地址")]
        [DisplayName("相机IP地址")]
        public string CameraIP { get; set; }
        public virtual string CameraIP { get; set; }
        [Category("相机设置")]
        [Description("上位机IP地址")]
        [DisplayName("上位机IP地址")]
        public string ComputerIP { get; set; }
        public virtual string ComputerIP { get; set; }
        [Category("保存设置")]
        [Description("图片保存目录")]
        [DisplayName("图片保存目录")]
        [Editor(typeof(FoldDialogEditor), typeof(UITypeEditor))]
        public string ImgDirectory { get; set; } = @"../Images";
        public virtual string ImgDirectory { get; set; } = @"../Images";
        [Category("保存设置")]
        [Description("图片保存天数")]
        [DisplayName("图片保存天数")]
        public int SaveImageDayLimit { get; set; } = 0;
        public virtual int SaveImageDayLimit { get; set; } = 0;
        [Category("保存设置")]
        [Description("图片保存默认配置,主要用于硬触发等没有明确操作配置说明的图片保存")]
        [DisplayName("图片保存默认配置")]
        [TypeConverter(typeof(ComplexObjectConvert))]
        [Editor(typeof(PropertyObjectEditor), typeof(UITypeEditor))]
        public ImageSaveOption ImageSaveOption { get; set; } = new ImageSaveOption();
        public virtual ImageSaveOption ImageSaveOption { get; set; } = new ImageSaveOption();
        [Category("拍摄设置")]
        [Description("默认曝光值,相机开启后就设置该曝光值")]
        [DisplayName("默认曝光值")]
        public float DefaultExposure { get; set; }
        public virtual float DefaultExposure { get; set; }
    }
    #endregion