Implemented memory-file transfer.

This commit is contained in:
soukoku
2014-04-07 19:21:33 -04:00
parent 05e4eb8504
commit 2820cf7a8d
9 changed files with 88 additions and 49 deletions

View File

@@ -1,16 +1,5 @@
using System; using NTwain.Properties;
using System.Collections.Generic; using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security;
using System.Security.Permissions;
using System.Text;
using System.Windows.Media.Imaging;
using NTwain.Properties;
using NTwain.Values;
using Microsoft.Win32.SafeHandles;
namespace NTwain namespace NTwain
{ {

View File

@@ -46,11 +46,9 @@
<AssemblyOriginatorKeyFile>Sign.snk</AssemblyOriginatorKeyFile> <AssemblyOriginatorKeyFile>Sign.snk</AssemblyOriginatorKeyFile>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="PresentationCore" />
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.Drawing" /> <Reference Include="System.Drawing" />
<Reference Include="System.Windows.Forms" /> <Reference Include="System.Windows.Forms" />
<Reference Include="WindowsBase" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="AsyncPump.cs" /> <Compile Include="AsyncPump.cs" />

View File

@@ -14,6 +14,6 @@ namespace NTwain
// keep this same in majors releases // keep this same in majors releases
public const string Release = "0.9.0.0"; public const string Release = "0.9.0.0";
// change this for each nuget release // change this for each nuget release
public const string Build = "0.9.1"; public const string Build = "0.9.2";
} }
} }

View File

@@ -869,11 +869,19 @@ namespace NTwain
private void DoImageMemoryXfer() private void DoImageMemoryXfer()
{ {
throw new NotImplementedException(); throw new NotImplementedException();
}
private void DoImageMemoryFileXfer()
{
// since it's memory-file xfer need info from both (maybe)
TWSetupMemXfer memInfo; TWSetupMemXfer memInfo;
if (DGControl.SetupMemXfer.Get(out memInfo) == ReturnCode.Success) TWSetupFileXfer fileInfo;
if (DGControl.SetupMemXfer.Get(out memInfo) == ReturnCode.Success &&
DGControl.SetupFileXfer.Get(out fileInfo) == ReturnCode.Success)
{ {
TWImageMemXfer xferInfo = new TWImageMemXfer(); TWImageMemXfer xferInfo = new TWImageMemXfer();
var tempFile = Path.GetTempFileName();
string finalFile = null;
try try
{ {
xferInfo.Memory = new TWMemory xferInfo.Memory = new TWMemory
@@ -882,18 +890,75 @@ namespace NTwain
TheMem = MemoryManager.Instance.Allocate(memInfo.Preferred) TheMem = MemoryManager.Instance.Allocate(memInfo.Preferred)
}; };
var xrc = ReturnCode.Success; var xrc = ReturnCode.Success;
do using (var outStream = File.OpenWrite(tempFile))
{ {
xrc = DGImage.ImageMemXfer.Get(xferInfo); do
if (xrc == ReturnCode.XferDone)
{ {
xrc = DGImage.ImageMemFileXfer.Get(xferInfo);
if (xrc == ReturnCode.Success ||
xrc == ReturnCode.XferDone)
{
byte[] buffer = new byte[(int)xferInfo.BytesWritten];
Marshal.Copy(xferInfo.Memory.TheMem, buffer, 0, buffer.Length);
outStream.Write(buffer, 0, buffer.Length);
}
} while (xrc == ReturnCode.Success);
}
if (xrc == ReturnCode.XferDone)
{
switch (fileInfo.Format)
{
case FileFormat.Bmp:
finalFile = Path.ChangeExtension(tempFile, ".bmp");
break;
case FileFormat.Dejavu:
finalFile = Path.ChangeExtension(tempFile, ".dejavu");
break;
case FileFormat.Exif:
finalFile = Path.ChangeExtension(tempFile, ".exit");
break;
case FileFormat.Fpx:
finalFile = Path.ChangeExtension(tempFile, ".fpx");
break;
case FileFormat.Jfif:
finalFile = Path.ChangeExtension(tempFile, ".jpg");
break;
case FileFormat.Jp2:
finalFile = Path.ChangeExtension(tempFile, ".jp2");
break;
case FileFormat.Jpx:
finalFile = Path.ChangeExtension(tempFile, ".jpx");
break;
case FileFormat.Pdf:
case FileFormat.PdfA:
case FileFormat.PdfA2:
finalFile = Path.ChangeExtension(tempFile, ".pdf");
break;
case FileFormat.Pict:
finalFile = Path.ChangeExtension(tempFile, ".pict");
break;
case FileFormat.Png:
finalFile = Path.ChangeExtension(tempFile, ".png");
break;
case FileFormat.Spiff:
finalFile = Path.ChangeExtension(tempFile, ".spiff");
break;
case FileFormat.Tiff:
case FileFormat.TiffMulti:
finalFile = Path.ChangeExtension(tempFile, ".tif");
break;
case FileFormat.Xbm:
finalFile = Path.ChangeExtension(tempFile, ".xbm");
break;
default:
finalFile = Path.ChangeExtension(tempFile, ".unknown");
break;
} }
} while (xrc == ReturnCode.Success); File.Move(tempFile, finalFile);
}
} }
finally finally
{ {
@@ -901,16 +966,19 @@ namespace NTwain
{ {
MemoryManager.Instance.Free(xferInfo.Memory.TheMem); MemoryManager.Instance.Free(xferInfo.Memory.TheMem);
} }
if (File.Exists(tempFile))
{
File.Delete(tempFile);
}
}
if (File.Exists(finalFile))
{
OnDataTransferred(new DataTransferredEventArgs(IntPtr.Zero, finalFile));
} }
} }
} }
private void DoImageMemoryFileXfer()
{
// no way to test, not supported by sample source
throw new NotImplementedException();
}
#endregion #endregion
#endregion #endregion

