FrmTest.cs 7.0 KB
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Runtime.ExceptionServices;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace IDHIKCamera
{
    public partial class FrmTest : Form
    {
        public FrmTest()
        {
            InitializeComponent();
            pictureBox1.Show();
        }
        /// <summary>
        /// 初始化摄像机名称和二维码类型
        /// </summary> 
        public static void LoadConfig()
        {
            try
            {

                IDHIK.Instance.Load();
                List<string> names = IDHIK.Instance.cameraName;


                if (names != null)
                {
                    // hikNameList.AddRange(names);
                    foreach (string name in names)
                    {
                        LibLogUtil.Info("加载到IDHIK相机:" + name);
                    }
                }
                //IDHIK.Instance.OpenAll();
            }
            catch (Exception ex)
            {
                LibLogUtil.Error("解析IDHIK出错:" + ex.StackTrace);
            }
        }

        private void btnIDHIKInit_Click(object sender, EventArgs e)
        {
            LoadConfig();
            foreach (string name in IDHIK.Instance.cameraName)
            {
                cbDeviceList.Items.Add(name);
            }

            if (cbDeviceList.Items.Count > 0)
            {
                cbDeviceList.SelectedIndex = 0;
                btnIDHIKInit.Visible = false;
            }
        }
        private Task scanTask = null;
        private int count = 0;
        private void btnIDHIKTest_Click(object sender, EventArgs e)
        {
            StopTest = true;
            string cName = cbDeviceList.Text;
            if (cName == "")
            {
                return;
            }
            ScanTest(cName);
            count = 1;
            if (checkBox1.Checked)
            {
                StopTest = false;
                scanTask = Task.Factory.StartNew(() =>
                {
                    while (checkBox1.Checked)
                    {
                        if (StopTest)
                        {
                            break;
                        }
                        count++;
                        ScanTest(cName);
                        Thread.Sleep(100);
                    }
                });
            }

        }

        private void ScanTest(string cName)
        {
            try
            {

                List<CodeInfo> codes = CameraScan(new string[] { cName });
                lblCodeInfo.Text = "";
                string str = $"扫码{count}:\r\n";
                foreach (CodeInfo code in codes)
                {
                    str += $"[{code.CodeType}][{code.X},{code.Y}]{code.CodeStr}\r\n";

                }
                lblCodeInfo.Text = str;
            }
            catch (Exception ex)
            {
                LibLogUtil.Error("出错:" + ex.ToString());
            }
        }



        [HandleProcessCorruptedStateExceptions]
        public List<CodeInfo> CameraScan(string[] cameraNameList)
        {
            HashSet<string> codestr = new HashSet<string>();
            List<CodeInfo> codeList = new List<CodeInfo>();
            if (cameraNameList == null || cameraNameList.Length <= 0)
            {
                throw new Exception("CameraScan方法没有传入相机名称.");
            }

            try
            {
                foreach (string cameraName in cameraNameList)
                {
                    string r = "";
                    LibLogUtil.Info($"【" + cameraName + "】开始取图片");
                    if (cameraName.Trim().Equals(""))
                    {
                        continue;
                    }
                    DateTime startTime = DateTime.Now;

                    Image bmp = null;
                    try
                    {
                        bmp = IDHIK.Instance.GrabOne(cameraName, out List<CodeInfo> cc);

                        //HalconDotNet.HOperatorSet.RotateImage()
                        if (bmp == null)
                        {
                            LibLogUtil.Error("  【" + cameraName + "】取图片失败[" + IDHIK.Instance._errInfo + "],关闭相机");
                            IDHIK.Instance.Close(cameraName);
                            continue;
                        }
                        LibLogUtil.Info("  【" + cameraName + "】取图片扫码完成,数量【" + cc.Count + "】");

                        cc.ForEach((c) =>
                        {
                            if (!codestr.Contains(c.CodeStr))
                            {
                                codeList.Add(c);
                                codestr.Add(c.CodeStr);
                                r += "##" + c.CodeStr;
                            }
                        });


                        LibLogUtil.Info(" 【" + cameraName + "】" + " 扫码完成【" + (DateTime.Now - startTime).TotalSeconds.ToString() + "】 " + count + ":" + r);


                        pictureBox1.Image = (Image)bmp;
                        pictureBox1.Refresh();
                    }
                    catch (AccessViolationException e)
                    {
                        LibLogUtil.Error(" 扫码出现AccessViolationException异常,关闭相机【" + cameraName + "】:" + e.ToString());
                        //  GC.Collect();
                    }
                    catch (Exception ex)
                    {
                        LibLogUtil.Error(" 扫码出错:" + ex.ToString());
                    }
                    finally
                    {
                        if (bmp != null)
                            bmp.Dispose();
                    }
                }
            }
            catch (AccessViolationException e)
            {
                LibLogUtil.Error(" 扫码出现AccessViolationException异常:" + e.ToString());
            }
            catch (Exception ex)
            {
                LibLogUtil.Error(" 扫码出错:" + ex.ToString());
            }
            return codeList;
        }
        private bool StopTest = false;
        private void btnExit_Click(object sender, EventArgs e)
        {
            StopTest = true;
            checkBox1.Checked = false;
            this.Close();
        }

        private void FrmTest_Load(object sender, EventArgs e)
        {
            Control.CheckForIllegalCrossThreadCalls = false;
            LoadConfig();
            foreach (string name in IDHIK.Instance.cameraName)
            {
                cbDeviceList.Items.Add(name);
            }

            if (cbDeviceList.Items.Count > 0)
            {
                cbDeviceList.SelectedIndex = 0;
            }
            else
            {
                btnIDHIKInit.Visible = true;
            }
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            checkBox1.Checked = false;
        }
    }
}