CameraShotCls.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. using Common;
  2. using MeterPlugInLibrary;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Drawing;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. namespace CarMeterSystem.OptionCls
  11. {
  12. /// <summary>
  13. /// 截图调用类
  14. /// </summary>
  15. public class CameraShotCls
  16. {
  17. private Log lg = Log.GetInstance(); //写日志
  18. public void Connection()
  19. {
  20. try
  21. {
  22. foreach (DhCameraShot shot in CarCache.cameraShots)
  23. {
  24. shot.Connection();
  25. }
  26. }
  27. catch (Exception ex)
  28. {
  29. lg.WriteLog(39, ex.Message);
  30. }
  31. }
  32. public void Close()
  33. {
  34. try
  35. {
  36. foreach (DhCameraShot shot in CarCache.cameraShots)
  37. {
  38. shot.Close();
  39. }
  40. }
  41. catch (Exception ex)
  42. {
  43. lg.WriteLog(39, ex.Message);
  44. }
  45. }
  46. /// <summary>
  47. /// 截图
  48. /// </summary>
  49. /// <param name="actualFirstNo">作业编号</param>
  50. public void CapPic(string actualFirstNo)
  51. {
  52. try
  53. {
  54. int iCnt = 1; //抓拍摄像头配置顺序必须是前面2个
  55. foreach (DhCameraShot shot in CarCache.cameraShots)
  56. {
  57. //放在tempImg文件夹下面就进行图片的压缩操作,否则不进行图片压缩
  58. //shot.CapPic(iCnt < 3 ? 0 : 1, PbCache.path + string.Format("\\imgShort\\tempImg\\{0}_{1}_{2}.jpg", PbCache.sportInfo.baseSpotNo, "C" + actualFirstNo, iCnt));
  59. shot.CapPic(iCnt < 2 ? 0 : 1, PbCache.path + string.Format("\\imgShort\\formalImg\\{0}_{1}_{2}.jpg", PbCache.sportInfo.baseSpotNo, "C" + actualFirstNo, iCnt));
  60. ZipFiles(PbCache.path + string.Format("\\imgShort\\formalImg\\{0}_{1}_{2}.jpg", PbCache.sportInfo.baseSpotNo, "C" + actualFirstNo, iCnt));
  61. // 第一次抓拍为仪表重量
  62. //if(iCnt == 1)
  63. //{
  64. // Log.GetInstance().WriteLog(35, "完成仪表拍照 成功标识:" + PbCache.shotSuccess.ToString() + "车号:" + PbCache.lockCarNo + ";重量:" + PbCache.lockWgt + ";时间:" + DateTime.Now.ToLongTimeString());
  65. //}
  66. iCnt++;
  67. }
  68. }
  69. catch (Exception ex)
  70. {
  71. lg.WriteLog(39,ex.Message);
  72. }
  73. }
  74. private void ZipFiles(String fn)
  75. {
  76. ImageZip iz = new ImageZip();
  77. if (string.IsNullOrEmpty(fn)) return;
  78. if (!File.Exists(fn)) return;
  79. //临时目录图片压缩后存储与正式目录
  80. string jpgFile = fn.Replace("tempImg", "temp");
  81. Image img = ImageZip.BytesToBitmap(ImageZip.ZipImageByte(fn));
  82. if (img != null)
  83. {
  84. //压缩图片并保存
  85. if(img.Width > AppConfigCache.imgWidth && img.Height > AppConfigCache.imgHeight)
  86. {
  87. img.Save(jpgFile);
  88. // BourneCao 20221220 使用以下方法继续压缩,比原图还要大
  89. //iz.ResourceImage = img;
  90. //iz.GetReducedImage(AppConfigCache.imgWidth, AppConfigCache.imgHeight).Save(jpgFile);
  91. }
  92. else
  93. {
  94. img.Save(jpgFile);
  95. }
  96. File.Delete(fn);//删除临时目录数据
  97. }
  98. //else
  99. //{
  100. // iz.ResourceImage = img;
  101. // //压缩图片并保存
  102. // iz.GetReducedImage(400, 400).Save(jpgFile);
  103. // //File.Move(fn, jpgFile);
  104. //}
  105. }
  106. /// <summary>
  107. /// 从硬盘录像机 进行截图
  108. /// </summary>
  109. /// <param name="actualFirstNo"></param>
  110. public void CapPicFromVideo(string actualFirstNo)
  111. {
  112. try
  113. {
  114. foreach (DhCameraShot shot in CarCache.cameraShots)
  115. {
  116. //放在tempImg文件夹下面就进行图片的压缩操作,否则不进行图片压缩
  117. //shot.CapPic(iCnt < 3 ? 0 : 1, PbCache.path + string.Format("\\imgShort\\tempImg\\{0}_{1}_{2}.jpg", PbCache.sportInfo.baseSpotNo, "C" + actualFirstNo, iCnt));
  118. bool shotSuccess = shot.CapPicFromVedio(PbCache.path + string.Format("\\imgShort\\formalImg\\{0}_{1}_", PbCache.sportInfo.baseSpotNo, "C" + actualFirstNo));
  119. PbCache.shotSuccess = shotSuccess;
  120. }
  121. }
  122. catch (Exception ex)
  123. {
  124. lg.WriteLog(39, ex.Message);
  125. }
  126. }
  127. /// <summary>
  128. /// 连接视频截图并关闭
  129. /// </summary>
  130. /// <param name="actualFirstNo">作业编号</param>
  131. public void CapMethod(string actualFirstNo)
  132. {
  133. Connection();
  134. CapPic(actualFirstNo);
  135. //CapPicFromVideo(actualFirstNo);
  136. Close();
  137. }
  138. }
  139. }