HIKIDMVS.cs 6.5 KB
using HalconDotNet;
using IDHIKCamera;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Reflection;

namespace CodeLibrary.camera
{
    public class HIKIDMVS : ClsCamera
    {
        static new  string cameraName = null;

        public override bool Load()
        {
            try
            {
                HDLogUtil.error("加载扫码相机:" );
                IDHIK.Instance.Load();
                List<string> names = IDHIK.Instance.cameraName;
                _name = names.ToArray();
                IDHIK.Instance.DrawLine = false;
                HDLogUtil.error($"加载扫码相机:{string.Join(",", names)}");
                if (names != null)
                {
                    // hikNameList.AddRange(names);
                    //foreach (string name in names)
                    //{
                    //    LogNet.log.Info("加载到IDHIK相机:" + name);
                    //}
                    cameraName = names.Find(s => s.Contains("ID"));
                    HDLogUtil.error("加载到扫码相机:" + cameraName);
                }
            }
            catch (Exception ex)
            {
                HDLogUtil.error("解析IDHIK出错:" + ex.StackTrace);
                return false;
            }
            return true;
        }

        public override bool Open(int index)
        {
            return IDHIK.Instance.Open(index);
        }

        public override bool Open(string name)
        {
            return IDHIK.Instance.Open(name);
        }

        public override bool OpenAll()
        {
            return IDHIK.Instance.OpenAll();
        }

        public override void Close(int index)
        {
            IDHIK.Instance.Close(index);
        }

        public override void Close(string name)
        {
            IDHIK.Instance.Close(name);
        }

        public override void CloseAll()
        {
            IDHIK.Instance.CloseAll();
        }

        public override HObject CaptureOnImage(string name, out List<CodeInfo> cc)
        {
            Bitmap bitmap = null;
            cc = GetImageAndIdentifyCode(out bitmap);
            return null;
        }

        public override HObject CaptureOnImage(string name, out Bitmap bmp,out List<CodeInfo> codeInfos, bool nohalcon = false)
        {
            codeInfos= GetImageAndIdentifyCode(out bmp);
            return null;
        }
       
        public override Bitmap GrabOne(int index, PixelType pt = PixelType.MONO8)
        {
            Bitmap bitmap = null;
            _ = GetImageAndIdentifyCode(out bitmap);
            return bitmap;
        }

        public override Bitmap GrabOne(string name, PixelType pt = PixelType.MONO8)
        {
            Bitmap bitmap = null;
            _ = GetImageAndIdentifyCode(out bitmap);
            return bitmap;
        }

        public override Bitmap GrabOneImage(string name, PixelType pt = PixelType.MONO8)
        {
            Bitmap bitmap = null;
            _ = GetImageAndIdentifyCode(out bitmap);
            return bitmap;
        }

        public override bool ProcessOnImage(string name, out object[] result, params Func<string, Bitmap, object>[] func)
        {
            result = null;
            return true;
        }


        static List<BarcodeInfo> grabOne(out Bitmap bitmap)
        {
            bitmap = null;
            List<BarcodeInfo> barcodeInfos = new List<BarcodeInfo>();
            if (string.IsNullOrEmpty(cameraName))
            {
                return barcodeInfos;
            }
            try
            {
                bitmap = (Bitmap)IDHIK.Instance.GrabOne(cameraName, out List<CodeInfo> cc);
                if (cc != null)
                {
                    cc.ForEach(c =>
                    {
                        barcodeInfos.Add(new BarcodeInfo()
                        {
                            Center = new PointF(c.X, c.Y),
                            Angle = (float)c.Orientation,
                            CodeType = c.CodeType,
                            Text = c.CodeStr
                        });

                    });
                }
            }
            catch (Exception e)
            {
                HDLogUtil.error($"{cameraName}取图失败:{e}");
            }
            return barcodeInfos;
        }


        public override List<CodeInfo> GetImageAndIdentifyCode(out Bitmap bitmap)
        {
            bitmap = null;
            List<CodeInfo> codeisc = new List<CodeInfo>();
            HDLogUtil.error("获取图片相机名称:" + cameraName);
            if (string.IsNullOrEmpty(cameraName))
            {
                return null;
            }
            try
            {
                List<CodeInfo> cod = new List<CodeInfo>();
                bitmap = (Bitmap)IDHIK.Instance.GrabOne(cameraName, out cod);
                cod.ForEach(c =>
                {
                    codeisc.Add(new CodeInfo()
                    {
                        CodeStr=c.CodeStr,
                        CodeType=c.CodeType,
                        X = c.X,
                        Y = c.Y,
                        Orientation= c.Orientation,
                    });

                });
            }
            catch (Exception e)
            {
                HDLogUtil.error($"{cameraName}取图失败:{e}");
            }
            return codeisc;
        }
    }


    public class BarcodeInfo
    {
        //
        // 摘要:
        //     文本
        public string Text { get; set; }

        //
        // 摘要:
        //     条码类型
        public string CodeType { get; set; }

        //
        // 摘要:
        //     中心点
        public PointF Center { get; set; }

        //
        // 摘要:
        //     角度,3点钟方向0°,逆时针为正,顺时针为负。
        public float Angle { get; set; }

        //
        // 摘要:
        //     条码尺寸大小
        public SizeF Size { get; set; }

        //
        // 摘要:
        //     原点垂直于经过中心点的直线的距离
        public float Distance { get; set; }

        //
        // 摘要:
        //     副本,深拷贝
        public BarcodeInfo Clone()
        {
            BarcodeInfo barcodeInfo = new BarcodeInfo();
            PropertyInfo[] properties = barcodeInfo.GetType().GetProperties();
            PropertyInfo[] properties2 = GetType().GetProperties();
            for (int i = 0; i < properties.Length; i++)
            {
                properties[i].SetValue(barcodeInfo, properties2[i].GetValue(this));
            }

            return barcodeInfo;
        }
    }
}