// 枚举相机 private void DeviceListAcq() { // 清空列表 cbDeviceList.Items.Clear(); System.GC.Collect(); // 枚举海康相机 if ((chooseHIK) && (!chooseBasler)) { try { // 相机数量 uint cameraNum = hIKVisionCamera.CameraNum(); // 枚举相机 List <string> items = hIKVisionCamera.EnumDevices(); for (int i = 0; i < cameraNum; i++) { cbDeviceList.Items.Add(items[i]); } // 选择第一项 if (cameraNum != 0) { cbDeviceList.SelectedIndex = 0; } } catch { MessageBox.Show("枚举设备失败!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } // 枚举basler相机 else if ((chooseBasler) && (!chooseHIK)) { try { // 返回相机数量 int cameraNum = baslerCamera.CameraNum(); // 枚举相机 List <ICameraInfo> items = baslerCamera.CameraEnum(); for (int i = 0; i < cameraNum; i++) { if (items[i][CameraInfoKey.DeviceType] == "BaslerGigE") { cbDeviceList.Items.Add("GigE: Basler " + items[i][CameraInfoKey.ModelName]); } else if (items[i][CameraInfoKey.DeviceType] == "BaslerUsb") { cbDeviceList.Items.Add("USB: Basler " + items[i][CameraInfoKey.ModelName]); } } // 选择第一项 if (cameraNum != 0) { cbDeviceList.SelectedIndex = 0; } } catch { MessageBox.Show("枚举设备失败,请检查连接状态!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } }
// 枚举相机 private void DeviceListAcq() { // 清空列表 cbDeviceList.DataSource = null; cbDeviceList.Items.Clear(); System.GC.Collect(); // 枚举海康相机 if ((chooseHIK) && (!chooseBasler)) { try { // 相机数量 uint cameraNum = hIKVisionCamera.CameraNum(); // 枚举相机 List <string> items = hIKVisionCamera.EnumDevices(); for (int i = 0; i < cameraNum; i++) { cbDeviceList.Items.Add(items[i]); } // 选择第一项 if (cameraNum != 0) { cbDeviceList.SelectedIndex = 0; } } catch { MessageBox.Show("枚举设备失败!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } // 枚举basler相机 else if ((chooseBasler) && (!chooseHIK)) { try { // 返回相机数量 int cameraNum = baslerCamera.CameraNum(); // 枚举相机 List <ICameraInfo> items = baslerCamera.CameraEnum(); for (int i = 0; i < cameraNum; i++) { if (items[i][CameraInfoKey.DeviceType] == "BaslerGigE") { cbDeviceList.Items.Add("GigE: Basler " + items[i][CameraInfoKey.ModelName]); } else if (items[i][CameraInfoKey.DeviceType] == "BaslerUsb") { cbDeviceList.Items.Add("USB: Basler " + items[i][CameraInfoKey.ModelName]); } } // 选择第一项 if (cameraNum != 0) { cbDeviceList.SelectedIndex = 0; } } catch { MessageBox.Show("枚举设备失败,请检查连接状态!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } else if ((chooseUSB) && (!chooseHIK) && (!chooseBasler)) { // 枚举USB相机 try { //连接相机 usbDeviceSource.initCamera(); //usbDeviceSource.callBackHandler += videoSourcePlayer_NewFrameReceived; //回调函数处理视频帧 //usbDeviceSource.videoSourcePlayerCallBackHandler += videoSourcePlayer_NewFrame; if (usbDeviceSource.DeviceExist) { // 返回相机数量 int cameraNum = usbDeviceSource.CameraNum(); // 枚举相机 foreach (FilterInfo device in usbDeviceSource.CameraEnum()) { cbDeviceList.Items.Add(device.Name); } // https://www.cnblogs.com/xiaoliangge/p/6006055.html // 选择第一项 if (cameraNum != 0) { cbDeviceList.SelectedIndex = 0; } this.bnOpen.Enabled = true; } else { throw new Exception(); } } catch { this.bnOpen.Enabled = false; cbDeviceList.Items.Add("枚举设备失败,请检查连接状态!"); cbDeviceList.SelectedIndex = 0; MessageBox.Show("枚举设备失败,请检查连接状态!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } }