WebCamera.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. using System;
  2. using AForge.Video;
  3. using System.Drawing;
  4. using AForge.Controls;
  5. using System.Threading;
  6. using System.Windows.Forms;
  7. using AForge.Video.DirectShow;
  8. using System.Collections.Generic;
  9. using Microsoft.Extensions.Logging;
  10. namespace shjxCamera
  11. {
  12. public class WebCamera
  13. {
  14. #region private fields
  15. private Bitmap _currFrame;
  16. #endregion
  17. #region private properties
  18. public VideoCaptureDevice VidCapDevice { get; set; }
  19. private VideoSourcePlayer VidPlayer { get; set; }
  20. /// <summary>
  21. /// 当前画面,相机开启时有效
  22. /// </summary>
  23. public Bitmap CurrFrame
  24. {
  25. get => VidCapDevice is not null ? _currFrame.Clone() as Bitmap : null;
  26. protected set => _currFrame = value;
  27. }
  28. /// <summary>
  29. /// 摄像头是否已打开
  30. /// </summary>
  31. public bool IsOpened
  32. {
  33. get
  34. {
  35. bool isOpened = false;
  36. if (VidPlayer == null)
  37. {
  38. return isOpened;
  39. }
  40. if (VidPlayer.InvokeRequired)
  41. {
  42. VidPlayer.Invoke(new Action(() =>
  43. {
  44. isOpened = VidPlayer.IsRunning;
  45. }));
  46. }
  47. else
  48. {
  49. isOpened = VidPlayer.IsRunning;
  50. }
  51. return isOpened;
  52. }
  53. }
  54. #endregion
  55. #region static
  56. private static FilterInfoCollection vidCollection;
  57. public static FilterInfoCollection GetCameras()
  58. {
  59. vidCollection = new(FilterCategory.VideoInputDevice);
  60. return vidCollection;
  61. }
  62. public static string GetCameraMoniker(int index)
  63. {
  64. return vidCollection[index].MonikerString;
  65. }
  66. public static string GetCameraName(int index)
  67. {
  68. return vidCollection[index].Name;
  69. }
  70. #endregion
  71. #region events
  72. /// <summary>
  73. /// 画面刷新事件
  74. /// </summary>
  75. public event NewFrameEventHandler NewFrameEvent;
  76. #endregion
  77. #region 构造函数
  78. /// <summary>
  79. /// 初始化摄像头
  80. /// </summary>
  81. /// <param name="cameraMoniker">摄像头名称</param>
  82. public WebCamera(string cameraMoniker)
  83. {
  84. VidCapDevice = new VideoCaptureDevice(cameraMoniker);
  85. VidCapDevice.NewFrame += GetCurrFrame;
  86. VidPlayer = new VideoSourcePlayer
  87. {
  88. VideoSource = VidCapDevice
  89. };
  90. _currFrame = null;
  91. }
  92. /// <summary>
  93. /// 初始化摄像头
  94. /// </summary>
  95. public WebCamera()
  96. {
  97. VidCapDevice = new VideoCaptureDevice();
  98. VidPlayer = new VideoSourcePlayer
  99. {
  100. VideoSource = VidCapDevice
  101. };
  102. _currFrame = null;
  103. }
  104. #endregion
  105. #region Camera Operate
  106. /// <summary>
  107. /// 连接指定的摄像头,并打开视频信号
  108. /// </summary>
  109. /// <param name="cameraMoniker"></param>
  110. /// <returns></returns>
  111. public bool OpenCamera(string cameraMoniker)
  112. {
  113. this.VidCapDevice.Source = cameraMoniker;
  114. VidCapDevice.NewFrame += this.GetCurrFrame;
  115. this.VidCapDevice.Start();
  116. return this.VidCapDevice.IsRunning;
  117. }
  118. /// <summary>
  119. /// 打开摄像头
  120. /// </summary>
  121. /// <returns></returns>
  122. public bool OpenCamera()
  123. {
  124. if (VidCapDevice != null && VidCapDevice.IsRunning == false)
  125. {
  126. VidCapDevice.NewFrame += NewFrameEvent;
  127. VidCapDevice.Start();
  128. }
  129. if (VidPlayer.InvokeRequired)
  130. {
  131. VidPlayer.Invoke(new Action(() =>
  132. {
  133. if (VidPlayer != null && !VidPlayer.IsRunning)
  134. {
  135. VidPlayer.Start();
  136. }
  137. }));
  138. }
  139. else
  140. {
  141. if (VidPlayer != null && !VidPlayer.IsRunning)
  142. {
  143. VidPlayer.Start();
  144. }
  145. }
  146. return true;
  147. }
  148. /// <summary>
  149. /// 关闭摄像头
  150. /// </summary>
  151. public void CloseCamera()
  152. {
  153. if (VidCapDevice != null && VidCapDevice.IsRunning)
  154. {
  155. VidCapDevice.SignalToStop();
  156. VidCapDevice.WaitForStop();
  157. //VidCapDevice.Stop();
  158. VidCapDevice.NewFrame -= NewFrameEvent;
  159. }
  160. if (VidPlayer.InvokeRequired)
  161. {
  162. VidPlayer.Invoke(new Action(() =>
  163. {
  164. if (VidPlayer == null || !VidPlayer.IsRunning) return;
  165. VidPlayer.SignalToStop();
  166. VidPlayer.WaitForStop();
  167. }));
  168. }
  169. else
  170. {
  171. if (VidPlayer == null || !VidPlayer.IsRunning) return;
  172. VidPlayer.SignalToStop();
  173. VidPlayer.WaitForStop();
  174. }
  175. }
  176. /// <summary>
  177. /// 拍照
  178. /// </summary>
  179. /// <returns></returns>
  180. public Bitmap GrabImage()
  181. {
  182. Bitmap bmp = null;
  183. if (VidPlayer.InvokeRequired)
  184. {
  185. VidPlayer.Invoke(new Action(() =>
  186. {
  187. if (VidPlayer.IsRunning)
  188. {
  189. bmp = VidPlayer.GetCurrentVideoFrame();
  190. }
  191. else
  192. {
  193. // 探头未连接,重试
  194. int cnt = 0; // 重试10秒
  195. while (!VidPlayer.IsRunning && cnt < 100)
  196. {
  197. VidPlayer.Start();
  198. Thread.Sleep(100);
  199. cnt++;
  200. //PublicDatas.LogProgram.Error("摄像头未连接,正在重试" + cnt.ToString());
  201. }
  202. Thread.Sleep(300);
  203. bmp = VidPlayer.GetCurrentVideoFrame();
  204. }
  205. }));
  206. }
  207. else
  208. {
  209. if (VidPlayer.IsRunning)
  210. {
  211. bmp = VidPlayer.GetCurrentVideoFrame();
  212. }
  213. else
  214. {
  215. // 探头未连接,重试
  216. int cnt = 0; // 重试10秒
  217. while (!VidPlayer.IsRunning && cnt < 100)
  218. {
  219. VidPlayer.Start();
  220. Thread.Sleep(100);
  221. cnt++;
  222. // PublicDatas.LogProgram.Error("摄像头未连接,正在重试" + cnt.ToString());
  223. }
  224. Thread.Sleep(300);
  225. bmp = VidPlayer.GetCurrentVideoFrame();
  226. }
  227. }
  228. return bmp;
  229. }
  230. #endregion
  231. #region public methods
  232. /// <summary>
  233. /// 设置显示摄像头图像区域
  234. /// </summary>
  235. /// <param name="parentControl"></param>
  236. /// <param name="left"></param>
  237. /// <param name="top"></param>
  238. /// <param name="width"></param>
  239. /// <param name="height"></param>
  240. public void ShowControl(ref Panel parentControl, int left, int top, int width, int height)
  241. {
  242. if (VidPlayer == null) return;
  243. parentControl.Controls.Add(VidPlayer);
  244. VidPlayer.Location = new Point(left, top);
  245. VidPlayer.Size = new Size(width, height);
  246. VidPlayer.Visible = true;
  247. }
  248. /// <summary>
  249. /// 显示摄像头属性设置窗体
  250. /// </summary>
  251. /// <param name="parentWindow"></param>
  252. public void ShowProperties(IntPtr parentWindow)
  253. {
  254. VidCapDevice.DisplayPropertyPage(parentWindow);
  255. }
  256. /// <summary>
  257. /// 设置相机属性值
  258. /// </summary>
  259. /// <param name="prop"></param>
  260. /// <param name="val"></param>
  261. /// <param name="flags"></param>
  262. public void SetProperties(CameraControlProperty prop, int val, CameraControlFlags flags)
  263. {
  264. VidCapDevice.SetCameraProperty(prop, val, flags);// 取消自动曝光
  265. }
  266. /// <summary>
  267. /// 读取当前属性值
  268. /// </summary>
  269. /// <param name="prop"></param>
  270. /// <param name="val"></param>
  271. /// <param name="flags"></param>
  272. public void GetProperties(CameraControlProperty prop, out int val, out CameraControlFlags flags)
  273. {
  274. VidCapDevice.GetCameraProperty(prop, out val, out flags);
  275. }
  276. /// <summary>
  277. /// 读取相机参数的取值范围
  278. /// </summary>
  279. /// <param name="prop"></param>
  280. /// <param name="minValue"></param>
  281. /// <param name="maxValue"></param>
  282. /// <param name="stepSize"></param>
  283. /// <param name="defaultValue"></param>
  284. /// <param name="flags"></param>
  285. public void GetPropertiesRange(CameraControlProperty prop, out int minValue, out int maxValue, out int stepSize, out int defaultValue, out CameraControlFlags flags)
  286. {
  287. VidCapDevice.GetCameraPropertyRange(prop, out minValue, out maxValue, out stepSize, out defaultValue, out flags);
  288. }
  289. /// <summary>
  290. /// 设置视频属性值,亮度、对比度、白平衡、增益等
  291. /// </summary>
  292. /// <param name="prop"></param>
  293. /// <param name="val"></param>
  294. /// <param name="flags"></param>
  295. public void SetVideoProcAmpProperties(VideoProcAmpProperty prop, int val, VideoProcAmpFlags flags)
  296. {
  297. VidCapDevice.SetVideoProcAmpProperty(prop, val, flags);
  298. }
  299. /// <summary>
  300. /// 读取当前视频参数
  301. /// </summary>
  302. /// <param name="prop"></param>
  303. /// <param name="val"></param>
  304. /// <param name="flags"></param>
  305. public void GetVideoProcAmpProperties(VideoProcAmpProperty prop, out int val, out VideoProcAmpFlags flags)
  306. {
  307. VidCapDevice.GetVideoProcAmpProperty(prop, out val, out flags);
  308. }
  309. /// <summary>
  310. /// 读取视频参数的取值范围
  311. /// </summary>
  312. /// <param name="prop"></param>
  313. /// <param name="minValue"></param>
  314. /// <param name="maxValue"></param>
  315. /// <param name="stepSize"></param>
  316. /// <param name="defaultValue"></param>
  317. /// <param name="flags"></param>
  318. public void GetVideoProcAmpPropertiesRange(VideoProcAmpProperty prop, out int minValue, out int maxValue, out int stepSize, out int defaultValue, out VideoProcAmpFlags flags)
  319. {
  320. VidCapDevice.GetVideoProcAmpPropertyRange(prop, out minValue, out maxValue, out stepSize, out defaultValue, out flags);
  321. }
  322. /// <summary>
  323. /// 实时将新的画面存入CurrFrame对象中
  324. /// </summary>
  325. /// <param name="sender"></param>
  326. /// <param name="e"></param>
  327. public void GetCurrFrame(object sender, NewFrameEventArgs e)
  328. {
  329. try
  330. {
  331. if (e.Frame != null)
  332. {
  333. CurrFrame = e.Frame;
  334. }
  335. }
  336. catch (Exception)
  337. {
  338. // ignored
  339. }
  340. }
  341. #endregion
  342. #region 未使用
  343. /// <summary>
  344. /// 拍照,返回CurrFrame图像
  345. /// </summary>
  346. /// <returns></returns>
  347. public Bitmap GrabImage2()
  348. {
  349. return CurrFrame;
  350. }
  351. /// <summary>
  352. /// 返回视频支持的分辨率
  353. /// </summary>
  354. /// <returns></returns>
  355. public List<string> GetVideoResolution()
  356. {
  357. List<string> resolution = new List<string>();
  358. foreach (var cap in VidCapDevice.VideoCapabilities)
  359. {
  360. resolution.Add(string.Format("{0} x {1}", cap.FrameSize.Width, cap.FrameSize.Height));
  361. }
  362. return resolution;
  363. }
  364. /// <summary>
  365. /// 设定视频的分辨率,index可以通过GetVideoResolution()获取所有支持的分辨率来确定
  366. /// </summary>
  367. /// <param name="index">在所有支持分辨率中的序号</param>
  368. public void SetVideoResolution(int index)
  369. {
  370. if (VidCapDevice.VideoCapabilities.Length > index)
  371. {
  372. VidCapDevice.VideoResolution = VidCapDevice.VideoCapabilities[index];
  373. }
  374. }
  375. #endregion
  376. // 抓取一帧图像,截取中间区域,按灰度值过滤后,计算平均RGB值
  377. public ColorPoint CaptureOne(string captureTag, int size, int gray_min, int gray_max)
  378. {
  379. Bitmap bmp = GrabImage11();
  380. if (bmp is null)
  381. {
  382. return new(0, 0, 0, captureTag);
  383. }
  384. int cntLow = 0, cntHigh = 0, cntNormal = 0;
  385. int sumR = 0, sumG = 0, sumB = 0;
  386. for (int y = bmp.Height / 2 - size / 2; y < bmp.Height / 2 + size / 2; y++)
  387. {
  388. for (int x = bmp.Width / 2 - size / 2; x < bmp.Width / 2 + size / 2; x++)
  389. {
  390. Color cc = bmp.GetPixel(x, y);
  391. int gray = Math.Min(255, (int)(cc.R * 0.299 + cc.G * 0.587 + cc.B * 0.114));
  392. if (gray < gray_min)
  393. {
  394. cntLow++;
  395. continue;
  396. }
  397. if (gray > gray_max)
  398. {
  399. cntHigh++;
  400. continue;
  401. }
  402. cntNormal++;
  403. sumR += cc.R;
  404. sumG += cc.G;
  405. sumB += cc.B;
  406. }
  407. }
  408. int avgR = 0, avgG = 0, avgB = 0;
  409. if (cntNormal > 0)
  410. {
  411. avgR = sumR / cntNormal;
  412. avgG = sumG / cntNormal;
  413. avgB = sumB / cntNormal;
  414. }
  415. // 记录颜色采样信息
  416. ColorPoint cp = new(avgR, avgG, avgB, captureTag);
  417. float total = size * size;
  418. if (!"watch".Equals(captureTag))
  419. {
  420. //logger.LogTrace($"<{captureTag}> HSB({cp.Hue,3},{cp.Saturation:F3},{cp.Brightness:F3}), RGBL({cp.R,3},{cp.G,3},{cp.B,3},{cp.Gray,3}), PLMH({total},{cntLow / total:F3},{cntNormal / total:F3},{cntHigh / total:F3}), WH:({bmp.Width},{bmp.Height})");
  421. //bmp.Save($"Data/{captureTag}.jpg", ImageFormat.Jpeg);
  422. }
  423. return cp;
  424. }
  425. // 背景底色采样
  426. public ColorPoint SampleBackground(string taskTag, int threshold_gray, int threshold_hue)
  427. {
  428. List<ColorPoint> cps = new List<ColorPoint>();
  429. for (int i = 0; i < 99; i++)
  430. {
  431. cps.Add(Sample($"{taskTag}/000S-{i + 1:D2}", threshold_gray, threshold_hue));
  432. if (cps.Count >= 5)
  433. {
  434. // 如排序后前三轮或后三轮的色调偏差小于阈值,完成采样
  435. ColorPoint[] ar = cps.ToArray();
  436. Array.Sort(ar);
  437. //Array.Reverse(ar);
  438. if (Math.Abs(ar[0].Hue - ar[2].Hue) <= threshold_hue || Math.Abs(ar[2].Hue - ar[4].Hue) <= threshold_hue)
  439. {
  440. ColorPoint cp = ar[2];
  441. return cp;
  442. }
  443. // 未达标,丢弃最早抓取的帧,继续采样
  444. cps.RemoveAt(0);
  445. }
  446. Thread.Sleep(100);
  447. }
  448. // 背景底色采样达到最大次数
  449. //logger.LogWarning($"<{taskTag}> 背景底色采样达到最大次数!");
  450. return cps.Last();
  451. }
  452. // 色值采样(抓取n帧图像合并计算)
  453. public ColorPoint Sample(string prefix, int threshold_gray, int threshold_hue)
  454. {
  455. int gray_min = threshold_gray;
  456. int gray_max = 255 - threshold_gray;
  457. List<ColorPoint> cps = new List<ColorPoint>();
  458. for (int i = 0; i < 99; i++)
  459. {
  460. string captureTag = $"{prefix}-{i + 1:D2}";
  461. int size = 200;
  462. ColorPoint cp = CaptureOne(captureTag, size, gray_min, gray_max);
  463. // 丢弃亮度过低的帧,继续采样
  464. if (cp.Gray <= gray_min)
  465. {
  466. Thread.Sleep(100);
  467. continue;
  468. }
  469. cps.Add(cp);
  470. if (cps.Count >= 5)
  471. {
  472. // 构建排列的数组
  473. ColorPoint[] ar = cps.ToArray();
  474. Array.Sort(ar);
  475. //Array.Reverse(ar);
  476. // 前三帧色调偏差小于等于阈值,取前三帧完成采样
  477. if (Math.Abs(ar[0].Hue - ar[2].Hue) <= threshold_hue)
  478. {
  479. return Merge(ar.Take(3).ToList(), prefix);
  480. }
  481. // 后三帧色调偏差小于等于阈值,取后三帧完成采样
  482. if (Math.Abs(ar[2].Hue - ar[4].Hue) <= threshold_hue)
  483. {
  484. return Merge(ar.Skip(2).Take(3).ToList(), prefix);
  485. }
  486. // 未达标,丢弃最早抓取的帧,继续采样
  487. cps.RemoveAt(0);
  488. }
  489. Thread.Sleep(100);
  490. }
  491. // 达到最大采样次数限制,返回最后一次采样结果
  492. //logger.LogWarning($"<{prefix}> 色值采样达到最大次数!");
  493. if (cps.Count > 0)
  494. {
  495. return cps.Last();
  496. }
  497. return new ColorPoint(0, 0, 0, prefix);
  498. }
  499. // 合并计算色值
  500. private static ColorPoint Merge(List<ColorPoint> list, string prefix)
  501. {
  502. int n = list.Count;
  503. if (n == 0)
  504. {
  505. return new ColorPoint(0, 0, 0, prefix);
  506. }
  507. int sampleR = 0, sampleG = 0, sampleB = 0;
  508. for (int i = 0; i < n; i++)
  509. {
  510. sampleR += list[i].R;
  511. sampleG += list[i].G;
  512. sampleB += list[i].B;
  513. }
  514. ColorPoint cp = new(sampleR / n, sampleG / n, sampleB / n, prefix);
  515. //logger.LogInformation($"<{prefix}> HSB:({cp.Hue,3},{cp.Saturation:F3},{cp.Brightness:F3}), Gray:{cp.Gray,3}, RGB:({cp.R,3},{cp.G,3},{cp.B,3})");
  516. return cp;
  517. }
  518. public Bitmap GrabImage11()
  519. {
  520. Bitmap bmp = null;
  521. if (VidPlayer.InvokeRequired)
  522. {
  523. VidPlayer.Invoke(new Action(() => bmp = VidPlayer.GetCurrentVideoFrame()));
  524. }
  525. else
  526. {
  527. bmp = VidPlayer.GetCurrentVideoFrame();
  528. }
  529. return bmp;
  530. }
  531. }
  532. }