View File

@@ -146,8 +146,8 @@ namespace NTwain
{ {
if (tryUpperWord) if (tryUpperWord)
{ {
// small routine to see if works with bad sources that put // small routine to work with bad sources that put
// 16bit value in the upper word instead of lower word. // 16bit value in the upper word instead of lower word as per the twain spec.
var rawType = Enum.GetUnderlyingType(returnType); var rawType = Enum.GetUnderlyingType(returnType);
if (typeof(ushort).IsAssignableFrom(rawType)) if (typeof(ushort).IsAssignableFrom(rawType))
{ {

View File

@@ -1,17 +1,9 @@
using System; using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NTwain.Triplets; using NTwain.Triplets;
using NTwain.Data; using NTwain.Data;
using NTwain.Values; using NTwain.Values;
using System.Runtime.InteropServices;
using System.Windows.Forms; using System.Windows.Forms;
using System.Diagnostics;
using System.Security.Permissions; using System.Security.Permissions;
using System.IO;
using System.ComponentModel;
using System.Threading;
namespace NTwain namespace NTwain
{ {

View File

@@ -1,10 +1,6 @@
using NTwain.Data; using NTwain.Data;
using System; using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Permissions; using System.Security.Permissions;
using System.Text;
using System.Windows.Forms;
namespace NTwain namespace NTwain
{ {

View File

@@ -1,9 +1,5 @@
using NTwain.Data; using NTwain.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Permissions; using System.Security.Permissions;
using System.Text;
using System.Windows.Forms; using System.Windows.Forms;
namespace NTwain namespace NTwain

View File

@@ -17,7 +17,7 @@
<Button Content="Test Acquire" Click="Button_Click_1"></Button> <Button Content="Test Acquire" Click="Button_Click_1"></Button>
</StackPanel> </StackPanel>
<ListBox x:Name="CapList" Grid.Row="1" BorderThickness="0"></ListBox> <ListBox x:Name="CapList" Grid.Row="1" BorderThickness="0" ScrollViewer.CanContentScroll="False"></ListBox>
<modern:AnimatedScrollViewer Grid.Row="1" Grid.Column="1" VerticalScrollBarVisibility="Auto" <modern:AnimatedScrollViewer Grid.Row="1" Grid.Column="1" VerticalScrollBarVisibility="Auto"
HorizontalScrollBarVisibility="Auto"> HorizontalScrollBarVisibility="Auto">