mirror of
https://github.com/soukoku/ntwain.git
synced 2026-02-25 13:04:07 +08:00
Road to v1 begins!
This commit is contained in:
29
Tests/Tester.WPF/ViewModels/CapVM.cs
Normal file
29
Tests/Tester.WPF/ViewModels/CapVM.cs
Normal file
@@ -0,0 +1,29 @@
|
||||
using NTwain.Data;
|
||||
|
||||
namespace Tester.WPF
|
||||
{
|
||||
/// <summary>
|
||||
/// Wraps a capability as a view model.
|
||||
/// </summary>
|
||||
class CapVM
|
||||
{
|
||||
public CapabilityId Cap { get; set; }
|
||||
|
||||
public string Name
|
||||
{
|
||||
get
|
||||
{
|
||||
if (Cap > CapabilityId.CustomBase)
|
||||
{
|
||||
return "[Custom] " + ((int)Cap - (int)CapabilityId.CustomBase);
|
||||
}
|
||||
return Cap.ToString();
|
||||
}
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return Name;
|
||||
}
|
||||
}
|
||||
}
|
||||
16
Tests/Tester.WPF/ViewModels/DSVM.cs
Normal file
16
Tests/Tester.WPF/ViewModels/DSVM.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
using NTwain.Data;
|
||||
|
||||
namespace Tester.WPF
|
||||
{
|
||||
/// <summary>
|
||||
/// Wraps a data source as view model.
|
||||
/// </summary>
|
||||
class DSVM
|
||||
{
|
||||
public TWIdentity DS { get; set; }
|
||||
|
||||
public string Name { get { return DS.ProductName; } }
|
||||
public string Version { get { return DS.Version.Info; } }
|
||||
public string Protocol { get { return string.Format("{0}.{1}", DS.ProtocolMajor, DS.ProtocolMinor); } }
|
||||
}
|
||||
}
|
||||
113
Tests/Tester.WPF/ViewModels/TwainVM.cs
Normal file
113
Tests/Tester.WPF/ViewModels/TwainVM.cs
Normal file
@@ -0,0 +1,113 @@
|
||||
using CommonWin32;
|
||||
using GalaSoft.MvvmLight.Messaging;
|
||||
using NTwain;
|
||||
using NTwain.Data;
|
||||
using System;
|
||||
using System.Reflection;
|
||||
using System.Threading;
|
||||
using System.Windows.Media;
|
||||
using System.Windows.Media.Imaging;
|
||||
|
||||
namespace Tester.WPF
|
||||
{
|
||||
/// <summary>
|
||||
/// Wraps the twain session as a view model for databinding.
|
||||
/// </summary>
|
||||
class TwainVM : TwainSession
|
||||
{
|
||||
public TwainVM()
|
||||
: base(TWIdentity.CreateFromAssembly(DataGroups.Image | DataGroups.Audio, Assembly.GetEntryAssembly()))
|
||||
{
|
||||
this.SynchronizationContext = SynchronizationContext.Current;
|
||||
}
|
||||
|
||||
private ImageSource _image;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the captured image.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// The image.
|
||||
/// </value>
|
||||
public ImageSource Image
|
||||
{
|
||||
get { return _image; }
|
||||
set
|
||||
{
|
||||
_image = value;
|
||||
RaisePropertyChanged("Image");
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnTransferError(TransferErrorEventArgs e)
|
||||
{
|
||||
if (e.Exception != null)
|
||||
{
|
||||
Messenger.Default.Send(new DialogMessage(e.Exception.Message, null)
|
||||
{
|
||||
Caption = "Transfer Error Exception",
|
||||
Icon = System.Windows.MessageBoxImage.Error,
|
||||
Button = System.Windows.MessageBoxButton.OK
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
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
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnTransferReady(TransferReadyEventArgs e)
|
||||
{
|
||||
// set it up to use file xfer
|
||||
|
||||
if (this.GetCurrentCap(CapabilityId.ICapXferMech).ConvertToEnum<XferMech>() == XferMech.File)
|
||||
{
|
||||
var formats = this.CapGetImageFileFormat();
|
||||
var wantFormat = formats.Contains(FileFormat.Tiff) ? FileFormat.Tiff : FileFormat.Bmp;
|
||||
|
||||
var fileSetup = new TWSetupFileXfer
|
||||
{
|
||||
Format = wantFormat,
|
||||
FileName = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "test.tif")
|
||||
};
|
||||
var rc = this.DGControl.SetupFileXfer.Set(fileSetup);
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnDataTransferred(DataTransferredEventArgs e)
|
||||
{
|
||||
if (e.NativeData != IntPtr.Zero)
|
||||
{
|
||||
Image = e.NativeData.GetWPFBitmap();
|
||||
}
|
||||
else if (!string.IsNullOrEmpty(e.FileDataPath))
|
||||
{
|
||||
var img = new BitmapImage(new Uri(e.FileDataPath));
|
||||
Image = img;
|
||||
}
|
||||
}
|
||||
|
||||
public void TestCapture(IntPtr hwnd)
|
||||
{
|
||||
if (State == 4)
|
||||
{
|
||||
if (this.CapGetPixelTypes().Contains(PixelType.BlackWhite))
|
||||
{
|
||||
this.CapSetPixelType(PixelType.BlackWhite);
|
||||
}
|
||||
|
||||
if (this.CapGetImageXferMechs().Contains(XferMech.File))
|
||||
{
|
||||
this.CapSetImageXferMech(XferMech.File);
|
||||
}
|
||||
|
||||
var rc = EnableSource(SourceEnableMode.NoUI, false, hwnd);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user