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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
using LB_VisionProcesses.Cameras;
using LB_VisionProcesses.Cameras.HRCameras;
using LB_VisionProcesses.Cameras.LBCameras;
using RJCP.IO.Ports;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
 
 
namespace LB_SmartVision.Forms.Pages.CameraPage
{
    public partial class CreateCameraForm : Form
    {
        public BaseCamera camera { get; set; }
        public bool bCreate = false;
 
        public CreateCameraForm(ICamera camera = null)
        {
            InitializeComponent();
            // 禁止修改窗口大小
            this.FormBorderStyle = FormBorderStyle.FixedDialog;
            if (camera == null)
                camera = new HRCamera();
 
            uiButtonCreate.Enabled = false;
        }
 
        private void uiButtonTest_Click(object sender, EventArgs e)
        {
            uiButtonCreate.Enabled = false;
 
            if (string.IsNullOrEmpty(uiComboBoxSN.Text))
                MessageBox.Show($"初始化相机[{uiComboBoxSN.Text}]为空!", "异常");
 
            if (camera == null)
            {
                MessageBox.Show("请先选择相机品牌!", "异常");
                return;
            }
 
            camera.CloseDevice();
            if (camera.InitDevice(uiComboBoxSN.Text, this.Handle))
            {
                uiButtonCreate.Enabled = true;
                camera.CloseDevice();
            }
            else
                MessageBox.Show($"初始化相机[{uiComboBoxSN.Text}]失败!", "异常");
        }
 
        private void uiButtonCreate_Click(object sender, EventArgs e)
        {
            if (camera.InitDevice(uiComboBoxSN.Text, this.Handle))
            {
                bCreate = true;
                this.Close();
            }
            else
                MessageBox.Show($"初始化相机[{uiComboBoxSN.Text}]失败!", "异常");
        }
 
        private void uiButtonCancel_Click(object sender, EventArgs e)
        {
            if (camera != null)
                camera.CloseDevice();
            bCreate = false;
            this.Close();
        }
 
        private void uiComboBoxBrand_MouseClick(object sender, MouseEventArgs e)
        {
            uiComboBoxBrand.Items.Clear();
            foreach (CameraBrand brand in Enum.GetValues(typeof(CameraBrand)))
            {
                if (brand != CameraBrand.UNSUPPORTED)
                    uiComboBoxBrand.Items.Add(brand.ToString());
            }
        }
 
        private void uiComboBoxBrand_SelectedIndexChanged(object sender, EventArgs e)
        {
            uiButtonCreate.Enabled = false;
            uiComboBoxSN.Text = string.Empty;
 
            if (camera != null)
                camera.CloseDevice();
 
            Enum.TryParse<CameraBrand>(uiComboBoxBrand.Text, out CameraBrand brand);
            switch (brand)
            {
                case CameraBrand.HRCamera:
                    camera = new HRCamera();
                    break;
                case CameraBrand.LBCamera:
                    camera = new LBCamera();
                    break;
                default:
                    return;
            }
        }
 
        private void uiComboBoxSN_MouseClick(object sender, MouseEventArgs e)
        {
            uiButtonCreate.Enabled = false;
            uiComboBoxSN.Text = string.Empty;
 
            uiComboBoxSN.Items.Clear();
            var list = camera.GetListEnum();
 
            foreach (string sn in list)
                uiComboBoxSN.Items.Add(sn);
        }
    }
}