Guest User

Untitled

a guest
May 29th, 2015
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.51 KB | None | 0 0
  1. using System;
  2. using System.Drawing;
  3. using System.IO;
  4. using System.Runtime.InteropServices;
  5. using System.Windows.Forms;
  6.  
  7. namespace TH.Parking.Wrapper.HBCamera
  8. {
  9. public class HB18TCamera
  10. {
  11. private IntPtr m_channelHandle;
  12. private string m_imagestoreFolderPath;
  13. private string m_videostoreFolderPath;
  14. private FileStream m_fStream;
  15. private string m_recordFilePath = string.Empty;
  16. private int m_maxFileSize = 0;
  17. private ulong m_CallBackChannelHandle = 100;
  18. private IntPtr[] m_cameraHandle;
  19.  
  20. public IntPtr[] CameraHandleChannels
  21. {
  22. get { return m_cameraHandle; }
  23. set { m_cameraHandle = value; }
  24. }
  25.  
  26. public ulong CallBackChannelHandle
  27. {
  28. get { return m_CallBackChannelHandle; }
  29. set { m_CallBackChannelHandle = value; }
  30. }
  31.  
  32. public int MaxRecordFileSize
  33. {
  34. get { return m_maxFileSize; }
  35. set { m_maxFileSize = value; }
  36. }
  37. public static string ImageFolderPath = "";
  38. public HB18TCamera(IntPtr handleChannel, string imageFolderPath, string videoFolderPath, int maxRecordFileSize)
  39. {
  40. m_channelHandle = handleChannel;
  41. m_imagestoreFolderPath = imageFolderPath;
  42. m_videostoreFolderPath = videoFolderPath;
  43. m_maxFileSize = maxRecordFileSize;
  44.  
  45. if (!Directory.Exists(this.m_imagestoreFolderPath))
  46. Directory.CreateDirectory(this.m_imagestoreFolderPath);
  47.  
  48. if (!Directory.Exists(this.m_imagestoreFolderPath))
  49. Directory.CreateDirectory(this.m_imagestoreFolderPath);
  50.  
  51. }
  52.  
  53. public static void AddWatermark(string watermarkText, string inputFilePath, string directionText = "")
  54. {
  55. FileStream fs = new FileStream(inputFilePath, FileMode.Open, FileAccess.Read);
  56. System.Drawing.Image bitmap = (System.Drawing.Image)Bitmap.FromStream(fs);
  57. fs.Close();
  58. fs.Dispose();
  59.  
  60. Font font = new Font("Courier New", 15, FontStyle.Bold, GraphicsUnit.Pixel);
  61. Point atpoint = new Point(120, 20);
  62. SolidBrush brush = new SolidBrush(Color.White);
  63. Graphics graphics = Graphics.FromImage(bitmap);
  64.  
  65. StringFormat sf = new StringFormat();
  66. sf.Alignment = StringAlignment.Center;
  67. sf.LineAlignment = StringAlignment.Center;
  68.  
  69. graphics.DrawString(watermarkText, font, brush, atpoint, sf);
  70.  
  71.  
  72. atpoint = new Point(30, 330);
  73. if (string.IsNullOrEmpty(directionText))
  74. {
  75. directionText = "Vào";
  76. }
  77. else
  78. {
  79. directionText = "Ra";
  80. }
  81.  
  82. graphics.DrawString(directionText, font, brush, atpoint, sf);
  83. graphics.Dispose();
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90. MemoryStream m = new MemoryStream();
  91. bitmap.Save(m, System.Drawing.Imaging.ImageFormat.Jpeg);
  92.  
  93. FileStream fileStr = File.OpenWrite(inputFilePath);
  94. m.WriteTo(fileStr);
  95. fileStr.Flush();
  96. fileStr.Close();
  97. }
  98.  
  99. public IntPtr ChannelHandle
  100. {
  101. get { return m_channelHandle; }
  102. set { m_channelHandle = value; }
  103. }
  104.  
  105. public string ImageStoreFolderPath
  106. {
  107. get { return m_imagestoreFolderPath; }
  108. set { m_imagestoreFolderPath = value; }
  109. }
  110.  
  111. public string VideoStoreFolderPath
  112. {
  113. get { return m_videostoreFolderPath; }
  114. set { m_videostoreFolderPath = value; }
  115. }
  116.  
  117. public static string GetUniqueFilePath(string filePath)
  118. {
  119. int tmp = 1;
  120. while (File.Exists(filePath))
  121. {
  122. tmp++;
  123. filePath = filePath.Replace("M.", string.Format("M({0}).", tmp.ToString()));
  124. }
  125. return filePath;
  126. }
  127. //[System.Runtime.InteropServices.DllImport("Kernal32")]
  128. [DllImport("kernel32", SetLastError = true)]
  129. private extern static Boolean CloseHandle(IntPtr handle);
  130.  
  131. public string CaptureImage()
  132. {
  133. string nowString = DateTime.Now.ToString("dd_MM_yyyy_HH_mm_ss_tt");
  134. string timeString = DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss tt");
  135. string fileName = nowString + ".bmp";
  136. string fullPath = GetUniqueFilePath(this.m_imagestoreFolderPath + "\\" + fileName);
  137. IntPtr imgBuff = IntPtr.Zero;
  138.  
  139. try
  140. {
  141. imgBuff = new IntPtr(704 * 576 * 2);
  142.  
  143. ulong size = (704 * 576 * 2);
  144. int tmp = T1800.T18_GetOriginalImage(this.m_channelHandle, imgBuff, ref size, 1);
  145. int bRet = -1;
  146. if (size == 352 * 288 * 2)
  147. bRet = T1800.T18_SaveYUVToBmpFile(fullPath, imgBuff, 352, 288, 1);
  148. else if (size == 704 * 288 * 2)
  149. bRet = T1800.T18_SaveYUVToBmpFile(fullPath, imgBuff, 704, 288, 1);
  150. else if (size == 704 * 576 * 2)
  151. bRet = T1800.T18_SaveYUVToBmpFile(fullPath, imgBuff, 704, 576, 1);
  152. else if (size == 352 * 240 * 2)
  153. bRet = T1800.T18_SaveYUVToBmpFile(fullPath, imgBuff, 352, 240, 1);
  154. else if ((size == 704 * 240 * 2))
  155. bRet = T1800.T18_SaveYUVToBmpFile(fullPath, imgBuff, 704, 240, 1);
  156. else if (size == 704 * 480 * 2)
  157. bRet = T1800.T18_SaveYUVToBmpFile(fullPath, imgBuff, 704, 480, 1);
  158. if (bRet > -1 && !string.IsNullOrEmpty(fullPath))
  159. {
  160. AddWatermark(timeString, fullPath);
  161. }
  162. }
  163. catch
  164. {
  165. fullPath = string.Empty;
  166. }
  167. finally
  168. {
  169. if (imgBuff != IntPtr.Zero)
  170. {
  171. CloseHandle(imgBuff);
  172. imgBuff = IntPtr.Zero;
  173. }
  174. }
  175.  
  176. return fullPath;
  177. }
  178.  
  179. public void BeginRecord(bool reqShowPreview = false)
  180. {
  181. if (!CameraUtilities.AllowRecordVideo)
  182. return;
  183.  
  184. Log.Logger.Error("Begin record called");
  185. string fileName = DateTime.Now.ToString("dd_MM_yyyy_HH_mm_ss_tt") + ".264";
  186. bool bRes = true;
  187. try
  188. {
  189. int tmpError = -1;
  190. m_recordFilePath = GetUniqueFilePath(this.m_videostoreFolderPath + "\\" + fileName);
  191.  
  192. tmpError = T1800.T18_SetupSubChannel(this.m_channelHandle, 0);
  193. tmpError = T1800.T18_StartVideoCapture(this.m_channelHandle);
  194. Log.Logger.Error(tmpError);
  195. tmpError = T1800.T18_SetupSubChannel(this.m_channelHandle, 1);
  196.  
  197. if (m_fStream != null) m_fStream.Close();
  198. if (File.Exists(m_recordFilePath)) File.Delete(m_recordFilePath);
  199. m_fStream = new FileStream(m_recordFilePath, FileMode.OpenOrCreate);
  200.  
  201. if (tmpError == -1) bRes = false;
  202. if (reqShowPreview)
  203. StartVideoPreview();
  204.  
  205. Log.Logger.Error("Begin recod");
  206. }
  207. catch (Exception ex)
  208. {
  209.  
  210. Log.Logger.Error(ex);
  211. bRes = false;
  212. }
  213. }
  214.  
  215. public bool EndRecord(bool bForceStopReview = false)
  216. {
  217. if (!CameraUtilities.AllowRecordVideo)
  218. return false;
  219. bool bRes = true;
  220.  
  221.  
  222. Log.Logger.Error("End record called");
  223. try
  224. {
  225. T1800.T18_SetupSubChannel(this.m_channelHandle, 0);
  226. T1800.T18_StopVideoCapture(this.m_channelHandle);
  227. T1800.T18_SetupSubChannel(this.m_channelHandle, 1);
  228. if (m_fStream != null)
  229. {
  230. Log.Logger.Error(m_fStream.Length);
  231. m_fStream.Flush();
  232. m_fStream.Close();
  233. m_fStream = null;
  234.  
  235. }
  236. else
  237. {
  238.  
  239. Log.Logger.Error("Stream null");
  240. }
  241. if (bForceStopReview)
  242. StopVideoPreview();
  243. Log.Logger.Error("Endded recod");
  244. }
  245. catch (Exception ex)
  246. {
  247. Log.Logger.Error("End record error");
  248. bRes = false;
  249. throw;
  250. }
  251. return bRes;
  252. }
  253. IntPtr _previewBoxHandle;
  254. Rectangle _rect;
  255. public bool StartVideoPreview(PictureBox picBox, bool forceRecord = true)
  256. {
  257. return StartVideoPreview(picBox.Handle, picBox.Width, picBox.Height, forceRecord);
  258. }
  259.  
  260.  
  261. public bool StartVideoPreview(IntPtr previewBoxHandle, int width, int height, bool forceRecord = true)
  262. {
  263. bool bRes = true;
  264. try
  265. {
  266. Rectangle rect = new Rectangle();
  267. _rect = rect;
  268. _previewBoxHandle = previewBoxHandle;
  269. rect.Size = new Size(width, height);
  270. int tmp = T1800.T18_StartVideoPreviewEx(m_channelHandle, previewBoxHandle, ref rect, false, 0, 30);
  271. if (tmp == -1) bRes = false;
  272. }
  273. catch
  274. {
  275. bRes = false;
  276.  
  277. }
  278. if (forceRecord && bRes && CameraUtilities.AllowRecordVideo)
  279. BeginRecord();
  280. return bRes;
  281. }
  282.  
  283. public bool StartVideoPreview(bool forceRecord = true)
  284. {
  285.  
  286. bool bRes = true;
  287. try
  288. {
  289.  
  290. int tmp = T1800.T18_StartVideoPreviewEx(m_channelHandle, _previewBoxHandle, ref _rect, false, 0, 30);
  291. if (tmp == -1) bRes = false;
  292. }
  293. catch
  294. {
  295. bRes = false;
  296.  
  297. }
  298. if (bRes)
  299. BeginRecord();
  300. return bRes;
  301. }
  302.  
  303. public bool StopVideoPreview(bool forceEndRecording = true)
  304. {
  305. if (forceEndRecording)
  306. EndRecord();
  307. bool bRes = false;
  308. bRes = T1800.T18_StopVideoPreview(m_channelHandle) != -1;
  309. return bRes;
  310. }
  311.  
  312. public int SaveCallBackData()
  313. {
  314. Log.Logger.Error("SaveCallBackData");
  315. IntPtr m_pDataStream = IntPtr.Zero;
  316. Application.DoEvents();
  317. try
  318. {
  319. Application.DoEvents();
  320. GC.KeepAlive(m_fStream);
  321. GC.KeepAlive(m_maxFileSize);
  322. GC.KeepAlive(m_recordFilePath);
  323. GC.KeepAlive(m_imagestoreFolderPath);
  324. GC.KeepAlive(m_videostoreFolderPath);
  325. Application.DoEvents();
  326. byte[] bSystemHeader = new byte[200 * 1024];
  327. ulong nDataLength = 200 * 1024;
  328. m_pDataStream = new IntPtr(200 * 1024);
  329. int nFrameType = 0;
  330. Application.DoEvents();
  331. while (true)
  332. {
  333. Application.DoEvents();
  334. int nError = T1800.T18_ReadStreamData(this.m_channelHandle, bSystemHeader, ref nDataLength, ref nFrameType);
  335.  
  336. Log.Logger.Error(nDataLength);
  337.  
  338. if (m_fStream == null) m_fStream = new FileStream(m_recordFilePath, FileMode.OpenOrCreate);
  339. m_fStream.Write(bSystemHeader, 0, (int)nDataLength);
  340. m_fStream.Flush();
  341.  
  342. Application.DoEvents();
  343. }
  344.  
  345. }
  346. catch (Exception e)
  347. {
  348. Application.DoEvents();
  349. throw e;
  350. }
  351. finally
  352. {
  353. Application.DoEvents();
  354. if (m_pDataStream != IntPtr.Zero)
  355. {
  356. Application.DoEvents();
  357. //CloseHandle(m_pDataStream);
  358. m_pDataStream = IntPtr.Zero;
  359. }
  360. Application.DoEvents();
  361.  
  362. }
  363. return 0;
  364. }
  365.  
  366. public string GenerateBlackImage(string direction, string side)
  367. {
  368. throw new NotImplementedException();
  369. }
  370. }
  371. }
Advertisement
Add Comment
Please, Sign In to add comment