Camera.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. using ivf_tl_Entity.GlobalEnums;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Runtime.ExceptionServices;
  6. using System.Runtime.InteropServices;
  7. using System.Security;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. namespace ivf_tl_Entity.CameraEntitys
  11. {
  12. /// <summary>
  13. /// 相机助手类
  14. /// </summary>
  15. public class Camera
  16. {
  17. public event Action<Exception, string, string, LogEnum> ExceptionLogEvent;
  18. private int index = 0;
  19. private int width = 0;
  20. private int height = 0;
  21. private int exposure = 0;
  22. [MarshalAs(UnmanagedType.LPTStr)]
  23. public string NumBer = "";
  24. /// <summary>
  25. /// MVC数字相机的索引号(从0开始),用于指定要进行初始化操作的相机。
  26. /// </summary>
  27. public int Index { get { return index; } }
  28. /// <summary>
  29. /// 图像宽度
  30. /// </summary>
  31. public int Width { get { return width; } }
  32. /// <summary>
  33. /// 图像高度
  34. /// </summary>
  35. public int Height { get { return height; } }
  36. /// <summary>
  37. /// 曝光时间(单位:100us)
  38. /// </summary>
  39. public int Exposure { get { return exposure; } }
  40. /// <summary>
  41. /// CCD是否加载
  42. /// </summary>
  43. public bool IsInit { get; set; } = false;
  44. /// <summary>
  45. /// CCD是否开启
  46. /// </summary>
  47. public bool IsStart { get; set; } = false;
  48. /// <summary>
  49. /// 图像字节数组
  50. /// </summary>
  51. public byte[] SourceBuffer
  52. {
  53. get
  54. {
  55. byte[] sourceBuffer = new byte[this.width * this.height * 3];
  56. Marshal.Copy(pDest, sourceBuffer, 0, sourceBuffer.Length);
  57. return sourceBuffer;
  58. }
  59. }
  60. /// <summary>
  61. /// 获取相机对应得序列号
  62. /// </summary>
  63. /// <returns></returns>
  64. [HandleProcessCorruptedStateExceptions]
  65. [SecurityCritical]
  66. public int GetNumbet()
  67. {
  68. try
  69. {
  70. int result = -1;
  71. lock (locker)
  72. {
  73. StringBuilder strSerial = new StringBuilder(125);
  74. result = MVCAPI.MV_Usb2GetSerial(this.hImager, strSerial);
  75. NumBer = strSerial.ToString();
  76. }
  77. return result;
  78. }
  79. catch (Exception ex)
  80. {
  81. ExceptionLogEvent?.Invoke(ex, "获取序列号", null, LogEnum.RunException);
  82. return -2;
  83. }
  84. }
  85. /// <summary>
  86. /// 获取一张图像
  87. /// </summary>
  88. //public BitmapImage BitmapImage
  89. //{
  90. // get
  91. // {
  92. // Bitmap bitmap = new Bitmap(this.width, this.height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
  93. // System.Drawing.Imaging.BitmapData bmpData = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), System.Drawing.Imaging.ImageLockMode.WriteOnly, bitmap.PixelFormat);
  94. // Marshal.Copy(this.SourceBuffer, 0, bmpData.Scan0, this.SourceBuffer.Length);
  95. // bitmap.UnlockBits(bmpData);
  96. // bitmap.RotateFlip(System.Drawing.RotateFlipType.RotateNoneFlipY);
  97. // BitmapImage bitmapImage = new BitmapImage();
  98. // using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
  99. // {
  100. // bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
  101. // bitmapImage.BeginInit();
  102. // bitmapImage.StreamSource = ms;
  103. // bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
  104. // bitmapImage.EndInit();
  105. // bitmapImage.Freeze();
  106. // }
  107. // return bitmapImage;
  108. // }
  109. //}
  110. /// <summary>
  111. /// 结构体
  112. /// </summary>
  113. private CapInfoStruct capInfoStruct = new CapInfoStruct();
  114. /// <summary>
  115. /// 由MV_Usb2Init返回的MVC设备句柄
  116. /// </summary>
  117. public IntPtr hImager;
  118. /// <summary>
  119. /// 回传的图像字节数组
  120. /// </summary>
  121. private IntPtr pDest;
  122. public Camera(int index, int width, int height, int exposure)
  123. {
  124. this.index = index;
  125. this.width = width;
  126. this.height = height;
  127. this.exposure = exposure;
  128. pDest = Marshal.AllocCoTaskMem(this.width * this.height * 3);
  129. }
  130. public Camera(CameraModel model)
  131. {
  132. this.index = model.Index;
  133. this.width = model.Width;
  134. this.height = model.Height;
  135. this.exposure = model.Exposure;
  136. pDest = Marshal.AllocCoTaskMem(this.width * this.height * 3);
  137. }
  138. /// <summary>
  139. /// 初使化相机[注:执行init()之后立即执行SetOpMode()以开启像捕捉 2021.11.11]
  140. /// </summary>
  141. /// <returns></returns>
  142. public int Init()
  143. {
  144. try
  145. {
  146. byte[] mGain = new byte[3];
  147. mGain[0] = Convert.ToByte(25);
  148. mGain[1] = Convert.ToByte(14);
  149. mGain[2] = Convert.ToByte(25);
  150. byte Control = Convert.ToByte(04);
  151. capInfoStruct.Buffer = Marshal.AllocCoTaskMem(this.width * this.height * 3);
  152. capInfoStruct.Width = this.width;
  153. capInfoStruct.Height = this.height;
  154. capInfoStruct.HorizontalOffset = 0;
  155. capInfoStruct.VerticalOffset = 0;
  156. capInfoStruct.Exposure = this.exposure;
  157. capInfoStruct.Gain = mGain;
  158. capInfoStruct.Control = Control;
  159. var result = InitCamera(this);
  160. if (result == 0) this.IsInit = true;
  161. // M8-P3b:相机初始化边界埋点(module=相机)。全 try 兜底。
  162. try
  163. {
  164. Aivfo.OperationLog.OperationLogger.Log("相机", "初始化",
  165. input: new { index, width, height, exposure },
  166. output: new { result, sn = NumBer },
  167. result: result == 0 ? "成功" : "失败",
  168. error: result == 0 ? null : $"InitCamera 返回 {result}");
  169. }
  170. catch { }
  171. return result;
  172. }
  173. catch (Exception ex)
  174. {
  175. ExceptionLogEvent?.Invoke(ex, $"{index}号相机初始化", null, LogEnum.RunException);
  176. return -2;
  177. }
  178. }
  179. public bool DisPose()
  180. {
  181. try
  182. {
  183. Marshal.FreeCoTaskMem(capInfoStruct.Buffer);
  184. Marshal.FreeCoTaskMem(pDest);
  185. return true;
  186. }
  187. catch (Exception ex)
  188. {
  189. ExceptionLogEvent?.Invoke(ex, $"{index}号相机释放内存", null, LogEnum.RunException);
  190. return false;
  191. }
  192. }
  193. public int Usb2Start(IntPtr mControlPtr, int left, int top, int width, int height)
  194. {
  195. var nRetValue = MVCAPI.MV_Usb2Start(hImager, "MVC Camera Preview", MVCAPI.WS_CHILD | MVCAPI.WS_VISIBLE, left, top, width, height, mControlPtr, 0, (int)System.Threading.ThreadPriority.Highest, (int)System.Threading.ThreadPriority.Highest);
  196. return nRetValue;
  197. }
  198. public int Usb2Stop()
  199. {
  200. var nRetValue = MVCAPI.MV_Usb2Stop(hImager);
  201. return nRetValue;
  202. }
  203. public int Usb2StartCapture()
  204. {
  205. try
  206. {
  207. var nRetValue = MVCAPI.MV_Usb2StartCapture(hImager, true);
  208. return nRetValue;
  209. }
  210. catch (Exception)
  211. {
  212. return -1;
  213. }
  214. }
  215. public int Usb2StopCapture()
  216. {
  217. try
  218. {
  219. var nRetValue = MVCAPI.MV_Usb2StartCapture(hImager, false);
  220. return nRetValue;
  221. }
  222. catch (Exception)
  223. {
  224. return -1;
  225. }
  226. }
  227. /// <summary>
  228. /// 多个方法共同锁
  229. /// </summary>
  230. private static readonly object locker = new object();
  231. [HandleProcessCorruptedStateExceptions]
  232. [SecurityCritical]
  233. private int InitCamera(Camera camera)
  234. {
  235. try
  236. {
  237. int result = -1;
  238. lock (locker)
  239. {
  240. //MVC2000
  241. result = MVCAPI.MV_Usb2Init("MVC2000", out camera.index, ref camera.capInfoStruct, out camera.hImager);
  242. }
  243. return result;
  244. }
  245. catch (Exception ex)
  246. {
  247. ExceptionLogEvent?.Invoke(ex, $"{camera.index}号相机初始化", null, LogEnum.RunException);
  248. return -2;
  249. }
  250. }
  251. /// <summary>
  252. /// 卸载相机
  253. /// </summary>
  254. /// <returns></returns>
  255. public int UnInit()
  256. {
  257. try
  258. {
  259. var result = UnInit(this);
  260. this.IsInit = result == 0;
  261. // M8-P3b:相机卸载边界埋点(module=相机)。全 try 兜底。
  262. try
  263. {
  264. Aivfo.OperationLog.OperationLogger.Log("相机", "卸载",
  265. input: new { index },
  266. output: new { result },
  267. result: result == 0 ? "成功" : "失败");
  268. }
  269. catch { }
  270. return result;
  271. }
  272. catch (Exception ex)
  273. {
  274. ExceptionLogEvent?.Invoke(ex, $"{index}号相机卸载", null, LogEnum.RunException);
  275. return -1;
  276. }
  277. }
  278. [HandleProcessCorruptedStateExceptions]
  279. [SecurityCritical]
  280. private int UnInit(Camera camera)
  281. {
  282. try
  283. {
  284. int result = -1;
  285. lock (locker)
  286. {
  287. result = MVCAPI.MV_Usb2Uninit(ref hImager);
  288. }
  289. return result;
  290. }
  291. catch (Exception ex)
  292. {
  293. ExceptionLogEvent?.Invoke(ex, $"{camera.index}号相机卸载", null, LogEnum.RunException);
  294. return -2;
  295. }
  296. }
  297. /// <summary>
  298. /// 设置摄像头的采集模式为连续采集模式
  299. /// </summary>
  300. /// <returns></returns>
  301. public int SetOpMode()
  302. {
  303. try
  304. {
  305. if (this.IsInit == false) return -1;
  306. var result = SetOpMode(this);
  307. this.IsStart = result == 0;
  308. return result;
  309. }
  310. catch (Exception ex)
  311. {
  312. ExceptionLogEvent?.Invoke(ex, $"{index}号相机设置采集模式", null, LogEnum.RunException);
  313. return -1;
  314. }
  315. }
  316. [HandleProcessCorruptedStateExceptions]
  317. [SecurityCritical]
  318. private int SetOpMode(Camera camera)
  319. {
  320. try
  321. {
  322. int result = -1;
  323. lock (locker)
  324. {
  325. result = MVCAPI.MV_Usb2SetOpMode(camera.hImager, 0, false);
  326. }
  327. return result;
  328. }
  329. catch (Exception ex)
  330. {
  331. ExceptionLogEvent?.Invoke(ex, $"{camera.index}号相机设置采集模式", null, LogEnum.RunException);
  332. return -2;
  333. }
  334. }
  335. /// <summary>
  336. /// 抓取一张图像
  337. /// </summary>
  338. /// <returns>返回该图字节数组</returns>
  339. public int GetRgbData()
  340. {
  341. try
  342. {
  343. if (this.IsStart == false) return -1;
  344. return GetRgbData(this);
  345. }
  346. catch (Exception ex)
  347. {
  348. ExceptionLogEvent?.Invoke(ex, $"{index}号相机抓图", null, LogEnum.RunException);
  349. return -2;
  350. }
  351. }
  352. [HandleProcessCorruptedStateExceptions]
  353. [SecurityCritical]
  354. private int GetRgbData(Camera camera)
  355. {
  356. try
  357. {
  358. int result = -1;
  359. lock (locker)
  360. {
  361. result = MVCAPI.MV_Usb2GetRgbData(camera.hImager, ref camera.capInfoStruct, camera.pDest);
  362. }
  363. return result;
  364. }
  365. catch (Exception ex)
  366. {
  367. ExceptionLogEvent?.Invoke(ex, $"{camera.index}号相机抓图", null, LogEnum.RunException);
  368. return -2;
  369. }
  370. }
  371. /// <summary>
  372. /// 抓取一张图像
  373. /// </summary>
  374. /// <returns>返回该图字节数组</returns>
  375. public int GetRawData()
  376. {
  377. if (this.IsStart == false) return -1;
  378. return GetRawData(this);
  379. }
  380. private int GetRawData(Camera camera)
  381. {
  382. try
  383. {
  384. int result = -1;
  385. lock (locker)
  386. {
  387. result = MVCAPI.MV_Usb2GetRawData(camera.hImager, ref camera.capInfoStruct);
  388. }
  389. return result;
  390. }
  391. catch (Exception ex)
  392. {
  393. ExceptionLogEvent?.Invoke(ex, $"{index}号相机抓图", null, LogEnum.RunException);
  394. return -2;
  395. }
  396. }
  397. /// <summary>
  398. /// 抓取一张图像
  399. /// </summary>
  400. /// <returns>返回该图字节数组</returns>
  401. public int RawToRgb()
  402. {
  403. if (this.IsStart == false) return -1;
  404. return RawToRgb(this);
  405. }
  406. private int RawToRgb(Camera camera)
  407. {
  408. try
  409. {
  410. int result = -1;
  411. lock (locker)
  412. {
  413. result = MVCAPI.MV_Usb2ConvertRawToRgb(camera.hImager, camera.capInfoStruct.Buffer, camera.width, camera.height, camera.pDest);
  414. }
  415. return result;
  416. }
  417. catch (Exception ex)
  418. {
  419. ExceptionLogEvent?.Invoke(ex, $"{index}号相机RawToRgb", null, LogEnum.RunException);
  420. return -2;
  421. }
  422. }
  423. /// <summary>
  424. /// 设置相机参数
  425. /// </summary>
  426. /// <param name="model"></param>
  427. /// <returns></returns>
  428. public int SetPartOfCapInfo(int Exposure)
  429. {
  430. return SetPartOfCapInfo(this, Exposure);
  431. }
  432. [HandleProcessCorruptedStateExceptions]
  433. [SecurityCritical]
  434. public int SetPartOfCapInfo(Camera camera, int Exposure)
  435. {
  436. try
  437. {
  438. int result;
  439. capInfoStruct.Exposure = Exposure;
  440. //this.width = model.Width;
  441. //this.height = model.Height;
  442. //capInfoStruct.Gain[0] = Convert.ToByte(model.Red);
  443. //capInfoStruct.Gain[1] = Convert.ToByte(model.Green);
  444. //capInfoStruct.Gain[2] = Convert.ToByte(model.Blue);
  445. //capInfoStruct.HorizontalOffset = model.HorizontalOffset;
  446. //capInfoStruct.VerticalOffset = model.VerticalOffset;
  447. //capInfoStruct.Exposure = model.Exposure;
  448. //capInfoStruct.Width = this.width;
  449. //capInfoStruct.Height = this.height;
  450. //capInfoStruct.Buffer = Marshal.AllocCoTaskMem(this.width * this.height);
  451. //pDest = Marshal.AllocCoTaskMem(this.width * this.height * 3);
  452. lock (locker)
  453. {
  454. result = MVCAPI.MV_Usb2SetPartOfCapInfo(hImager, ref capInfoStruct);
  455. }
  456. return result;
  457. }
  458. catch (Exception ex)
  459. {
  460. ExceptionLogEvent?.Invoke(ex, $"{camera.index}号相机设置参数", null, LogEnum.RunException);
  461. return -2;
  462. }
  463. }
  464. public bool SaveJPG(string fullName)
  465. {
  466. MemoryStream memoryStream = null;
  467. FileStream fs = null;
  468. bool success = false;
  469. try
  470. {
  471. memoryStream = new MemoryStream(this.SourceBuffer);
  472. fs = new FileStream(fullName, FileMode.OpenOrCreate);
  473. memoryStream.WriteTo(fs);
  474. success = true;
  475. }
  476. catch (Exception ex)
  477. {
  478. success = false;
  479. }
  480. finally
  481. {
  482. if (fs != null)
  483. {
  484. fs.Close();
  485. fs.Dispose();
  486. }
  487. if (memoryStream != null)
  488. {
  489. memoryStream.Close();
  490. memoryStream.Dispose();
  491. }
  492. }
  493. return success;
  494. }
  495. public bool SavePic(string fullName,int width,int height)
  496. {
  497. try
  498. {
  499. var a = MVCAPI.SavePic(this.SourceBuffer, width, height, fullName);
  500. if (a == 1) return true;
  501. return false;
  502. }
  503. catch (Exception ex)
  504. {
  505. ExceptionLogEvent?.Invoke(ex, $"保存图片", null, LogEnum.RunException);
  506. return false;
  507. }
  508. }
  509. }
  510. }