CreateFromScannerDialogsViewModel.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. using ComPDFKit.PDFDocument;
  2. using NTwain;
  3. using NTwain.Data;
  4. using PDF_Master.CustomControl;
  5. using PDF_Master.Helper;
  6. using PDF_Master.Model;
  7. using Prism.Commands;
  8. using Prism.Mvvm;
  9. using Prism.Regions;
  10. using Prism.Services.Dialogs;
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Diagnostics;
  14. using System.IO;
  15. using System.Linq;
  16. using System.Reflection;
  17. using System.Runtime.InteropServices;
  18. using System.Text;
  19. using System.Threading.Tasks;
  20. using System.Windows;
  21. using System.Windows.Controls;
  22. using System.Windows.Forms;
  23. using System.Windows.Interop;
  24. using System.Windows.Media;
  25. using System.Windows.Media.Imaging;
  26. using WIA;
  27. namespace PDF_Master.ViewModels.Dialog.HomePageToolsDialogs
  28. {
  29. //扫描仪设备
  30. public class ScannerItem : BindableBase
  31. {
  32. public DataSource DataSourceItem { get; set; }
  33. public ScannerItem(DataSource dataSource)
  34. {
  35. DataSourceItem = dataSource;
  36. ScannerName = dataSource.Name;
  37. }
  38. public string ScannerName { get; set; }
  39. }
  40. public class CreateFromScannerDialogsViewModel : BindableBase, IDialogAware
  41. {
  42. public static ImageFile imageFile;
  43. public string Title => "";
  44. //图片路径
  45. public string FilePath { get; private set; }
  46. public event Action<IDialogResult> RequestClose;
  47. public virtual void RaiseRequestClose(IDialogResult dialogResult)
  48. {
  49. RequestClose?.Invoke(dialogResult);
  50. }
  51. private List<ScannerItem> _scanners = new List<ScannerItem>();
  52. public List<ScannerItem> Scanners
  53. {
  54. get { return _scanners; }
  55. set => SetProperty(ref _scanners, value);
  56. }
  57. public List<string> _ScanItems =new List<string>();
  58. //图片路径
  59. public List<string> Scanitems
  60. {
  61. get { return _ScanItems; }
  62. set => SetProperty(ref _ScanItems, value);
  63. }
  64. private bool _IsEnable = true;
  65. public bool IsEnable
  66. {
  67. get { return _IsEnable; }
  68. set { SetProperty(ref _IsEnable, value);
  69. }
  70. }
  71. /// <summary>
  72. /// 没有扫描仪显示图
  73. /// </summary>
  74. private Visibility _Gridvis=Visibility.Collapsed;
  75. public Visibility Gridvis
  76. {
  77. get { return _Gridvis; }
  78. set { SetProperty(ref _Gridvis, value);
  79. if (Gridvis == Visibility.Visible)
  80. {
  81. IsEnable = false;
  82. }
  83. else
  84. {
  85. IsEnable = true;
  86. }
  87. }
  88. }
  89. #region 框架
  90. public bool CanCloseDialog()
  91. {
  92. return true;
  93. }
  94. public void OnDialogClosed()
  95. {
  96. }
  97. public void OnDialogOpened(IDialogParameters parameters)
  98. {
  99. }
  100. #endregion
  101. #region 文案
  102. private string _TextCreate;
  103. public string TextCreate
  104. {
  105. get { return _TextCreate; }
  106. set
  107. {
  108. SetProperty(ref _TextCreate, value);
  109. }
  110. }
  111. private string _BtnScan;
  112. public string BtnScan
  113. {
  114. get { return _BtnScan; }
  115. set
  116. {
  117. SetProperty(ref _BtnScan, value);
  118. }
  119. }
  120. private string _BtnCancle;
  121. public string BtnCancle
  122. {
  123. get { return _BtnCancle; }
  124. set
  125. {
  126. SetProperty(ref _BtnCancle, value);
  127. }
  128. }
  129. private string _TextScans;
  130. public string TextScans
  131. {
  132. get { return _TextScans; }
  133. set
  134. {
  135. SetProperty(ref _TextScans, value);
  136. }
  137. }
  138. private string _TextNoScan;
  139. public string TextNoScan
  140. {
  141. get { return _TextNoScan; }
  142. set
  143. {
  144. SetProperty(ref _TextNoScan, value);
  145. }
  146. }
  147. private void InitString()
  148. {
  149. TextCreate = App.HomePageLoader.GetString("TextCreate");
  150. BtnScan = App.HomePageLoader.GetString("BtnScan");
  151. BtnCancle = App.HomePageLoader.GetString("BtnCancle");
  152. TextScans = App.HomePageLoader.GetString("TextScans");
  153. TextNoScan = App.HomePageLoader.GetString("TextNoScan");
  154. }
  155. #endregion
  156. private int _SeIndex=-1;
  157. public int SeIndex
  158. {
  159. get { return _SeIndex; }
  160. set => SetProperty(ref _SeIndex, value);
  161. }
  162. public MainContentViewModel mainContentViewModel;
  163. public DelegateCommand ToScanCommand { get; set; }
  164. public DelegateCommand CreateCommand { get; set; }
  165. public DelegateCommand<object> SelectedScannerCommand { get; set; }
  166. CreateFromScannerDialogsViewModel(IRegionManager regionManager, IDialogService dialogService)
  167. {
  168. ToScanCommand = new DelegateCommand(ToScan);
  169. ScanAdd();
  170. InitString();
  171. }
  172. public void ScanAdd()
  173. {
  174. DeviceManager deviceManager;
  175. deviceManager = new DeviceManager();
  176. for (int i = 1; i <= deviceManager.DeviceInfos.Count; i++)
  177. {
  178. if (deviceManager.DeviceInfos[i].Type != WiaDeviceType.ScannerDeviceType)
  179. {
  180. continue;
  181. }
  182. Scanitems.Add(deviceManager.DeviceInfos[i].Properties["Name"].get_Value());
  183. }
  184. if (Scanitems.Count == 0)
  185. {
  186. Gridvis = Visibility.Visible;
  187. }
  188. else
  189. {
  190. SeIndex = 0;
  191. }
  192. }
  193. public void ToScan()
  194. {
  195. if (SeIndex>-1)
  196. {
  197. imageFile = Scan();
  198. }
  199. }
  200. public ImageFile Scan()
  201. {
  202. ImageFile image = null;
  203. DeviceManager deviceManager;
  204. deviceManager = new DeviceManager();
  205. Device device;
  206. try
  207. {
  208. WIA.CommonDialog dialog = new WIA.CommonDialog();
  209. Items items;
  210. for (int i = 1, j = 0; i <= deviceManager.DeviceInfos.Count; i++)
  211. {
  212. if (deviceManager.DeviceInfos[i].Type != WiaDeviceType.ScannerDeviceType)
  213. {
  214. continue;
  215. }
  216. if (j == SeIndex)
  217. {
  218. device = deviceManager.DeviceInfos[i].Connect();
  219. items = dialog.ShowSelectItems(device, SingleSelect: true);
  220. if (items != null)
  221. {
  222. //image = (ImageFile)device.Items[1];
  223. image = dialog.ShowTransfer(device.Items[1]);
  224. ToImage(image);
  225. break;
  226. }
  227. }
  228. j++;
  229. }
  230. if (image != null)
  231. {
  232. }
  233. return image;
  234. }
  235. catch (COMException ex)
  236. {
  237. Gridvis = Visibility.Visible;
  238. return null;
  239. }
  240. }
  241. public void ToImage(ImageFile img)
  242. {
  243. if (img != null)
  244. {
  245. string path = App.CachePath.ScanFilePath;
  246. string FileName = path+ "\\"+ Guid.NewGuid().ToString() + ".bmp";
  247. img.SaveFile(FileName);
  248. string keyy = "Imagepath";
  249. DialogParameters keyValues = new DialogParameters();
  250. keyValues.Add(keyy, FileName);
  251. RaiseRequestClose(new Prism.Services.Dialogs.DialogResult(ButtonResult.OK, keyValues));
  252. App.CachePath.AddToDeleteFiles(path);
  253. }
  254. }
  255. private void JpegInsertPage(ref CPDFDocument topdfdoc, string filename, FileInfo fileinfo, int width, int height, int index = 0)
  256. {
  257. string tempFileName = "";
  258. if (fileinfo.Extension == ".jpg" || fileinfo.Extension == ".jpeg")
  259. {
  260. topdfdoc.InsertPage(index, width, height, filename);
  261. }
  262. else
  263. {
  264. tempFileName = Path.GetTempPath() + "pngtemp.jpg";
  265. if (!PictureConverter.SaveJpeg(filename, tempFileName))
  266. {
  267. MessageBoxEx.Show("图片格式有问题");
  268. }
  269. topdfdoc.InsertPage(index, width, height, tempFileName);
  270. }
  271. }
  272. public string savefilename(string filename, string filepath)
  273. {
  274. FileInfo file = new FileInfo(filename);
  275. return CommonHelper.CreateFilePath(filepath + file.Name.Replace(file.Extension, ".pdf") );
  276. }
  277. }
  278. }