biz_property_scanner_to_engine.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.IO;
  7. using Saraff.Twain;
  8. using HXX.Scanner.Common;
  9. namespace HXX.Scanner.Biz
  10. {
  11. /// <summary>
  12. /// 老类,设置基础信息到扫描仪
  13. /// </summary>
  14. public class biz_property_scanner_to_engine
  15. {
  16. public static void Set(Twain32 engine, Device_Scanner scanner)
  17. {
  18. set_gray(engine);
  19. set_bitDepth(engine);
  20. set_file_mode(engine);
  21. set_file_type(engine);
  22. if (!scanner.deviceName.ToUpper().Contains("KODAK"))
  23. {
  24. set_removeBorder(engine);
  25. }
  26. //来自 界面设置 或 网端传参
  27. set_paper(engine, scanner);
  28. set_AB(engine);
  29. try { engine.Capabilities.Brightness.Set(scanner.ld); } catch (Exception ee) { }
  30. try { engine.Capabilities.Contrast.Set(scanner.dbd); } catch (Exception ee) { }
  31. engine.Capabilities.XResolution.Set(scanner.resolution);
  32. engine.Capabilities.YResolution.Set(scanner.resolution);
  33. //engine.Capabilities.XResolution.Set(200);
  34. //engine.Capabilities.YResolution.Set(200);
  35. engine.ShowUI = config_environment.use_source_ui;
  36. /////////////////test
  37. //this._twain32.CloseDataSource();
  38. //this._twain32.SourceIndex = index;
  39. //this._twain32.OpenDataSource();
  40. //this._twain32.Capabilities.XResolution.Set(200);
  41. //this._twain32.Capabilities.YResolution.Set(200);
  42. //this._twain32.Capabilities.PixelType.Set(TwPixelType.Gray);
  43. //this._twain32.Capabilities.XferMech.Set(TwSX.File);
  44. //this._twain32.Capabilities.ImageFileFormat.Set(TwFF.Tiff);
  45. //this._twain32.Capabilities.SupportedSizes.Reset();
  46. //this._twain32.Capabilities.SupportedSizes.Set(TwSS.A4);
  47. //this._twain32.Capabilities.XferCount.Set(2);
  48. //this._twain32.Capabilities.DuplexEnabled.Set(true);
  49. //this._twain32.Capabilities.Brightness.Set(0);
  50. //this._twain32.Capabilities.Contrast.Set(0);
  51. }
  52. /// <summary>
  53. /// 设置纸张格式
  54. /// </summary>
  55. /// <param name="engine"></param>
  56. /// <param name="scanner"></param>
  57. private static void set_paper(Twain32 engine, Device_Scanner scanner)
  58. {
  59. try
  60. {
  61. try
  62. {
  63. //佳能必须要先Reset,再Set才有效
  64. engine.Capabilities.SupportedSizes.Reset();
  65. }
  66. catch (Exception e)
  67. {
  68. LogManager.WriteLog(e);
  69. }
  70. var autoSizeCab = engine.Capabilities.AutoSize.ToString().ToLower();
  71. if (autoSizeCab.Contains("not"))
  72. {
  73. //var supported = engine.Capabilities.SupportedSizes.Get();
  74. if (config_environment.paper_size == PaperSize.A3) { engine.Capabilities.SupportedSizes.Set(TwSS.A3); }
  75. if (config_environment.paper_size == PaperSize.A4) { engine.Capabilities.SupportedSizes.Set(TwSS.A4); }
  76. if (config_environment.paper_size == PaperSize.K8) { engine.Capabilities.SupportedSizes.Set(TwSS.A3); }
  77. if (config_environment.paper_size == PaperSize.K16) { engine.Capabilities.SupportedSizes.Set(TwSS.A4); }
  78. }
  79. else
  80. {
  81. engine.SetCap(TwCap.AutoSize, true);
  82. }
  83. }
  84. catch (Exception ex)
  85. {
  86. //Console.WriteLine("SupportedSizes got exception");
  87. if (!ex.Message.Contains("Data parameter out of range"))
  88. {
  89. LogManager.WriteLog(ex);
  90. }
  91. }
  92. }
  93. /// <summary>
  94. /// 设置单双面
  95. /// </summary>
  96. /// <param name="engine"></param>
  97. private static void set_AB(Twain32 engine)
  98. {
  99. engine.Capabilities.FeederEnabled.Set(true);
  100. if (config_environment.scan_type == ScanType.Formal)
  101. {
  102. engine.Capabilities.XferCount.Set(-1);
  103. if (config_environment.scan_ab)
  104. {
  105. engine.Capabilities.DuplexEnabled.Set(true);
  106. }
  107. else
  108. {
  109. engine.Capabilities.DuplexEnabled.Set(false);
  110. }
  111. }
  112. else
  113. {
  114. if (config_environment.scan_ab)
  115. {
  116. engine.Capabilities.XferCount.Set(2);
  117. engine.Capabilities.DuplexEnabled.Set(true);
  118. }
  119. else
  120. {
  121. engine.Capabilities.XferCount.Set(1);
  122. engine.Capabilities.DuplexEnabled.Set(false);
  123. }
  124. }
  125. }
  126. ///// <summary>
  127. ///// 设置灰度
  128. ///// </summary>
  129. ///// <param name="engine"></param>
  130. //private static void set_gray(Twain32 engine)
  131. //{
  132. // Boolean hasBw = false;
  133. // Boolean hasGray = false;
  134. // Twain32.Enumeration _pixelTypes = engine.Capabilities.PixelType.Get();
  135. // for (int i = 0; i < _pixelTypes.Count; i++)
  136. // {
  137. // if (_pixelTypes[i].ToString().Equals("BW"))
  138. // {
  139. // hasBw = true;
  140. // }
  141. // if (_pixelTypes[i].ToString().Equals("Gray"))
  142. // {
  143. // hasGray = true;
  144. // }
  145. // }
  146. // //一般都支持这个模式
  147. // // test gray
  148. // if (false)
  149. // {
  150. // if (hasBw)
  151. // {
  152. // try
  153. // {
  154. // engine.Capabilities.PixelType.Set(TwPixelType.BW);
  155. // }
  156. // catch (Exception ee)
  157. // {
  158. // Console.WriteLine("Error set PixelType BW " + ee.Message);
  159. // }
  160. // }
  161. // else
  162. // {
  163. // Console.WriteLine("Error " + DateTime.Now.ToString() + " PixelType BW not supported");
  164. // }
  165. // }
  166. // //测试灰度
  167. // if (hasGray)
  168. // {
  169. // try
  170. // {
  171. // engine.Capabilities.PixelType.Set(TwPixelType.Gray);
  172. // }
  173. // catch (Exception ee)
  174. // {
  175. // Console.WriteLine("Error set PixelType Gray " + ee.Message);
  176. // }
  177. // }
  178. // else
  179. // {
  180. // Console.WriteLine("Error " + DateTime.Now.ToString() + " PixelType Gray not supported");
  181. // }
  182. //}
  183. /// <summary>
  184. /// 设置灰度
  185. /// </summary>
  186. /// <param name="engine"></param>
  187. private static void set_gray(Twain32 engine)
  188. {
  189. Boolean hasBw = false;
  190. Boolean hasGray = false;
  191. Boolean hasColor = false;
  192. Twain32.Enumeration _pixelTypes = engine.Capabilities.PixelType.Get();
  193. for (int i = 0; i < _pixelTypes.Count; i++)
  194. {
  195. if (_pixelTypes[i].ToString().Equals("BW"))
  196. {
  197. hasBw = true;
  198. }
  199. if (_pixelTypes[i].ToString().Equals("Gray"))
  200. {
  201. hasGray = true;
  202. }
  203. if (_pixelTypes[i].ToString().Equals("RGB"))
  204. {
  205. hasColor = true;
  206. }
  207. }
  208. if (config_environment.web_parameter.isColor == 0)
  209. {
  210. if (hasGray)
  211. {
  212. try
  213. {
  214. engine.Capabilities.PixelType.Set(TwPixelType.Gray);
  215. }
  216. catch (Exception ee)
  217. {
  218. LogManager.WriteLog("Error set PixelType GRAY " + ee.Message);
  219. }
  220. }
  221. }
  222. //else if (config_environment.web_parameter.paperColor.ToLower() == "black")
  223. //{
  224. // if (hasBw)
  225. // {
  226. // try
  227. // {
  228. // engine.Capabilities.PixelType.Set(TwPixelType.BW);
  229. // }
  230. // catch (Exception ee)
  231. // {
  232. // LogManager.WriteLog("Error set PixelType BW " + ee.Message);
  233. // }
  234. // }
  235. //}
  236. else if (config_environment.web_parameter.isColor == 1)
  237. {
  238. if (hasColor)
  239. {
  240. try
  241. {
  242. engine.Capabilities.PixelType.Set(TwPixelType.RGB);
  243. }
  244. catch (Exception ee)
  245. {
  246. LogManager.WriteLog("Error set PixelType RGB " + ee.Message);
  247. }
  248. }
  249. }
  250. }
  251. /// <summary>
  252. /// 设置色深
  253. /// </summary>
  254. /// <param name="engine"></param>
  255. private static void set_bitDepth(Twain32 engine)
  256. {
  257. if (false)
  258. {
  259. try
  260. {
  261. engine.Capabilities.BitDepth.Set(1);
  262. }
  263. catch (Exception ee)
  264. {
  265. Console.WriteLine(DateTime.Now.ToString() + " Error set BitDepth 1" + ee.Message);
  266. }
  267. }
  268. }
  269. /// <summary>
  270. /// 设置保存格式
  271. /// </summary>
  272. /// <param name="engine"></param>
  273. private static void set_file_mode(Twain32 engine)
  274. {
  275. try
  276. {
  277. engine.Capabilities.XferMech.Set(TwSX.File);
  278. }
  279. catch (Exception ee)
  280. {
  281. Console.WriteLine("Error set XferMech TwSX.File " + ee.Message);
  282. }
  283. }
  284. /// <summary>
  285. /// 设置文件格式
  286. /// </summary>
  287. /// <param name="engine"></param>
  288. private static void set_file_type(Twain32 engine)
  289. {
  290. //try
  291. //{
  292. // Twain32.Enumeration imageFileFormats = engine.Capabilities.ImageFileFormat.Get();
  293. // Boolean hasTiff = false;
  294. // for (int i = 0; i < imageFileFormats.Count; i++)
  295. // {
  296. // if (imageFileFormats[i].ToString().ToLower().Equals("tiff"))
  297. // {
  298. // hasTiff = true;
  299. // break;
  300. // }
  301. // }
  302. // if (hasTiff)
  303. // {
  304. // engine.Capabilities.ImageFileFormat.Set(TwFF.Tiff);
  305. // }
  306. //}
  307. //catch (Exception ee)
  308. //{
  309. //}
  310. //try
  311. //{
  312. // engine.Capabilities.ImageFileFormat.Set(TwFF.Jfif);
  313. // //engine.Capabilities.JpegPixelType.Set(TwPixelType.Gray);
  314. // //engine.Capabilities.JpegQuality.Set(TwJQ.High);
  315. // engine.Capabilities.Compression.Set(TwCompression.Jpeg2000);
  316. //}
  317. //catch (Exception ee)
  318. //{
  319. //}
  320. try
  321. {
  322. engine.Capabilities.ImageFileFormat.Set(TwFF.Bmp);
  323. }
  324. catch (Exception ee)
  325. {
  326. }
  327. }
  328. /// <summary>
  329. /// 不明设置
  330. /// </summary>
  331. /// <param name="engine"></param>
  332. private static void set_removeBorder(Twain32 engine)
  333. {
  334. try
  335. {
  336. Twain32.Enumeration bRemoveBorderCap = (Twain32.Enumeration)engine.GetCap((TwCap)TwCapExt.bRemoveBorder);
  337. bool hasRemoveBorder = false;
  338. for (int i = 0; i < bRemoveBorderCap.Count; i++)
  339. {
  340. if (bRemoveBorderCap[i].ToString().Equals("1"))
  341. {
  342. hasRemoveBorder = true;
  343. break;
  344. }
  345. }
  346. if (hasRemoveBorder)
  347. {
  348. engine.SetCap((TwCap)TwCapExt.bRemoveBorder, 1);
  349. }
  350. }
  351. catch (Exception ee)
  352. {
  353. Console.WriteLine(DateTime.Now.ToString() + " error set bRemoveBorder " + ee.Message);
  354. }
  355. }
  356. }
  357. }