TestPlate.cs
3.0 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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 };
}
}
}