MIChrome5Pro.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. using PaintDotNet.Setting;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Drawing;
  5. using System.Runtime.InteropServices;
  6. using System.Threading;
  7. using TUCAMAPI;
  8. using TUCamera;
  9. namespace PaintDotNet.ImageCollect.CameraManager
  10. {
  11. internal class MIChrome5Pro : CameraPublic
  12. {
  13. public bool m_isWaiting;
  14. public bool m_isRecording = false;
  15. public Thread m_waitingThread;
  16. public TUCAM_FRAME m_frame;
  17. public TUCAM_TRIGGER_ATTR m_triggerAttr = new TUCAM_TRIGGER_ATTR();
  18. public SetValueDel setValueDel;
  19. public CameraPreviewDialog cameraPreviewDialog;
  20. public CameraSettingDialog cameraSettingDialog;
  21. byte[] pBuf = null;
  22. object lockObj = new object();
  23. private TUCAM_OPEN m_handler;
  24. public TUCamera.TUCamera m_currentCamera;
  25. public CameraPreviewDialog CameraPreviewDialog
  26. {
  27. set
  28. {
  29. this.cameraPreviewDialog = value;
  30. }
  31. }
  32. public CameraSettingDialog CameraSettingDialog
  33. {
  34. set
  35. {
  36. this.cameraSettingDialog = value;
  37. }
  38. }
  39. public SetValueDel SetValueDel
  40. {
  41. set
  42. {
  43. this.setValueDel = value;
  44. }
  45. }
  46. public MIChrome5Pro()
  47. {
  48. m_currentCamera = TUCameraManager.GetInstance().GetCurrentCamera();
  49. m_handler = m_currentCamera.GetHandler();
  50. }
  51. /// <summary>
  52. /// 初始化
  53. /// </summary>
  54. public override void Init()
  55. {
  56. this.m_isRecording = false;
  57. this.m_triggerAttr.nTgrMode = (int)TUCAM_CAPTURE_MODES.TUCCM_SEQUENCE;
  58. this.m_triggerAttr.nFrames = 1;
  59. this.m_triggerAttr.nDelayTm = 0;
  60. this.m_triggerAttr.nExpMode = (int)TUCAM_TRIGGER_EXP.TUCTE_EXPTM;
  61. this.m_triggerAttr.nEdgeMode = (int)TUCAM_TRIGGER_EDGE.TUCTD_RISING;
  62. }
  63. /// <summary>
  64. /// 释放
  65. /// </summary>
  66. public override void Uninit()
  67. {
  68. //关闭所有打开的
  69. m_currentCamera.Close();
  70. }
  71. /// <summary>
  72. /// 开始预览
  73. /// </summary>
  74. ///
  75. public override void StartPreview()
  76. {
  77. this.StartWaitForFrame(this.m_handler);
  78. }
  79. /// <summary>
  80. /// 停止预览
  81. /// </summary>
  82. public override void StopPreview()
  83. {
  84. if (this.m_isWaiting)
  85. this.StopWaitForFrame(this.m_handler);
  86. }
  87. /// <summary>
  88. /// 获取当前分辨率
  89. /// </summary>
  90. /// <returns></returns>
  91. public unsafe override Size GetCurrentResolution()
  92. {
  93. /*
  94. Size size = new Size();
  95. TUCAM_VALUE_INFO valueInfo = new TUCAM_VALUE_INFO();
  96. valueInfo.nID = (int)TUCAM_IDINFO.TUIDI_CURRENT_WIDTH;
  97. TUCamAPI.TUCAM_Dev_GetInfo(this.m_opCamList[this.m_indexCam].hIdxTUCam, ref valueInfo);
  98. size.Width = valueInfo.nValue;
  99. valueInfo.nID = (int)TUCAM_IDINFO.TUIDI_CURRENT_HEIGHT;
  100. TUCamAPI.TUCAM_Dev_GetInfo(this.m_opCamList[this.m_indexCam].hIdxTUCam, ref valueInfo);
  101. size.Height = valueInfo.nValue;
  102. return size;
  103. */
  104. return m_currentCamera.GetResolution();
  105. }
  106. public void StopWaitForFrame(TUCAM_OPEN opCam)
  107. {
  108. if (!m_isWaiting)
  109. {
  110. return;
  111. }
  112. if (m_currentCamera.IsOpen())
  113. {
  114. m_isWaiting = false;
  115. TUCamAPI.TUCAM_Buf_AbortWait(opCam.hIdxTUCam);
  116. //m_waitingThread.Join();//存疑
  117. m_waitingThread.Abort();
  118. TUCamAPI.TUCAM_Cap_Stop(opCam.hIdxTUCam); // Stop capture
  119. TUCamAPI.TUCAM_Buf_Release(opCam.hIdxTUCam); // Release alloc buffer after stop capture and quit drawing thread
  120. }
  121. }
  122. public void StartWaitForFrame(TUCAM_OPEN opCam)
  123. {
  124. if (m_isWaiting)
  125. {
  126. return;
  127. }
  128. if (m_currentCamera.IsOpen())
  129. {
  130. m_isWaiting = true;
  131. m_waitingThread = new Thread(new ThreadStart(WaitForFrameThreadEntity));
  132. m_frame.pBuffer = IntPtr.Zero;
  133. m_frame.ucFormatGet = (byte)TUFRM_FORMATS.TUFRM_FMT_USUAl;
  134. m_frame.uiRsdSize = 1;
  135. TUCamAPI.TUCAM_Buf_Alloc(opCam.hIdxTUCam, ref m_frame); // Alloc buffer after set resolution or set ROI attribute
  136. TUCamAPI.TUCAM_Cap_Start(opCam.hIdxTUCam, (uint)m_triggerAttr.nTgrMode); // Start capture
  137. m_waitingThread.Start();
  138. }
  139. }
  140. /// <summary>
  141. /// 获取图像帧
  142. /// </summary>
  143. public void WaitForFrameThreadEntity()
  144. {
  145. IntPtr hIdxTUCam = this.m_handler.hIdxTUCam;
  146. while (m_isWaiting)
  147. {
  148. m_frame.ucFormatGet = (byte)TUFRM_FORMATS.TUFRM_FMT_USUAl;
  149. if (TUCAMRET.TUCAMRET_SUCCESS == TUCamAPI.TUCAM_Buf_WaitForFrame(hIdxTUCam, ref m_frame))
  150. {
  151. if (IntPtr.Zero != m_frame.pBuffer)
  152. {
  153. int nSize = (int)(m_frame.uiImgSize + m_frame.usHeader);
  154. if(pBuf==null) pBuf = new byte[nSize];
  155. Marshal.Copy(m_frame.pBuffer, pBuf, 0, nSize);
  156. Buffer.BlockCopy(pBuf, (int)(m_frame.usHeader), pBuf, 0, (int)(m_frame.uiImgSize));
  157. if(cameraPreviewDialog!=null && !cameraPreviewDialog.IsDisposed)
  158. {
  159. this.cameraPreviewDialog.Invoke((EventHandler)(delegate
  160. {
  161. setValueDel(pBuf, lockObj);
  162. }));
  163. }
  164. /* if (cameraSettingDialog != null && !cameraSettingDialog.IsDisposed)
  165. {
  166. this.cameraSettingDialog.Invoke((EventHandler)(delegate
  167. {
  168. setValueDel(pBuf, lockObj);
  169. }));
  170. }*/
  171. }
  172. }
  173. }
  174. }
  175. public void ManualRelease()
  176. {
  177. if (pBuf != null)
  178. {
  179. pBuf = null;
  180. }
  181. if (m_waitingThread != null)
  182. {
  183. if (m_waitingThread.ThreadState == ThreadState.Stopped)
  184. {
  185. m_waitingThread = null;
  186. }
  187. }
  188. this.Dispose();
  189. this.Close();
  190. GC.Collect();
  191. }
  192. /// <summary>
  193. /// 判断颜色灰度值 如果是true 则设置为灰色
  194. /// </summary>
  195. /// <param name="isGray"></param>
  196. public void SetColorModel(bool isGray) {
  197. if (m_currentCamera.IsOpen())
  198. {
  199. TUCamAPI.TUCAM_Capa_SetValue(this.m_handler.hIdxTUCam, (int)TUCAM_IDCAPA.TUIDC_MONOCHROME, isGray ? 1 : 0);
  200. }
  201. }
  202. }
  203. }