2014-04-21 04:57:38 +08:00
|
|
|
|
using CommonWin32;
|
|
|
|
|
using GalaSoft.MvvmLight.Messaging;
|
|
|
|
|
using NTwain;
|
2014-04-08 19:36:28 +08:00
|
|
|
|
using NTwain.Data;
|
|
|
|
|
using System;
|
2014-09-17 19:33:52 +08:00
|
|
|
|
using System.Linq;
|
2014-08-14 19:34:55 +08:00
|
|
|
|
using System.IO;
|
2014-04-08 19:36:28 +08:00
|
|
|
|
using System.Reflection;
|
2014-04-21 04:57:38 +08:00
|
|
|
|
using System.Threading;
|
2014-04-08 19:36:28 +08:00
|
|
|
|
using System.Windows.Media;
|
|
|
|
|
using System.Windows.Media.Imaging;
|
|
|
|
|
|
|
|
|
|
namespace Tester.WPF
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Wraps the twain session as a view model for databinding.
|
|
|
|
|
/// </summary>
|
2014-04-15 07:04:48 +08:00
|
|
|
|
class TwainVM : TwainSession
|
2014-04-08 19:36:28 +08:00
|
|
|
|
{
|
|
|
|
|
public TwainVM()
|
|
|
|
|
: base(TWIdentity.CreateFromAssembly(DataGroups.Image | DataGroups.Audio, Assembly.GetEntryAssembly()))
|
|
|
|
|
{
|
2014-05-15 08:13:29 +08:00
|
|
|
|
//this.SynchronizationContext = SynchronizationContext.Current;
|
2014-04-08 19:36:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private ImageSource _image;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the captured image.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>
|
|
|
|
|
/// The image.
|
|
|
|
|
/// </value>
|
|
|
|
|
public ImageSource Image
|
|
|
|
|
{
|
|
|
|
|
get { return _image; }
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
_image = value;
|
2014-04-21 08:45:08 +08:00
|
|
|
|
OnPropertyChanged("Image");
|
2014-04-08 19:36:28 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-12 23:22:05 +08:00
|
|
|
|
protected override void OnTransferError(TransferErrorEventArgs e)
|
|
|
|
|
{
|
2014-05-15 08:13:29 +08:00
|
|
|
|
App.Current.Dispatcher.BeginInvoke(new Action(() =>
|
2014-04-12 23:22:05 +08:00
|
|
|
|
{
|
2014-05-15 08:13:29 +08:00
|
|
|
|
if (e.Exception != null)
|
2014-04-12 23:22:05 +08:00
|
|
|
|
{
|
2014-05-15 08:13:29 +08:00
|
|
|
|
Messenger.Default.Send(new DialogMessage(e.Exception.Message, null)
|
|
|
|
|
{
|
|
|
|
|
Caption = "Transfer Error Exception",
|
|
|
|
|
Icon = System.Windows.MessageBoxImage.Error,
|
|
|
|
|
Button = System.Windows.MessageBoxButton.OK
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
else
|
2014-04-12 23:22:05 +08:00
|
|
|
|
{
|
2014-05-15 08:13:29 +08:00
|
|
|
|
Messenger.Default.Send(new DialogMessage(string.Format("Return Code: {0}\nCondition Code: {1}", e.ReturnCode, e.SourceStatus.ConditionCode), null)
|
|
|
|
|
{
|
|
|
|
|
Caption = "Transfer Error",
|
|
|
|
|
Icon = System.Windows.MessageBoxImage.Error,
|
|
|
|
|
Button = System.Windows.MessageBoxButton.OK
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}));
|
2014-04-12 23:22:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
2014-04-08 19:36:28 +08:00
|
|
|
|
protected override void OnTransferReady(TransferReadyEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
// set it up to use file xfer
|
|
|
|
|
|
2014-05-20 19:25:57 +08:00
|
|
|
|
if (this.CurrentSource.CapGetCurrent(CapabilityId.ICapXferMech).ConvertToEnum<XferMech>() == XferMech.File)
|
2014-04-08 19:36:28 +08:00
|
|
|
|
{
|
2014-11-10 02:23:06 +08:00
|
|
|
|
var formats = this.CurrentSource.ICapImageFileFormat.Get();
|
2014-04-08 19:36:28 +08:00
|
|
|
|
var wantFormat = formats.Contains(FileFormat.Tiff) ? FileFormat.Tiff : FileFormat.Bmp;
|
|
|
|
|
|
|
|
|
|
var fileSetup = new TWSetupFileXfer
|
|
|
|
|
{
|
|
|
|
|
Format = wantFormat,
|
2014-08-14 19:34:55 +08:00
|
|
|
|
FileName = GetUniqueName(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "test", ".tif")
|
2014-04-08 19:36:28 +08:00
|
|
|
|
};
|
2014-05-25 06:39:07 +08:00
|
|
|
|
var rc = this.CurrentSource.DGControl.SetupFileXfer.Set(fileSetup);
|
2014-04-08 19:36:28 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-14 19:34:55 +08:00
|
|
|
|
private string GetUniqueName(string dir, string name, string ext)
|
|
|
|
|
{
|
|
|
|
|
var filePath = Path.Combine(dir, name + ext);
|
|
|
|
|
int next = 1;
|
|
|
|
|
while (File.Exists(filePath))
|
|
|
|
|
{
|
|
|
|
|
filePath = Path.Combine(dir, string.Format("{0} ({1}){2}", name, next++, ext));
|
|
|
|
|
}
|
|
|
|
|
return filePath;
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-08 19:36:28 +08:00
|
|
|
|
protected override void OnDataTransferred(DataTransferredEventArgs e)
|
|
|
|
|
{
|
2014-05-15 08:13:29 +08:00
|
|
|
|
ImageSource img = null;
|
2014-04-17 08:39:30 +08:00
|
|
|
|
if (e.NativeData != IntPtr.Zero)
|
2014-04-08 19:36:28 +08:00
|
|
|
|
{
|
2014-05-15 08:13:29 +08:00
|
|
|
|
img = e.NativeData.GetWPFBitmap();
|
2014-04-17 08:39:30 +08:00
|
|
|
|
}
|
|
|
|
|
else if (!string.IsNullOrEmpty(e.FileDataPath))
|
|
|
|
|
{
|
2014-05-15 08:13:29 +08:00
|
|
|
|
img = new BitmapImage(new Uri(e.FileDataPath));
|
|
|
|
|
}
|
|
|
|
|
if (img != null)
|
|
|
|
|
{
|
|
|
|
|
if (img.CanFreeze)
|
|
|
|
|
{
|
|
|
|
|
img.Freeze();
|
|
|
|
|
}
|
|
|
|
|
App.Current.Dispatcher.BeginInvoke(new Action(() =>
|
|
|
|
|
{
|
|
|
|
|
Image = img;
|
|
|
|
|
}));
|
2014-04-17 08:39:30 +08:00
|
|
|
|
}
|
2014-04-08 19:36:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void TestCapture(IntPtr hwnd)
|
|
|
|
|
{
|
|
|
|
|
if (State == 4)
|
|
|
|
|
{
|
2014-11-10 02:23:06 +08:00
|
|
|
|
if (this.CurrentSource.ICapPixelType.Get().Contains(PixelType.BlackWhite))
|
2014-04-08 19:36:28 +08:00
|
|
|
|
{
|
2014-11-10 02:23:06 +08:00
|
|
|
|
this.CurrentSource.ICapPixelType.Set(PixelType.BlackWhite);
|
2014-04-08 19:36:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
2014-11-10 02:23:06 +08:00
|
|
|
|
if (this.CurrentSource.ICapXferMech.Get().Contains(XferMech.File))
|
2014-04-08 19:36:28 +08:00
|
|
|
|
{
|
2014-11-10 02:23:06 +08:00
|
|
|
|
this.CurrentSource.ICapXferMech.Set(XferMech.File);
|
2014-04-08 19:36:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
2014-09-12 09:14:41 +08:00
|
|
|
|
var rc = this.CurrentSource.Enable(SourceEnableMode.NoUI, false, hwnd);
|
2014-04-08 19:36:28 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|