Device_Scanner.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. using Saraff.Twain;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Drawing;
  5. using System.Drawing.Imaging;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Runtime.InteropServices;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Windows.Forms;
  12. using WIA;
  13. namespace HXX.Scanner.Biz
  14. {
  15. /// <summary>
  16. /// 扫描仪,根据老类修改而来,很多方法没用
  17. /// </summary>
  18. public class Device_Scanner
  19. {
  20. public Device_Scanner(DeviceInfo deviceInfo)
  21. {
  22. this._deviceInfo = deviceInfo;
  23. this.manufacturer = (string)deviceInfo.Properties["Manufacturer"].get_Value();
  24. }
  25. public Device_Scanner(string deviceName)
  26. {
  27. this.isTwain = true;
  28. this.deviceName = deviceName;
  29. if (deviceName.ToLower().Contains("canon"))
  30. {
  31. this.manufacturer = "CANON";
  32. }
  33. else if (deviceName.ToLower().Contains("panasonic"))
  34. {
  35. this.manufacturer = "Panasonic";
  36. }
  37. else if (deviceName.ToLower().Contains("kodak"))
  38. {
  39. this.manufacturer = "Kodak";
  40. }
  41. else if (deviceName.ToLower().Contains("epson"))
  42. {
  43. this.manufacturer = "Epson";
  44. }
  45. else if (deviceName.ToLower().Contains("hp"))
  46. {
  47. this.manufacturer = "HP";
  48. }
  49. else if (deviceName.ToLower().Contains("huago"))
  50. {
  51. this.manufacturer = "HUAGO";
  52. }
  53. else
  54. {
  55. this.manufacturer = "";
  56. }
  57. }
  58. /// <summary>
  59. /// 是否被激活
  60. /// </summary>
  61. public int isActive = 0;
  62. /// <summary>
  63. /// 是否支持tw,废弃
  64. /// </summary>
  65. public Boolean isTwain = false;
  66. /// <summary>
  67. /// 名称
  68. /// </summary>
  69. public string deviceName;
  70. /// <summary>
  71. /// 制造商
  72. /// </summary>
  73. public string manufacturer;
  74. /// <summary>
  75. /// 分辨率
  76. /// </summary>
  77. public int resolution = 200;
  78. /// <summary>
  79. /// 亮度
  80. /// </summary>
  81. public int ld = 0;
  82. /// <summary>
  83. /// 对比度
  84. /// </summary>
  85. public int dbd = 0;
  86. /// <summary>
  87. /// 纸张规格
  88. /// </summary>
  89. public PaperSize zz = PaperSize.A3;
  90. /// <summary>
  91. /// wia使用,废弃
  92. /// </summary>
  93. public readonly DeviceInfo _deviceInfo;
  94. /// <summary>
  95. /// 废弃
  96. /// </summary>
  97. public int width_pixel = (int)Math.Ceiling(200 * 297 / 25.4); //默认A3
  98. /// <summary>
  99. /// 废弃
  100. /// </summary>
  101. public int height_pixel = (int)Math.Ceiling(200 * 420 / 25.4);
  102. /// <summary>
  103. /// 废弃
  104. /// </summary>
  105. public int colorMode = 65536 * 2;
  106. /// <summary>
  107. /// 废弃
  108. /// </summary>
  109. //这个参数暂时没用,WIA.WiaImageIntent.GrayscaleIntent.GetHashCode();//1; //
  110. public Boolean duplex = true;
  111. /// <summary>
  112. /// 下标
  113. /// </summary>
  114. public int dsIndex { get; set; }
  115. /// <summary>
  116. /// 废弃
  117. /// </summary>
  118. public Twain32.Enumeration Brightness;
  119. /// <summary>
  120. /// 废弃
  121. /// </summary>
  122. public int BrightnessBaseIndex = 0;
  123. /// <summary>
  124. /// 废弃
  125. /// </summary>
  126. public Twain32.Enumeration Contrasts;
  127. /// <summary>
  128. /// 废弃
  129. /// </summary>
  130. public int ContrastsBaseIndex = 0;
  131. /// <summary>
  132. /// 废弃
  133. /// </summary>
  134. public bool openSourceReady = false;
  135. /// <summary>
  136. /// 废弃
  137. /// </summary>
  138. public bool isChoose = false;
  139. /// <summary>
  140. /// 设置纸张,会顺便设置宽高
  141. /// </summary>
  142. /// <param name="zz"></param>
  143. public void setPaperSize(PaperSize zz)
  144. {
  145. this.zz = zz;
  146. int wMM = 210;
  147. int hMM = 297;
  148. if (this.zz == PaperSize.A4) { wMM = 210; hMM = 297; }
  149. else if (this.zz == PaperSize.A3) { wMM = 297; hMM = 420; }
  150. else if (this.zz == PaperSize.K16) { wMM = 195; hMM = 270; }
  151. else if (this.zz == PaperSize.K8) { wMM = 270; hMM = 390; }
  152. this.width_pixel = (int)Math.Ceiling(this.resolution * wMM / 25.4);
  153. this.height_pixel = (int)Math.Ceiling(this.resolution * hMM / 25.4);
  154. }
  155. /// <summary>
  156. /// 压缩,约65倍
  157. /// </summary>
  158. /// <param name="src"></param>
  159. /// <param name="dst"></param>
  160. /// <param name="delSrc"></param>
  161. public static void zipImage(string src, string dst, bool delSrc)
  162. {
  163. EncoderParameters eps = new EncoderParameters(1);
  164. //黑白压缩
  165. //eps.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Compression, (long)EncoderValue.CompressionCCITT4);
  166. //灰度压缩
  167. eps.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Compression, (long)EncoderValue.CompressionLZW);
  168. Bitmap bi = new Bitmap(src);
  169. ImageCodecInfo ici = GetEncoderInfo("image/tiff");
  170. bi.Save(dst, ici, eps);
  171. bi.Dispose();
  172. bi = null;
  173. if (delSrc)
  174. {
  175. File.Delete(src);
  176. }
  177. }
  178. /// <summary>
  179. /// 压缩,约65倍
  180. /// </summary>
  181. /// <param name="src"></param>
  182. /// <param name="dst"></param>
  183. /// <param name="delSrc"></param>
  184. public static void zipImage_jpeg(string src, string dst, bool delSrc)
  185. {
  186. EncoderParameters eps = new EncoderParameters(1);
  187. //黑白压缩
  188. //eps.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Compression, (long)EncoderValue.CompressionCCITT4);
  189. //灰度压缩
  190. eps.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Compression, (long)EncoderValue.CompressionLZW);
  191. Bitmap bi = new Bitmap(src);
  192. ImageCodecInfo ici = GetEncoderInfo("image/jpeg");
  193. bi.Save(dst, ici, eps);
  194. bi.Dispose();
  195. bi = null;
  196. if (delSrc)
  197. {
  198. File.Delete(src);
  199. }
  200. }
  201. public ImageFile ScanTIFFTest(string dir, string test)
  202. {
  203. var device = this._deviceInfo.Connect();
  204. //device.Propertiesf
  205. SetWIAProperty(device.Properties, WiaPropertyConstant.DocumentHandlingSelect, 5);
  206. SetWIAProperty(device.Properties, WiaPropertyConstant.Pages, 1);
  207. //if (duplex)
  208. //{
  209. // device.Properties["Document Handling Select"].set_Value(37);
  210. // //device.Properties["Pages"].set_Value(1);
  211. //}
  212. //else
  213. //{
  214. // device.Properties["Document Handling Select"].set_Value(1); //默认
  215. // //device.Properties["Pages"].set_Value(1); //默认
  216. //}
  217. CommonDialogClass dlg = new CommonDialogClass();
  218. //新扫描界面
  219. //dlg.ShowAcquisitionWizard(device);
  220. //你想扫描什么界面
  221. //ImageFile imageF = dlg.ShowAcquireImage(WiaDeviceType.ScannerDeviceType, WiaImageIntent.UnspecifiedIntent, WiaImageBias.MaximizeQuality, WIA.FormatID.wiaFormatTIFF, false, true) as ImageFile;
  222. //显示设备选择界面
  223. //var deviceTemp = dlg.ShowSelectDevice(WiaDeviceType.ScannerDeviceType, false, true);
  224. //你想扫描什么界面
  225. //Items items = dlg.ShowSelectItems(device, WiaImageIntent.UnspecifiedIntent, WiaImageBias.MaximizeQuality, true, true, true);
  226. //显示设备属性
  227. //dlg.ShowDeviceProperties(device, true);
  228. //var item = device.Items[1];//index是从1开始的,而不是从0
  229. //显示扫描属性:亮度、对比度、DPI、颜色模式
  230. //dlg.ShowItemProperties(item, true);
  231. //显示完成百分比
  232. //var trans = dlg.ShowTransfer(item, WIA.FormatID.wiaFormatTIFF, true);
  233. //Items items = dlg.ShowSelectItems(device, WiaImageIntent.UnspecifiedIntent, WiaImageBias.MaximizeQuality, false, true, false);
  234. //MessageBox.Show(items.Count.ToString());
  235. //foreach (Item it in items)
  236. //{
  237. // AdjustScannerSettings(it, resolution, 0, 0, width_pixel, height_pixel, ld, dbd, colorMode);
  238. // CommonDialogClass dlgTemp = new CommonDialogClass();
  239. // ImageFile imageFile = dlgTemp.ShowTransfer(
  240. // it,
  241. // WIA.FormatID.wiaFormatTIFF,
  242. // true)
  243. // as ImageFile;
  244. // if (imageFile != null)
  245. // {
  246. // string tmpFile = dir + "\\_tmp" + DateTime.Now.ToString("MMddHHmmssfff") + ".tif";
  247. // string testFileName2 = dir + "\\测试" + DateTime.Now.ToString("MMddHHmmssfff") + "_A.tif";
  248. // imageFile.SaveFile(tmpFile);
  249. // zipImage(tmpFile, testFileName2, true);
  250. // }
  251. //}
  252. var item = device.Items[1];//index是从1开始的,而不是从0
  253. try
  254. {
  255. AdjustScannerSettings(item, resolution, 0, 0, width_pixel, height_pixel, ld, dbd, colorMode);
  256. if (true)
  257. {
  258. //第一面
  259. var imageFile = dlg.ShowTransfer(item, WIA.FormatID.wiaFormatTIFF, true) as ImageFile;
  260. if (imageFile != null)
  261. {
  262. string tmpFile = dir + "\\_tmp" + DateTime.Now.ToString("MMddHHmmssfff") + ".tif";
  263. string testFileName2 = dir + "\\测试" + DateTime.Now.ToString("MMddHHmmssfff") + "_A.tif";
  264. imageFile.SaveFile(tmpFile);
  265. zipImage(tmpFile, testFileName2, false);
  266. }
  267. }
  268. if (duplex)
  269. {
  270. //第二面
  271. ImageFile imageFile2 = dlg.ShowTransfer(item, WIA.FormatID.wiaFormatTIFF, true) as ImageFile;
  272. if (imageFile2 != null)
  273. {
  274. string tmpFile = dir + "\\_tmp" + DateTime.Now.ToString("MMddHHmmssfff") + ".tif";
  275. string testFileName2 = dir + "\\测试" + DateTime.Now.ToString("MMddHHmmssfff") + "_B.tif";
  276. imageFile2.SaveFile(tmpFile);
  277. zipImage(tmpFile, testFileName2, false);
  278. }
  279. }
  280. //打开扫描文件夹
  281. System.Diagnostics.Process.Start("explorer.exe", dir);
  282. return null;//return imageFile;
  283. }
  284. catch (COMException e)
  285. {
  286. Console.WriteLine(e.ToString());
  287. Console.WriteLine(e.ErrorCode);
  288. Console.WriteLine(e.Message);
  289. uint errorCode = (uint)e.ErrorCode;
  290. if (errorCode == 0x80210006)
  291. {
  292. MessageBox.Show("扫描仪状态错误");
  293. }
  294. else if (errorCode == 0x80210064)
  295. {
  296. MessageBox.Show("取消扫描");
  297. }
  298. else if (errorCode == 0x80210003)
  299. {
  300. MessageBox.Show("请放入要扫描的纸张");
  301. }
  302. else
  303. {
  304. //MessageBox.Show("未知错误", "Error", MessageBoxButtons.OK);
  305. throw e;
  306. }
  307. }
  308. return new ImageFile();
  309. }
  310. private static ImageCodecInfo GetEncoderInfo(string mimeType)
  311. {
  312. ImageCodecInfo[] encoders = ImageCodecInfo.GetImageEncoders();
  313. for (int j = 0; j < encoders.Length; j++)
  314. {
  315. if (encoders[j].MimeType == mimeType)
  316. return encoders[j];
  317. }
  318. throw new Exception(mimeType + " mime type not found in ImageCodecInfo");
  319. }
  320. private static void showPropValues(IProperties ps)
  321. {
  322. foreach (Property p in ps)
  323. {
  324. Console.WriteLine(p.Type.ToString() + ">" + p.SubType.ToString() + ">"
  325. + p.PropertyID + " > " + p.Name + " > " + p.get_Value());
  326. }
  327. }
  328. private static void AdjustScannerSettings(IItem scannnerItem, int scanResolutionDPI, int scanStartLeftPixel,
  329. int scanStartTopPixel, int scanWidthPixels, int scanHeightPixels,
  330. int brightnessPercents, int contrastPercents, int colorMode)
  331. {
  332. IProperties ps = scannnerItem.Properties;
  333. //showPropValues(ps);
  334. SetWIAProperty(ps, WiaPropertyConstant.HorizontalResolution, scanResolutionDPI);
  335. SetWIAProperty(ps, WiaPropertyConstant.VerticalResolution, scanResolutionDPI);
  336. //(最大-页宽)/2
  337. object propNameStr = "" + WiaPropertyConstant.HorizontalExtent;
  338. Property hExt = ps.get_Item(ref propNameStr);
  339. scanStartLeftPixel = (int)Decimal.Round(((int)hExt.get_Value() - scanWidthPixels) / 2);
  340. SetWIAProperty(ps, WiaPropertyConstant.HorizontalStartPostion, scanStartLeftPixel);
  341. SetWIAProperty(ps, WiaPropertyConstant.VerticalStartPosition, scanStartTopPixel);
  342. SetWIAProperty(ps, WiaPropertyConstant.HorizontalExtent, scanWidthPixels);
  343. SetWIAProperty(ps, WiaPropertyConstant.VerticalExtent, scanHeightPixels);
  344. SetWIAProperty(ps, WiaPropertyConstant.Brightness, brightnessPercents);
  345. SetWIAProperty(ps, WiaPropertyConstant.Contrast, contrastPercents);
  346. SetWIAProperty(ps, WiaPropertyConstant.BitsPerPixel, 1); //默认24,8,1
  347. //SetWIAProperty(ps, WiaPropertyConstant.DocumentHandlingSelect, 5);
  348. //SetWIAProperty(ps, WiaPropertyConstant.Pages, 2);
  349. SetWIAProperty(ps, WiaPropertyConstant.CurrentIntent, colorMode);
  350. //showPropValues(ps);
  351. }
  352. private static void SetWIAProperty(IProperties properties, int propName, object propValue)
  353. {
  354. try
  355. {
  356. object propNameStr = "" + propName;
  357. Property prop = properties.get_Item(ref propNameStr);
  358. if (prop != null)
  359. {
  360. //Console.WriteLine(prop.get_Value().ToString());
  361. prop.set_Value(ref propValue);
  362. }
  363. else
  364. {
  365. Console.WriteLine("prop null" + propNameStr);
  366. }
  367. }
  368. catch (Exception e)
  369. {
  370. Console.WriteLine(propName + " > " + e);
  371. }
  372. }
  373. public override string ToString()
  374. {
  375. if (deviceName != null && deviceName.Length > 0)
  376. {
  377. return deviceName;
  378. }
  379. if (this._deviceInfo != null)
  380. {
  381. return (string)this._deviceInfo.Properties["Name"].get_Value();
  382. }
  383. return "Null";
  384. }
  385. }
  386. }