123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358 |
- using ComPDFKit.PDFDocument;
- using PDF_Master.CustomControl;
- using PDF_Master.Helper;
- using PDF_Master.Model;
- using Prism.Commands;
- using Prism.Mvvm;
- using Prism.Regions;
- using Prism.Services.Dialogs;
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.IO;
- using System.Linq;
- using System.Reflection;
- using System.Runtime.InteropServices;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Forms;
- using System.Windows.Interop;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using WIA;
- namespace PDF_Master.ViewModels.Dialog.HomePageToolsDialogs
- {
- //扫描仪设备
- public class ScannerItem
- {
- public Item DataSourceItem { get; set; }
- public string ScannerName { get; set; }
- public ScannerItem(Item dataSource)
- {
- DataSourceItem = dataSource;
- ScannerName = dataSource.Properties["Name"].ToString();
- }
- }
- public class CreateFromScannerDialogsViewModel : BindableBase, IDialogAware
- {
- public static ImageFile imageFile;
- public string Title => "";
- //图片路径
- public string FilePath { get; private set; }
- public event Action<IDialogResult> RequestClose;
- public virtual void RaiseRequestClose(IDialogResult dialogResult)
- {
- RequestClose?.Invoke(dialogResult);
- }
-
- private List<ScannerItem> _scanners = new List<ScannerItem>();
- public List<ScannerItem> Scanners
- {
- get { return _scanners; }
- set => SetProperty(ref _scanners, value);
- }
- public List<string> _ScanItems =new List<string>();
- //图片路径
- public List<string> Scanitems
- {
- get { return _ScanItems; }
- set => SetProperty(ref _ScanItems, value);
- }
- private bool _IsEnable = true;
- public bool IsEnable
- {
- get { return _IsEnable; }
- set { SetProperty(ref _IsEnable, value);
-
- }
- }
- /// <summary>
- /// 没有扫描仪显示图
- /// </summary>
- private Visibility _Gridvis=Visibility.Collapsed;
- public Visibility Gridvis
- {
- get { return _Gridvis; }
- set { SetProperty(ref _Gridvis, value);
- if (Gridvis == Visibility.Visible)
- {
- IsEnable = false;
- }
- else
- {
- IsEnable = true;
- }
- }
- }
- #region 框架
- public bool CanCloseDialog()
- {
- return true;
- }
- public void OnDialogClosed()
- {
- }
- public void OnDialogOpened(IDialogParameters parameters)
- {
- }
- #endregion
- #region 文案
- private string _TextCreate;
- public string TextCreate
- {
- get { return _TextCreate; }
- set
- {
- SetProperty(ref _TextCreate, value);
- }
- }
- private string _BtnScan;
- public string BtnScan
- {
- get { return _BtnScan; }
- set
- {
- SetProperty(ref _BtnScan, value);
- }
- }
- private string _BtnCancle;
- public string BtnCancle
- {
- get { return _BtnCancle; }
- set
- {
- SetProperty(ref _BtnCancle, value);
- }
- }
- private string _TextScans;
- public string TextScans
- {
- get { return _TextScans; }
- set
- {
- SetProperty(ref _TextScans, value);
- }
- }
- private string _TextNoScan;
- public string TextNoScan
- {
- get { return _TextNoScan; }
- set
- {
- SetProperty(ref _TextNoScan, value);
- }
- }
- private void InitString()
- {
- TextCreate = App.HomePageLoader.GetString("TextCreate");
- BtnScan = App.HomePageLoader.GetString("BtnScan");
- BtnCancle = App.HomePageLoader.GetString("BtnCancle");
- TextScans = App.HomePageLoader.GetString("TextScans");
- TextNoScan = App.HomePageLoader.GetString("TextNoScan");
- }
- #endregion
- private int _SeIndex=-1;
- public int SeIndex
- {
- get { return _SeIndex; }
- set => SetProperty(ref _SeIndex, value);
- }
- public MainContentViewModel mainContentViewModel;
- public DelegateCommand ToScanCommand { get; set; }
- public DelegateCommand CreateCommand { get; set; }
- public DelegateCommand<object> SelectedScannerCommand { get; set; }
- CreateFromScannerDialogsViewModel(IRegionManager regionManager, IDialogService dialogService)
- {
- ToScanCommand = new DelegateCommand(ToScan);
- ScanAdd();
- InitString();
- }
- public void ScanAdd()
- {
- DeviceManager deviceManager;
- deviceManager = new DeviceManager();
- for (int i = 1; i <= deviceManager.DeviceInfos.Count; i++)
- {
- if (deviceManager.DeviceInfos[i].Type != WiaDeviceType.ScannerDeviceType)
- {
- continue;
- }
- Scanitems.Add(deviceManager.DeviceInfos[i].Properties["Name"].get_Value());
- }
- if (Scanitems.Count == 0)
- {
- Gridvis = Visibility.Visible;
- }
- else
- {
- SeIndex = 0;
- }
- }
- public void ToScan()
- {
-
- if (SeIndex>-1)
- {
- imageFile = Scan();
- }
-
- }
- public ImageFile Scan()
- {
- ImageFile image = null;
- DeviceManager deviceManager;
- deviceManager = new DeviceManager();
- Device device;
- try
- {
- WIA.CommonDialog dialog = new WIA.CommonDialog();
- Items items;
- for (int i = 1, j = 0; i <= deviceManager.DeviceInfos.Count; i++)
- {
- if (deviceManager.DeviceInfos[i].Type != WiaDeviceType.ScannerDeviceType)
- {
- continue;
- }
- if (j == SeIndex)
- {
- //device = deviceManager.DeviceInfos[i].Connect();
- //const string WIA_SCAN_COLOR_MODE = "{B9F49665-9A07-4B70-9AE7-B06FC1A4BE3D}";
- //const string WIA_HORIZONTAL_RESOLUTION = "{6146}";
- //const string WIA_VERTICAL_RESOLUTION = "{6147}";
- //var scanner = deviceManager.DeviceInfos[i].Connect();
- //var item = device.Items[1] as Item;
- //item.Properties["6146"].set_Value(300);
- //item.Properties[WIA_SCAN_COLOR_MODE].set_Value(2);
- //item.Properties[WIA_HORIZONTAL_RESOLUTION].set_Value(300);
- //item.Properties[WIA_VERTICAL_RESOLUTION].set_Value(300);
- device = deviceManager.DeviceInfos[i].Connect();
- items = dialog.ShowSelectItems(device, SingleSelect: true);
- if (items != null)
- {
- //image = (ImageFile)device.Items[1];
- image = dialog.ShowTransfer(device.Items[1]);
- ToImage(image);
- break;
- }
- }
- j++;
- }
- if (image != null)
- {
- }
- return image;
- }
- catch (COMException)
- {
- AlertsMessage alertsMessage = new AlertsMessage();
- alertsMessage.ShowDialog(App.HomePageLoader.GetString("Scanner_Error"), App.HomePageLoader.GetString("Scanner_Errorbody"), App.HomePageLoader.GetString("Scanner_Errorok"), IconType.Tip);
-
- return null;
- }
- }
- public void ToImage(ImageFile img)
- {
-
- if (img != null)
- {
- string path = App.CachePath.ScanFilePath;
- string FileName = path+ "\\"+ Guid.NewGuid().ToString() + ".bmp";
- img.SaveFile(FileName);
- string keyy = "Imagepath";
- DialogParameters keyValues = new DialogParameters();
- keyValues.Add(keyy, FileName);
- RaiseRequestClose(new Prism.Services.Dialogs.DialogResult(ButtonResult.OK, keyValues));
- App.CachePath.AddToDeleteFiles(path);
- }
- }
- private void JpegInsertPage(ref CPDFDocument topdfdoc, string filename, FileInfo fileinfo, int width, int height, int index = 0)
- {
- string tempFileName = "";
- if (fileinfo.Extension == ".jpg" || fileinfo.Extension == ".jpeg")
- {
- topdfdoc.InsertPage(index, width, height, filename);
- }
- else
- {
- tempFileName = Path.GetTempPath() + "pngtemp.jpg";
- if (!PictureConverter.SaveJpeg(filename, tempFileName))
- {
- MessageBoxEx.Show("图片格式有问题");
- }
- topdfdoc.InsertPage(index, width, height, tempFileName);
- }
- }
- public string savefilename(string filename, string filepath)
- {
- FileInfo file = new FileInfo(filename);
- return CommonHelper.CreateFilePath(filepath + file.Name.Replace(file.Extension, ".pdf") );
- }
- }
- }
|