|
@@ -1,23 +1,33 @@
|
|
|
-using NTwain;
|
|
|
+using ComPDFKit.PDFDocument;
|
|
|
+using NTwain;
|
|
|
using NTwain.Data;
|
|
|
+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 : BindableBase
|
|
@@ -34,238 +44,186 @@ namespace PDF_Master.ViewModels.Dialog.HomePageToolsDialogs
|
|
|
public class CreateFromScannerDialogsViewModel : BindableBase, IDialogAware
|
|
|
{
|
|
|
|
|
|
+
|
|
|
+ public static ImageFile imageFile;
|
|
|
private List<ScannerItem> _scanners = new List<ScannerItem>();
|
|
|
public List<ScannerItem> Scanners
|
|
|
{
|
|
|
get { return _scanners; }
|
|
|
set => SetProperty(ref _scanners, value);
|
|
|
}
|
|
|
- //图片
|
|
|
- private ImageSource _img;
|
|
|
- public ImageSource Img
|
|
|
+
|
|
|
+ public string Title => "";
|
|
|
+ //图片路径
|
|
|
+ public string FilePath { get; private set; }
|
|
|
+ public event Action<IDialogResult> RequestClose;
|
|
|
+ public virtual void RaiseRequestClose(IDialogResult dialogResult)
|
|
|
{
|
|
|
- get { return _img; }
|
|
|
- set => SetProperty(ref _img, value);
|
|
|
+ RequestClose?.Invoke(dialogResult);
|
|
|
}
|
|
|
|
|
|
- //没有检测到扫描仪
|
|
|
- private bool _isEmptyScanner;
|
|
|
- public bool IsEmptyScanner
|
|
|
+ #region 框架
|
|
|
+ public bool CanCloseDialog()
|
|
|
{
|
|
|
- get { return _isEmptyScanner; }
|
|
|
- set => SetProperty(ref _isEmptyScanner, value);
|
|
|
+ return true;
|
|
|
}
|
|
|
|
|
|
- //没有生成图片
|
|
|
- private bool isEmptyImg;
|
|
|
- public bool IsEmptyImg
|
|
|
+ public void OnDialogClosed()
|
|
|
{
|
|
|
- get { return isEmptyImg; }
|
|
|
- set => SetProperty(ref isEmptyImg, value);
|
|
|
+
|
|
|
}
|
|
|
|
|
|
- public string Title => "";
|
|
|
- //图片路径
|
|
|
- public string FilePath { get; private set; }
|
|
|
- private bool IsScanner = false;
|
|
|
- public event Action<IDialogResult> RequestClose;
|
|
|
+ public void OnDialogOpened(IDialogParameters parameters)
|
|
|
+ {
|
|
|
+
|
|
|
+ }
|
|
|
+ #endregion
|
|
|
|
|
|
- private TwainSession twain;
|
|
|
|
|
|
|
|
|
- public DelegateCommand CancelCommand { get; set; }
|
|
|
+
|
|
|
+
|
|
|
+ //没有检测到扫描仪
|
|
|
+ private int _SeIndex=0;
|
|
|
+ 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; }
|
|
|
|
|
|
- public CreateFromScannerDialogsViewModel()
|
|
|
- {
|
|
|
- CancelCommand = new DelegateCommand(cancel);
|
|
|
- CreateCommand = new DelegateCommand(createFile, CanExcuteCreateFile).ObservesProperty(() => Img);
|
|
|
- SelectedScannerCommand = new DelegateCommand<object>(SelectedScanner);
|
|
|
|
|
|
- SetupTwain();
|
|
|
|
|
|
- if (Scanners.Count > 0)
|
|
|
- IsEmptyScanner = false;
|
|
|
- else
|
|
|
- IsEmptyScanner = true;
|
|
|
+ CreateFromScannerDialogsViewModel(IRegionManager regionManager, IDialogService dialogService)
|
|
|
+ {
|
|
|
+
|
|
|
+ ToScanCommand = new DelegateCommand(ToScan);
|
|
|
}
|
|
|
|
|
|
- //选择扫描仪
|
|
|
- private void SelectedScanner(object obj)
|
|
|
+ public void ToScan()
|
|
|
{
|
|
|
- if(obj != null)
|
|
|
- {
|
|
|
- if (twain.State == 4)
|
|
|
- {
|
|
|
- twain.CurrentSource.Close();
|
|
|
- }
|
|
|
- Img = null;
|
|
|
- OpenSaveScanner();
|
|
|
- var result = obj as ScannerItem;
|
|
|
- if (result.DataSourceItem.Open() != ReturnCode.Success)
|
|
|
- MessageBox.Show("连接失败");
|
|
|
- else
|
|
|
- {
|
|
|
-
|
|
|
- }
|
|
|
- Preview();
|
|
|
- }
|
|
|
-
|
|
|
+ imageFile = Scan();
|
|
|
+
|
|
|
}
|
|
|
|
|
|
- //启动检测扫描仪
|
|
|
- private void SetupTwain()
|
|
|
- {
|
|
|
- var appId = TWIdentity.CreateFromAssembly(DataGroups.Image, Assembly.GetEntryAssembly());
|
|
|
- twain = new TwainSession(appId);
|
|
|
- twain.Open();
|
|
|
- //Todo:在程序32位时,可正常扫描;
|
|
|
- //在程序64位时,不能正常获取扫描仪,并崩溃:找不到TWAINDSM.DLL模块。
|
|
|
|
|
|
- foreach (var s in twain.Select(s => new { DS = s }))
|
|
|
- {
|
|
|
- var item = new ScannerItem(s.DS);
|
|
|
- Scanners.Add(item);
|
|
|
- }
|
|
|
+ public ImageFile Scan()
|
|
|
+ {
|
|
|
+ ImageFile image = null;
|
|
|
+ DeviceManager deviceManager;
|
|
|
+ deviceManager = new DeviceManager();
|
|
|
+ Device device;
|
|
|
|
|
|
- twain.TransferError += (s, e) =>
|
|
|
- {
|
|
|
- CloseSaveScanner();
|
|
|
- // MessageBox.Show("图片传输错误:" + e.Exception.Message);
|
|
|
- };
|
|
|
- twain.DataTransferred += _session_DataTransferred;
|
|
|
- twain.SourceDisabled += (s, e) =>
|
|
|
- {
|
|
|
- //MessageBox.Show("源数据已清除");
|
|
|
- };
|
|
|
- twain.TransferReady += (s, e) =>
|
|
|
+ try
|
|
|
{
|
|
|
- //MessageBox.Show("数据传输就绪");
|
|
|
- };
|
|
|
|
|
|
- twain.SynchronizationContext = System.Threading.SynchronizationContext.Current;
|
|
|
- }
|
|
|
+ WIA.CommonDialog dialog = new WIA.CommonDialog();
|
|
|
|
|
|
- //生成图片+预览图片
|
|
|
- private void _session_DataTransferred(object sender, DataTransferredEventArgs e)
|
|
|
- {
|
|
|
- if (IsScanner)
|
|
|
- {
|
|
|
- string docPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
|
|
|
- FilePath = docPath + "\\" + DateTime.Now.ToString("yyyy-MM-dd_hh-mm-ss") + ".png";
|
|
|
-
|
|
|
- var encoder = new PngBitmapEncoder();
|
|
|
- encoder.Frames.Add(BitmapFrame.Create(e.GetNativeImageStream().ConvertToWpfBitmap(300, 0)));
|
|
|
- var test = encoder.Frames[0];
|
|
|
- FileStream fileStream = new FileStream(FilePath, FileMode.Create, FileAccess.ReadWrite);
|
|
|
- encoder.Save(fileStream);
|
|
|
- fileStream.Close();
|
|
|
- CloseSaveScanner();
|
|
|
- }
|
|
|
|
|
|
- ImageSource img = GenerateThumbnail(e);
|
|
|
- if (img != null)
|
|
|
- {
|
|
|
- App.Current.Dispatcher.BeginInvoke(new Action(() =>
|
|
|
+ Items items;
|
|
|
+ for (int i = 1, j = 0; i <= deviceManager.DeviceInfos.Count; i++)
|
|
|
{
|
|
|
- Img = img;
|
|
|
- }));
|
|
|
- }
|
|
|
- }
|
|
|
|
|
|
- //图片缩略图
|
|
|
- private ImageSource GenerateThumbnail(DataTransferredEventArgs e)
|
|
|
- {
|
|
|
- BitmapSource img = null;
|
|
|
+ if (deviceManager.DeviceInfos[i].Type != WiaDeviceType.ScannerDeviceType)
|
|
|
+ {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
|
|
|
- switch (e.TransferType)
|
|
|
- {
|
|
|
- case XferMech.Native:
|
|
|
- using (var stream = e.GetNativeImageStream())
|
|
|
+ if (j == SeIndex)
|
|
|
{
|
|
|
- if (stream != null)
|
|
|
+ device = deviceManager.DeviceInfos[i].Connect();
|
|
|
+ items = dialog.ShowSelectItems(device, SingleSelect: true);
|
|
|
+ if (items != null)
|
|
|
{
|
|
|
- // twain.CurrentSource.Capabilities.ICapYResolution.SetValue();
|
|
|
- img = stream.ConvertToWpfBitmap(300, 0);
|
|
|
+ image = dialog.ShowTransfer(device.Items[1]);
|
|
|
+ ToImage(image);
|
|
|
}
|
|
|
}
|
|
|
- break;
|
|
|
- case XferMech.File:
|
|
|
- img = new BitmapImage(new Uri(e.FileDataPath));
|
|
|
- if (img.CanFreeze)
|
|
|
- {
|
|
|
- img.Freeze();
|
|
|
- }
|
|
|
- break;
|
|
|
- case XferMech.Memory:
|
|
|
- // TODO: build current image from multiple data-xferred event
|
|
|
- break;
|
|
|
- }
|
|
|
- return img;
|
|
|
- }
|
|
|
+ j++;
|
|
|
+ }
|
|
|
|
|
|
- //预览图片
|
|
|
- private void Preview()
|
|
|
- {
|
|
|
- if (twain.CurrentSource == null) return;
|
|
|
+ if (image != null)
|
|
|
+ {
|
|
|
|
|
|
- var windowsPtr = new WindowInteropHelper(Window.GetWindow(App.Current.MainWindow)).Handle;
|
|
|
- twain.CurrentSource.Enable(SourceEnableMode.NoUI, false, windowsPtr);
|
|
|
+ }
|
|
|
+ return image;
|
|
|
+ }
|
|
|
+ catch (COMException ex)
|
|
|
+ {
|
|
|
+ if (ex.ErrorCode == -2145320939)
|
|
|
+ {
|
|
|
+ //throw new ScannerNotFoundException();
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ //throw new ScannerException("COM Exception", ex);
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- private void CloseSaveScanner()
|
|
|
- {
|
|
|
- IsScanner = false;
|
|
|
- }
|
|
|
|
|
|
- private void OpenSaveScanner()
|
|
|
- {
|
|
|
- IsScanner = true;
|
|
|
- }
|
|
|
- /// <summary>
|
|
|
- /// 是否可以创建
|
|
|
- /// </summary>
|
|
|
- /// <returns></returns>
|
|
|
- private bool CanExcuteCreateFile()
|
|
|
+ public void ToImage(ImageFile img)
|
|
|
{
|
|
|
- if (Img == null)
|
|
|
- IsEmptyImg = true;
|
|
|
- else
|
|
|
- IsEmptyImg = false;
|
|
|
+ //System.Windows.Forms.SaveFileDialog mys = new System.Windows.Forms.SaveFileDialog();
|
|
|
+ //mys.FileName = "MergeDocuments";
|
|
|
+ //mys.Filter = "bmp文件|*.bmp";
|
|
|
+ ////mys.ShowDialog();
|
|
|
+ //string myFileN = mys.FileName.ToString();
|
|
|
+ //myFileN = "C\\";//要保存的文件路径
|
|
|
+ //string FileName = myFileN+ mys.FileName;
|
|
|
+ //FileName = (FileName.Remove(0, FileName.Length - 4).Contains(".bmp")) ? FileName : FileName + ".bmp";
|
|
|
+ //System.IO.Directory.CreateDirectory("C\\img.bmp");
|
|
|
+ if (img != null)
|
|
|
|
|
|
- return (Img != null);
|
|
|
- }
|
|
|
+ {
|
|
|
+ //System.IO.Directory.CreateDirectory("C\\img.bmp");
|
|
|
+ string path = App.CachePath.ScanFilePath;
|
|
|
+ string FileName = path+ "\\"+ Guid.NewGuid().ToString() + ".bmp";
|
|
|
+ //File.Create(FileName);
|
|
|
+ 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 createFile()
|
|
|
- {
|
|
|
- DialogParameters valuePairs = new DialogParameters();
|
|
|
- valuePairs.Add(ParameterNames.DataModel, FilePath);
|
|
|
- RequestClose.Invoke(new DialogResult(ButtonResult.OK, valuePairs));
|
|
|
- }
|
|
|
|
|
|
- private void cancel()
|
|
|
- {
|
|
|
- RequestClose.Invoke(new DialogResult(ButtonResult.Cancel));
|
|
|
- }
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
- #region 框架
|
|
|
- public bool CanCloseDialog()
|
|
|
- {
|
|
|
- return true;
|
|
|
}
|
|
|
|
|
|
- public void OnDialogClosed()
|
|
|
+
|
|
|
+ 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 void OnDialogOpened(IDialogParameters parameters)
|
|
|
- {
|
|
|
|
|
|
+ public string savefilename(string filename, string filepath)
|
|
|
+ {
|
|
|
+ FileInfo file = new FileInfo(filename);
|
|
|
+ return CommonHelper.CreateFilePath(filepath + file.Name.Replace(file.Extension, ".pdf") );
|
|
|
}
|
|
|
- #endregion
|
|
|
-
|
|
|
}
|
|
|
-}
|
|
|
+}
|