TestPlate.cs 3.0 KB
using CodeLibrary;
using OnlineStore.Common;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Runtime.ExceptionServices;
using System.Text;
using System.Threading.Tasks;

namespace OnlineStore.DeviceLibrary
{
    public class TestPlate
    {
        private static Asa.Signal signal;
        static string deviceName = "出口相机";
        static TestPlate() {
            string path1 = @"backgroundimage.png";
            signal = new Asa.Signal();
            //signal.ExistChanged += Signal_ExistChanged;
            bool rtn = signal.BgImage(path1);
            LogUtil.info("出料图像识别背景图加载:" + rtn);
            signal.RegionAdd(new Asa.Signal.EyemRegion() { X = 1773, Y = 1097, Width = 1125, Height = 337, Ratio = 0.2 });
            signal.RegionAdd(new Asa.Signal.EyemRegion() { X = 1773, Y = 1889, Width = 1125, Height = 685, Ratio = 0.2 });
            //cameraName = Config.CameraName;
        }
        [HandleProcessCorruptedStateExceptions]
        public static bool[] TestIt(string cameraName, bool needsaveone = false)
        {
            HalconDotNet.HObject ho_Image = null;
            Bitmap bmp=null;
            try
            {
                ho_Image = Camera._cam.CaptureOnImage(cameraName, out bmp);
                if (ho_Image == null)
                {
                    LogUtil.error(deviceName + "  【" + cameraName + "】取图片失败[" + Camera._cam.ErrInfo + "],关闭相机");
                    Camera._cam.Close(cameraName);
                    Task.Delay(1000).Wait();
                    ho_Image = Camera._cam.CaptureOnImage(cameraName, out bmp);
                    if (ho_Image == null)
                    {
                        LogUtil.error(deviceName + "  【" + cameraName + "】第2次取图片失败[" + Camera._cam.ErrInfo + "],关闭相机");
                        return new bool[] { false, false };
                    }
                }
                LogUtil.info("开始图像识别料盘");
                bool rtn = signal.Compare(bmp);
                if (needsaveone)
                {
                    CodeManager.SaveImageToFile("料盘识别", cameraName, bmp);
                }
                if (rtn)
                {
                    LogUtil.error(deviceName + $"  图像匹配成功:[0]:{signal.IsExist[0]},[1]:{signal.IsExist[1]}");
                    return signal.IsExist;
                }
                else
                {
                    LogUtil.error(deviceName + "  图像匹配失败");
                    return new bool[] { false, false };
                }

            }
            catch (Exception e)
            {
                LogUtil.error(deviceName + "  " + e.Message);
            }
            finally {
                ho_Image.Dispose();
                if (bmp != null) {
                    bmp.Dispose();
                }
                
            }
            return new bool[] { false, false };
        }
    }
}