CreateFromScannerDialogsViewModel.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. private int _SeIndex=-1;
  102. public int SeIndex
  103. {
  104. get { return _SeIndex; }
  105. set => SetProperty(ref _SeIndex, value);
  106. }
  107. public MainContentViewModel mainContentViewModel;
  108. public DelegateCommand ToScanCommand { get; set; }
  109. public DelegateCommand CreateCommand { get; set; }
  110. public DelegateCommand<object> SelectedScannerCommand { get; set; }
  111. CreateFromScannerDialogsViewModel(IRegionManager regionManager, IDialogService dialogService)
  112. {
  113. ToScanCommand = new DelegateCommand(ToScan);
  114. ScanAdd();
  115. }
  116. public void ScanAdd()
  117. {
  118. DeviceManager deviceManager;
  119. deviceManager = new DeviceManager();
  120. for (int i = 1; i <= deviceManager.DeviceInfos.Count; i++)
  121. {
  122. if (deviceManager.DeviceInfos[i].Type != WiaDeviceType.ScannerDeviceType)
  123. {
  124. continue;
  125. }
  126. Scanitems.Add(deviceManager.DeviceInfos[i].Properties["Name"].get_Value());
  127. }
  128. if (Scanitems.Count == 0)
  129. {
  130. Gridvis = Visibility.Visible;
  131. }
  132. else
  133. {
  134. SeIndex = 0;
  135. }
  136. }
  137. public void ToScan()
  138. {
  139. if (SeIndex>-1)
  140. {
  141. imageFile = Scan();
  142. }
  143. }
  144. public ImageFile Scan()
  145. {
  146. ImageFile image = null;
  147. DeviceManager deviceManager;
  148. deviceManager = new DeviceManager();
  149. Device device;
  150. try
  151. {
  152. WIA.CommonDialog dialog = new WIA.CommonDialog();
  153. Items items;
  154. for (int i = 1, j = 0; i <= deviceManager.DeviceInfos.Count; i++)
  155. {
  156. if (deviceManager.DeviceInfos[i].Type != WiaDeviceType.ScannerDeviceType)
  157. {
  158. continue;
  159. }
  160. if (j == SeIndex)
  161. {
  162. device = deviceManager.DeviceInfos[i].Connect();
  163. items = dialog.ShowSelectItems(device, SingleSelect: true);
  164. if (items != null)
  165. {
  166. //image = (ImageFile)device.Items[1];
  167. image = dialog.ShowTransfer(device.Items[1]);
  168. ToImage(image);
  169. break;
  170. }
  171. }
  172. j++;
  173. }
  174. if (image != null)
  175. {
  176. }
  177. return image;
  178. }
  179. catch (COMException ex)
  180. {
  181. Gridvis = Visibility.Visible;
  182. return null;
  183. }
  184. }
  185. public void ToImage(ImageFile img)
  186. {
  187. if (img != null)
  188. {
  189. string path = App.CachePath.ScanFilePath;
  190. string FileName = path+ "\\"+ Guid.NewGuid().ToString() + ".bmp";
  191. img.SaveFile(FileName);
  192. string keyy = "Imagepath";
  193. DialogParameters keyValues = new DialogParameters();
  194. keyValues.Add(keyy, FileName);
  195. RaiseRequestClose(new Prism.Services.Dialogs.DialogResult(ButtonResult.OK, keyValues));
  196. App.CachePath.AddToDeleteFiles(path);
  197. }
  198. }
  199. private void JpegInsertPage(ref CPDFDocument topdfdoc, string filename, FileInfo fileinfo, int width, int height, int index = 0)
  200. {
  201. string tempFileName = "";
  202. if (fileinfo.Extension == ".jpg" || fileinfo.Extension == ".jpeg")
  203. {
  204. topdfdoc.InsertPage(index, width, height, filename);
  205. }
  206. else
  207. {
  208. tempFileName = Path.GetTempPath() + "pngtemp.jpg";
  209. if (!PictureConverter.SaveJpeg(filename, tempFileName))
  210. {
  211. MessageBoxEx.Show("图片格式有问题");
  212. }
  213. topdfdoc.InsertPage(index, width, height, tempFileName);
  214. }
  215. }
  216. public string savefilename(string filename, string filepath)
  217. {
  218. FileInfo file = new FileInfo(filename);
  219. return CommonHelper.CreateFilePath(filepath + file.Name.Replace(file.Extension, ".pdf") );
  220. }
  221. }
  222. }