You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

506 lines
20 KiB

1 month ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Drawing.Imaging;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Windows.Forms;
  12. using AForge.Video.DirectShow;
  13. using System.Threading;
  14. using System.Reflection;
  15. using ShenTun.ImageCollection.Common;
  16. using OpenCvSharp;
  17. namespace ShenTun.ImageCollection.AForgeCamera
  18. {
  19. public partial class FrmCamera : Form
  20. {
  21. private FilterInfoCollection videoDevices;
  22. private VideoCaptureDevice videoSource;
  23. private ImageData imgData;
  24. private RequestInterface requset;
  25. public DialogResponse Result { get; private set; }
  26. public FrmCamera()
  27. {
  28. InitializeComponent();
  29. }
  30. public FrmCamera(RequestInterface _requset)
  31. {
  32. InitializeComponent();
  33. requset= _requset;
  34. // this.Refresh();
  35. }
  36. private void CameraConn()
  37. {
  38. videoSource = new VideoCaptureDevice(videoDevices[cmbCamera.SelectedIndex].MonikerString);
  39. int index = Convert.ToInt32(cmbPixelVideo.SelectedValue);
  40. videoSource.VideoResolution = videoSource.VideoCapabilities[index];
  41. videoSourcePlayer1.VideoSource = videoSource;
  42. }
  43. private void SetPatientInfo()
  44. {
  45. if (requset == null) return;
  46. lbBarCode.Text = requset.BarCode;
  47. lbPatName.Text = requset.PatientName;
  48. lbSexName.Text = requset.SexName;
  49. lbAge.Text = requset.Age;
  50. lbAsbitenNames.Text = requset.AsbitemName;
  51. }
  52. private void SetRangValue()
  53. {
  54. VideoProcAmpFlags flag;
  55. int value, maxvalue, minvalue, stepSize, defalutvalue;
  56. videoSource.GetVideoProperty(VideoProcAmpProperty.Brightness, out value, out flag);
  57. videoSource.GetVideoPropertyRange(VideoProcAmpProperty.Brightness, out minvalue, out maxvalue, out stepSize, out defalutvalue, out flag);
  58. tbBrightness.MaxValue = maxvalue;
  59. tbBrightness.MinValue = minvalue;
  60. tbBrightness.Tag = flag;
  61. tbBrightness.Value = value;
  62. videoSource.GetVideoProperty(VideoProcAmpProperty.Contrast, out value, out flag);
  63. videoSource.GetVideoPropertyRange(VideoProcAmpProperty.Contrast, out minvalue, out maxvalue, out stepSize, out defalutvalue, out flag);
  64. tbContrast.MaxValue = maxvalue;
  65. tbContrast.MinValue = minvalue;
  66. tbContrast.Tag = flag;
  67. tbContrast.Value = value;
  68. videoSource.GetVideoProperty(VideoProcAmpProperty.Hue, out value, out flag);
  69. videoSource.GetVideoPropertyRange(VideoProcAmpProperty.Hue, out minvalue, out maxvalue, out stepSize, out defalutvalue, out flag);
  70. tbHue.MaxValue = maxvalue;
  71. tbHue.MinValue = minvalue;
  72. tbHue.Tag = flag;
  73. tbHue.Value = value;
  74. videoSource.GetVideoProperty(VideoProcAmpProperty.Saturation, out value, out flag);
  75. videoSource.GetVideoPropertyRange(VideoProcAmpProperty.Saturation, out minvalue, out maxvalue, out stepSize, out defalutvalue, out flag);
  76. tbSaturation.MaxValue = maxvalue;
  77. tbSaturation.MinValue = minvalue;
  78. tbSaturation.Tag = flag;
  79. tbSaturation.Value = value;
  80. videoSource.GetVideoProperty(VideoProcAmpProperty.Gamma, out value, out flag);
  81. videoSource.GetVideoPropertyRange(VideoProcAmpProperty.Gamma, out minvalue, out maxvalue, out stepSize, out defalutvalue, out flag);
  82. tbGamma.MaxValue = maxvalue;
  83. tbGamma.MinValue = minvalue;
  84. tbGamma.Tag = flag;
  85. tbGamma.Value = value;
  86. //videoSource.GetVideoProperty(VideoProcAmpProperty.ColorEnable, out value, out flag);
  87. }
  88. private void setValue(VideoProcAmpProperty vpap)
  89. {
  90. int value = 0;
  91. VideoProcAmpFlags flag;
  92. switch (vpap)
  93. {
  94. case VideoProcAmpProperty.Brightness:
  95. value = Convert.ToInt32(tbBrightness.Value);
  96. flag=(VideoProcAmpFlags)tbBrightness.Tag;
  97. videoSource.SetVideoProperty(VideoProcAmpProperty.Brightness, value, flag);
  98. break;
  99. case VideoProcAmpProperty.Contrast:
  100. value = Convert.ToInt32(tbContrast.Value);
  101. flag = (VideoProcAmpFlags)tbContrast.Tag;
  102. videoSource.SetVideoProperty(VideoProcAmpProperty.Contrast, value, flag);
  103. break;
  104. case VideoProcAmpProperty.Hue:
  105. value = Convert.ToInt32(tbHue.Value);
  106. flag = (VideoProcAmpFlags)tbHue.Tag;
  107. videoSource.SetVideoProperty(VideoProcAmpProperty.Hue, value, flag);
  108. break;
  109. case VideoProcAmpProperty.Saturation:
  110. value = Convert.ToInt32(tbSaturation.Value);
  111. flag = (VideoProcAmpFlags)tbSaturation.Tag;
  112. videoSource.SetVideoProperty(VideoProcAmpProperty.Saturation, value, flag);
  113. break;
  114. case VideoProcAmpProperty.Gamma:
  115. value = Convert.ToInt32(tbGamma.Value);
  116. flag = (VideoProcAmpFlags)tbGamma.Tag;
  117. videoSource.SetVideoProperty(VideoProcAmpProperty.Gamma, value, flag);
  118. break;
  119. }
  120. }
  121. private void SetCmbDevice()
  122. {
  123. if (videoDevices.Count == 0) return;
  124. string name = "";
  125. List<DeviceItem> list = new List<DeviceItem>();
  126. for (int i = 0; i < videoDevices.Count; i++)
  127. {
  128. name = videoDevices[0].Name;
  129. DeviceItem d = new DeviceItem()
  130. {
  131. index = i,
  132. text = name,
  133. };
  134. list.Add(d);
  135. }
  136. cmbCamera.DisplayMember = "text";
  137. cmbCamera.ValueMember = "index";
  138. cmbCamera.DataSource = list;
  139. }
  140. private void SetCmbPixelVideo(VideoCapabilities[] _videoCapabilities)
  141. {
  142. if (_videoCapabilities.Length == 0) return;
  143. string text = "";
  144. List<VideoPixelItem> list = new List<VideoPixelItem>();
  145. for (int i = 0; i < _videoCapabilities.Length; i++)
  146. {
  147. text = string.Format("{0} x {1}", _videoCapabilities[i].FrameSize.Width, _videoCapabilities[i].FrameSize.Height);
  148. VideoPixelItem item = new VideoPixelItem()
  149. {
  150. index = i,
  151. text = text,
  152. width = _videoCapabilities[i].FrameSize.Width
  153. };
  154. list.Add(item);
  155. }
  156. var listSort= list.OrderByDescending(p => p.width).ToList();
  157. cmbPixelVideo.DataSource = listSort;
  158. cmbPixelVideo.DisplayMember = "text";
  159. cmbPixelVideo.ValueMember = "index";
  160. }
  161. private void FrmCamera_Load(object sender, EventArgs e)
  162. {
  163. try
  164. {
  165. this.SetStyle(ControlStyles.UserPaint, true);
  166. this.SetStyle(ControlStyles.AllPaintingInWmPaint, true); // 禁止擦除背景.
  167. this.SetStyle(ControlStyles.DoubleBuffer, true);
  168. // 枚举所有视频输入设备
  169. videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
  170. imgData = new ImageData();
  171. if (videoDevices.Count == 0)
  172. throw new ApplicationException();
  173. SetCmbDevice();
  174. cmbCamera.SelectedIndex = 0;
  175. var _cameraDevice = new VideoCaptureDevice(videoDevices[0].MonikerString);
  176. SetCmbPixelVideo(_cameraDevice.VideoCapabilities);
  177. CameraConn();
  178. videoSourcePlayer1.Start();
  179. //int cameraIndex = Convert.ToInt32(IniConfigHelper.ReadSetting(ini_file, "Option", "CameraIndex", "0"));
  180. //cmbCamera.SelectedValue = cameraIndex;
  181. SetPatientInfo();
  182. SetRangValue();
  183. Result = new DialogResponse() { code = -1 };
  184. //this.Refresh();
  185. //this.Invalidate();
  186. if (requset.VideoDevice == -1)
  187. {
  188. MessageBox.Show("请在客户端参数中设置视频设备");
  189. }
  190. else
  191. {
  192. cmbCamera.SelectedValue = requset.VideoDevice;
  193. }
  194. if (requset.VideoPixel == -1)
  195. {
  196. MessageBox.Show("请在客户端参数中设置视频分辨率");
  197. }
  198. else {
  199. cmbPixelVideo.SelectedValue = requset.VideoPixel;
  200. }
  201. }
  202. catch (ApplicationException)
  203. {
  204. //cmbCamera.Items.Add("没有本地设备");
  205. videoDevices = null;
  206. }
  207. catch (Exception ex)
  208. {
  209. //cmbCamera.Items.Add("没有本地设备");
  210. videoDevices = null;
  211. MessageBox.Show("初始化失败:" + ex.Message);
  212. }
  213. }
  214. private void cmbCamera_SelectedValueChanged(object sender, EventArgs e)
  215. {
  216. try
  217. {
  218. if (this.cmbCamera.SelectedValue == null) return;
  219. if (videoDevices.Count == 0 || videoDevices.Count == 1) return;
  220. int index = Convert.ToInt32(this.cmbCamera.SelectedValue);
  221. var _cameraDevice = new VideoCaptureDevice(videoDevices[index].MonikerString);
  222. SetCmbPixelVideo(_cameraDevice.VideoCapabilities);
  223. }
  224. catch (Exception ex)
  225. {
  226. MessageBox.Show("切换摄像头失败:" + ex.Message);
  227. }
  228. }
  229. private void cmbPixelVideo_SelectedValueChanged(object sender, EventArgs e)
  230. {
  231. try
  232. {
  233. if (videoSourcePlayer1.VideoSource == null) return;
  234. //if (cmbCamera.Enabled) return;
  235. VideoCaptureDevice videoSource = (VideoCaptureDevice)videoSourcePlayer1.VideoSource;
  236. int index = Convert.ToInt32(cmbPixelVideo.SelectedValue);
  237. videoSource.VideoResolution = videoSource.VideoCapabilities[index];
  238. videoSourcePlayer1.VideoSource = videoSource;
  239. if (videoSourcePlayer1.IsRunning)
  240. {
  241. videoSourcePlayer1.SignalToStop();
  242. videoSourcePlayer1.WaitForStop();
  243. }
  244. videoSourcePlayer1.Start();
  245. }
  246. catch (Exception ex)
  247. {
  248. MessageBox.Show("切换分辨率异常:" + ex.Message);
  249. }
  250. }
  251. private void btnDeviceProperty_Click(object sender, EventArgs e)
  252. {
  253. VideoCaptureDevice videoSource = (VideoCaptureDevice)videoSourcePlayer1.VideoSource;
  254. videoSource.DisplayPropertyPage(IntPtr.Zero);
  255. }
  256. private void tbBrightness_ValueChanged(object sender, EventArgs e)
  257. {
  258. try
  259. {
  260. setValue(VideoProcAmpProperty.Brightness);
  261. }
  262. catch (Exception ex)
  263. {
  264. MessageBox.Show(ex.Message);
  265. }
  266. }
  267. private void tbContrast_ValueChanged(object sender, EventArgs e)
  268. {
  269. try
  270. {
  271. setValue(VideoProcAmpProperty.Contrast);
  272. }
  273. catch (Exception ex)
  274. {
  275. MessageBox.Show(ex.Message);
  276. }
  277. }
  278. private void tbHue_ValueChanged(object sender, EventArgs e)
  279. {
  280. try
  281. {
  282. setValue(VideoProcAmpProperty.Hue);
  283. }
  284. catch (Exception ex)
  285. {
  286. MessageBox.Show(ex.Message);
  287. }
  288. }
  289. private void tbSaturation_ValueChanged(object sender, EventArgs e)
  290. {
  291. try
  292. {
  293. setValue(VideoProcAmpProperty.Saturation);
  294. }
  295. catch (Exception ex)
  296. {
  297. MessageBox.Show(ex.Message);
  298. }
  299. }
  300. private void tbGamma_ValueChanged(object sender, EventArgs e)
  301. {
  302. try
  303. {
  304. setValue(VideoProcAmpProperty.Gamma);
  305. }
  306. catch (Exception ex)
  307. {
  308. MessageBox.Show(ex.Message);
  309. }
  310. }
  311. private void btnPhotoGraph_Click(object sender, EventArgs e)
  312. {
  313. try
  314. {
  315. if (!videoSourcePlayer1.IsRunning) return;
  316. this.btnPhotoGraph.Enabled = false;
  317. Bitmap imgMap = this.videoSourcePlayer1.GetCurrentVideoFrame();
  318. int row = dgvImg.Rows.Add();
  319. dgvImg.Rows[row].Cells[0].Value = 1;
  320. dgvImg.Rows[row].Cells[1].Value = imgMap;
  321. dgvImg.Rows[row].Cells[2].Value = getFilePath();
  322. dgvImg.Refresh();
  323. }
  324. catch (Exception ex)
  325. {
  326. MessageBox.Show("采集图像异常:" + ex.Message);
  327. }
  328. finally
  329. {
  330. this.btnPhotoGraph.Enabled = true;
  331. }
  332. }
  333. private string getFilePath()
  334. {
  335. DateTime dt = DateTime.Now;
  336. string year = dt.Year.ToString();
  337. string month = dt.Month.ToString();
  338. string day = dt.Day.ToString();
  339. string file_path = ImageHelper.GetLoaclPath(requset.ImagePath, requset.AsbitemName);
  340. string fileName = string.Format("{0}\\{1}_{2}.{3}", file_path, requset.BarCode, "0001", "jpg");
  341. if (File.Exists(fileName))
  342. {
  343. for (int i = 1; i < requset.Total; i++)
  344. {
  345. int index = fileName.IndexOf("_");
  346. string idStr = fileName.Substring(index + 1, 4);
  347. int id = Convert.ToInt32(idStr);
  348. id++;
  349. string temp = Convert.ToString(id).PadLeft(4, '0');
  350. fileName = string.Format("{0}\\{1}_{2}.{3}", file_path, requset.BarCode, temp, "jpg");
  351. if (!File.Exists(fileName)) break;
  352. }
  353. }
  354. return fileName;
  355. }
  356. private void btnClose_Click(object sender, EventArgs e)
  357. {
  358. try
  359. {
  360. if (string.IsNullOrEmpty(requset.ImagePath))
  361. {
  362. Result.message = "本地图片目录未设置";
  363. return;
  364. }
  365. if (string.IsNullOrEmpty(requset.BarCode))
  366. {
  367. Result.message = "条形码不能为空";
  368. return;
  369. }
  370. if (videoSourcePlayer1.IsRunning)
  371. {
  372. videoSourcePlayer1.SignalToStop();
  373. videoSourcePlayer1.WaitForStop();
  374. }
  375. List<ImageResponse> list = new List<ImageResponse>();
  376. foreach(DataGridViewRow row in dgvImg.Rows)
  377. {
  378. Bitmap imgMap = (Bitmap)row.Cells[1].Value;
  379. string fileName = row.Cells[2].Value.ToString();
  380. ImageCodecInfo Encoder = ImageHelper.GetEncoder(ImageFormat.Jpeg);
  381. System.Drawing.Imaging.Encoder myEncoder = System.Drawing.Imaging.Encoder.Quality;
  382. EncoderParameters myEncoderParameters = new EncoderParameters(1);
  383. EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, requset.Quality);
  384. myEncoderParameters.Param[0] = myEncoderParameter;
  385. if (File.Exists(fileName)) File.Delete(fileName);
  386. imgMap.Save(fileName, Encoder, myEncoderParameters);
  387. System.Drawing.Image imge = System.Drawing.Image.FromFile(fileName);
  388. ImageResponse imageResponse = new ImageResponse()
  389. {
  390. Image = ImageHelper.ConvertImageToBase64(imge),
  391. FilePath = fileName,
  392. ImageFormat ="jpg"
  393. };
  394. list.Add(imageResponse);
  395. }
  396. Result=new DialogResponse() {
  397. code = 1,
  398. data= list
  399. };
  400. this.DialogResult= DialogResult.OK;
  401. }
  402. catch (Exception ex)
  403. {
  404. MessageBox.Show("采集图像异常:" + ex.Message);
  405. }
  406. }
  407. private void btnImageShare_Click(object sender, EventArgs e)
  408. {
  409. if (dgvImg.Rows.Count == 0) return;
  410. Bitmap imgMap = (Bitmap)dgvImg.Rows[dgvImg.CurrentCell.RowIndex].Cells[1].Value;
  411. string fileName = dgvImg.Rows[dgvImg.CurrentCell.RowIndex].Cells[2].Value.ToString();
  412. MemoryStream mstr = new MemoryStream();
  413. imgMap.Save(mstr, ImageFormat.Jpeg);
  414. FrmImage frm=new FrmImage(mstr, fileName);
  415. frm.ShowDialog();
  416. if (frm.DialogResult != DialogResult.OK) return;
  417. dgvImg.Rows[dgvImg.CurrentCell.RowIndex].Cells[1].Value = ImageData.Image;
  418. }
  419. private void btnImageContrast_Click(object sender, EventArgs e)
  420. {
  421. try
  422. {
  423. //string path_current = string.Format("{0}\\ShenTun\\ComparisonPicture\\{1}\\{2}\\{3}", System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments), DateTime.Now.Year,DateTime.Now.Month,DateTime.Now.Day);
  424. string path_current = string.Format("{0}\\ShenTun\\ComparisonPicture", System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments));
  425. if (!Directory.Exists(path_current))
  426. {
  427. Directory.CreateDirectory(path_current);
  428. }
  429. string path1 = string.Format("{0}\\{1}.{2}", path_current, "p1", "jpg");
  430. if(File.Exists(path1))File.Delete(path1);
  431. string path2 = string.Format("{0}\\{1}.{2}", path_current, "p2", "jpg");
  432. if (File.Exists(path2)) File.Delete(path2);
  433. if (dgvImg.Rows.Count == 0) return;
  434. int count = 0;
  435. for (int i = 0; i < dgvImg.Rows.Count; i++) {
  436. if (dgvImg.Rows[i].Cells[0].Value.ToString().Equals("1")) count++;
  437. }
  438. if (count != 2)
  439. {
  440. MessageBox.Show("图片数量必须为2张选中");
  441. return;
  442. }
  443. count = 0;
  444. foreach (DataGridViewRow row in dgvImg.Rows)
  445. {
  446. if (row.Cells[0].Value.ToString().Equals("1") && count == 0)
  447. {
  448. Bitmap imgMap = (Bitmap)row.Cells[1].Value;
  449. imgMap.Save(path1, ImageFormat.Jpeg);
  450. count++;
  451. }
  452. else if(row.Cells[0].Value.ToString().Equals("1") && count != 0)
  453. {
  454. Bitmap imgMap = (Bitmap)row.Cells[1].Value;
  455. imgMap.Save(path2, ImageFormat.Jpeg);
  456. }
  457. }
  458. FrmComparison frm = new FrmComparison(path1, path2);
  459. frm.ShowDialog();
  460. }
  461. catch(Exception ex)
  462. {
  463. MessageBox.Show("操作图像比对异常:" + ex.Message);
  464. }
  465. }
  466. }
  467. }