CreateFromScannerDialogsViewModel.cs 9.8 KB

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