mirror of
https://github.com/soukoku/ntwain.git
synced 2026-01-02 04:17:08 +08:00
A naive and lazy implementation of mem xfer for #1.
This commit is contained in:
@@ -21,12 +21,14 @@ namespace NTwain
|
|||||||
/// Gets the file path to the complete data if the transfer was file or memory-file.
|
/// Gets the file path to the complete data if the transfer was file or memory-file.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <value>
|
/// <value>
|
||||||
/// The file.
|
/// The file path.
|
||||||
/// </value>
|
/// </value>
|
||||||
public string FilePath { get; internal set; }
|
public string FileDataPath { get; internal set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the memory data if the transfer was memory.
|
/// Gets the raw memory data if the transfer was memory.
|
||||||
|
/// Consumer application will need to do the parsing based on the values
|
||||||
|
/// from <see cref="ImageInfo"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <value>
|
/// <value>
|
||||||
/// The memory data.
|
/// The memory data.
|
||||||
@@ -39,6 +41,6 @@ namespace NTwain
|
|||||||
/// <value>
|
/// <value>
|
||||||
/// The final image information.
|
/// The final image information.
|
||||||
/// </value>
|
/// </value>
|
||||||
public TWImageInfo FinalImageInfo { get; internal set; }
|
public TWImageInfo ImageInfo { get; internal set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -14,6 +14,6 @@ namespace NTwain
|
|||||||
// keep this same in majors releases
|
// keep this same in majors releases
|
||||||
public const string Release = "0.10.0.0";
|
public const string Release = "0.10.0.0";
|
||||||
// change this for each nuget release
|
// change this for each nuget release
|
||||||
public const string Build = "0.10.0";
|
public const string Build = "0.10.1";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -810,7 +810,7 @@ namespace NTwain
|
|||||||
var xrc = DGAudio.AudioFileXfer.Get();
|
var xrc = DGAudio.AudioFileXfer.Get();
|
||||||
if (xrc == ReturnCode.XferDone)
|
if (xrc == ReturnCode.XferDone)
|
||||||
{
|
{
|
||||||
OnDataTransferred(new DataTransferredEventArgs { FilePath = filePath });
|
OnDataTransferred(new DataTransferredEventArgs { FileDataPath = filePath });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -837,7 +837,7 @@ namespace NTwain
|
|||||||
{
|
{
|
||||||
lockedPtr = MemoryManager.Instance.Lock(dataPtr);
|
lockedPtr = MemoryManager.Instance.Lock(dataPtr);
|
||||||
}
|
}
|
||||||
OnDataTransferred(new DataTransferredEventArgs { NativeData = lockedPtr, FinalImageInfo = imgInfo });
|
OnDataTransferred(new DataTransferredEventArgs { NativeData = lockedPtr, ImageInfo = imgInfo });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
@@ -874,14 +874,12 @@ namespace NTwain
|
|||||||
{
|
{
|
||||||
imgInfo = null;
|
imgInfo = null;
|
||||||
}
|
}
|
||||||
OnDataTransferred(new DataTransferredEventArgs { FilePath = filePath, FinalImageInfo = imgInfo });
|
OnDataTransferred(new DataTransferredEventArgs { FileDataPath = filePath, ImageInfo = imgInfo });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void DoImageMemoryXfer()
|
private void DoImageMemoryXfer()
|
||||||
{
|
{
|
||||||
throw new NotImplementedException();
|
|
||||||
|
|
||||||
TWSetupMemXfer memInfo;
|
TWSetupMemXfer memInfo;
|
||||||
if (DGControl.SetupMemXfer.Get(out memInfo) == ReturnCode.Success)
|
if (DGControl.SetupMemXfer.Get(out memInfo) == ReturnCode.Success)
|
||||||
{
|
{
|
||||||
@@ -898,6 +896,12 @@ namespace NTwain
|
|||||||
TheMem = MemoryManager.Instance.Allocate(memInfo.Preferred)
|
TheMem = MemoryManager.Instance.Allocate(memInfo.Preferred)
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// do the unthinkable and keep all xferred batches in memory,
|
||||||
|
// possibly defeating the purpose of mem xfer
|
||||||
|
// unless compression is used.
|
||||||
|
// todo: use array instead of memory stream?
|
||||||
|
using (MemoryStream xferredData = new MemoryStream())
|
||||||
|
{
|
||||||
var xrc = ReturnCode.Success;
|
var xrc = ReturnCode.Success;
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
@@ -907,13 +911,15 @@ namespace NTwain
|
|||||||
xrc == ReturnCode.XferDone)
|
xrc == ReturnCode.XferDone)
|
||||||
{
|
{
|
||||||
State = 7;
|
State = 7;
|
||||||
|
// optimize and allocate buffer only once instead of inside the loop?
|
||||||
byte[] buffer = new byte[(int)xferInfo.BytesWritten];
|
byte[] buffer = new byte[(int)xferInfo.BytesWritten];
|
||||||
// todo: need lock before use?
|
|
||||||
IntPtr lockPtr = IntPtr.Zero;
|
IntPtr lockPtr = IntPtr.Zero;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
lockPtr = MemoryManager.Instance.Lock(xferInfo.Memory.TheMem);
|
lockPtr = MemoryManager.Instance.Lock(xferInfo.Memory.TheMem);
|
||||||
Marshal.Copy(lockPtr, buffer, 0, buffer.Length);
|
Marshal.Copy(lockPtr, buffer, 0, buffer.Length);
|
||||||
|
xferredData.Write(buffer, 0, buffer.Length);
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
@@ -922,8 +928,6 @@ namespace NTwain
|
|||||||
MemoryManager.Instance.Unlock(lockPtr);
|
MemoryManager.Instance.Unlock(lockPtr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// now what?
|
|
||||||
|
|
||||||
}
|
}
|
||||||
} while (xrc == ReturnCode.Success);
|
} while (xrc == ReturnCode.Success);
|
||||||
|
|
||||||
@@ -940,12 +944,17 @@ namespace NTwain
|
|||||||
//}
|
//}
|
||||||
if (DGImage.ImageInfo.Get(out imgInfo) == ReturnCode.Success)
|
if (DGImage.ImageInfo.Get(out imgInfo) == ReturnCode.Success)
|
||||||
{
|
{
|
||||||
//OnDataTransferred(new DataTransferredEventArgs(IntPtr.Zero, null));
|
OnDataTransferred(new DataTransferredEventArgs { MemData = xferredData.ToArray(), ImageInfo = imgInfo });
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
Trace.TraceError("Failed to get image info after ImageMemXfer.");
|
throw new TwainException("Failed to get image info after ImageMemXfer.");
|
||||||
imgInfo = null;
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// todo: provide better mechanism for failed xfer, like event handler
|
||||||
|
throw new TwainException("Failed to transfer data.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -994,7 +1003,7 @@ namespace NTwain
|
|||||||
{
|
{
|
||||||
State = 7;
|
State = 7;
|
||||||
byte[] buffer = new byte[(int)xferInfo.BytesWritten];
|
byte[] buffer = new byte[(int)xferInfo.BytesWritten];
|
||||||
// todo: need lock before use?
|
|
||||||
IntPtr lockPtr = IntPtr.Zero;
|
IntPtr lockPtr = IntPtr.Zero;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@@ -1086,7 +1095,7 @@ namespace NTwain
|
|||||||
{
|
{
|
||||||
imgInfo = null;
|
imgInfo = null;
|
||||||
}
|
}
|
||||||
OnDataTransferred(new DataTransferredEventArgs { FilePath = finalFile, FinalImageInfo = imgInfo });
|
OnDataTransferred(new DataTransferredEventArgs { FileDataPath = finalFile, ImageInfo = imgInfo });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -120,7 +120,7 @@ namespace NTwain
|
|||||||
{
|
{
|
||||||
lockedPtr = MemoryManager.Instance.Lock(dataPtr);
|
lockedPtr = MemoryManager.Instance.Lock(dataPtr);
|
||||||
}
|
}
|
||||||
OnDataTransferred(new DataTransferredEventArgs { NativeData = lockedPtr, FilePath = file });
|
OnDataTransferred(new DataTransferredEventArgs { NativeData = lockedPtr, FileDataPath = file });
|
||||||
}
|
}
|
||||||
//}
|
//}
|
||||||
//else if (group == DataGroups.Audio)
|
//else if (group == DataGroups.Audio)
|
||||||
|
|||||||
@@ -67,9 +67,9 @@ namespace Tester.WPF
|
|||||||
{
|
{
|
||||||
Image = e.NativeData.GetWPFBitmap();
|
Image = e.NativeData.GetWPFBitmap();
|
||||||
}
|
}
|
||||||
else if (!string.IsNullOrEmpty(e.FilePath))
|
else if (!string.IsNullOrEmpty(e.FileDataPath))
|
||||||
{
|
{
|
||||||
var img = new BitmapImage(new Uri(e.FilePath));
|
var img = new BitmapImage(new Uri(e.FileDataPath));
|
||||||
Image = img;
|
Image = img;
|
||||||
}
|
}
|
||||||
base.OnDataTransferred(e);
|
base.OnDataTransferred(e);
|
||||||
|
|||||||
@@ -76,9 +76,9 @@ namespace Tester.Winform
|
|||||||
if (img != null)
|
if (img != null)
|
||||||
pictureBox1.Image = img;
|
pictureBox1.Image = img;
|
||||||
}
|
}
|
||||||
else if (!string.IsNullOrEmpty(e.FilePath))
|
else if (!string.IsNullOrEmpty(e.FileDataPath))
|
||||||
{
|
{
|
||||||
var img = new Bitmap(e.FilePath);
|
var img = new Bitmap(e.FileDataPath);
|
||||||
pictureBox1.Image = img;
|
pictureBox1.Image = img;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user