我正在尝试与一台兼容两种功能的打印机和扫描仪--佳能Pixma MG5750 --使用NTwain库与C#接口。我正在编写一个程序,将图像扫描到Image对象中。
扫描仪在扫描图像之前必须预热;扫描时它会显示以下弹出内容:

一旦此过程完成,它将开始扫描文档。
虽然这个程序确实有效,但问题是,这个热身过程有时会花费太长的时间,没有明显的原因,长达几分钟。这个问题在使用佳能自己的应用程序IJ扫描实用程序时从未出现,它使用TWAIN并显示相同的对话框,但只显示几秒钟。
我是否可以使用TWAIN功能来提高热身过程的速度?我尝试过更改ICapXResolution和ICapYResolution,但这些只会提高热身后实际扫描的速度,而不会影响热身本身。
我的程序如下所示。请注意,它是一个控制台应用程序,因此使用了ThreadPool。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using NTwain;
using NTwain.Data;
using System.Drawing;
using System.Threading;
namespace TwainExample
{
class Program
{
[STAThread]
static void Main(string[] args)
{
ThreadPool.QueueUserWorkItem(o => TwainWork());
Console.ReadLine();
}
static void TwainWork()
{
var identity = TWIdentity.CreateFromAssembly(DataGroups.Image, Assembly.GetEntryAssembly());
var twain = new TwainSession(identity);
twain.Open();
twain.DataTransferred += (s, e) =>
{
var stream = e.GetNativeImageStream();
var image = Image.FromStream(stream);
// Do things with the image...
};
var source = twain.First();
Console.WriteLine($"Scanning from {source.Name}...");
var openCode = source.Open();
Console.WriteLine($"Open: {openCode}");
source.Enable(SourceEnableMode.NoUI, false, IntPtr.Zero);
}
}
}它的产出如下:
Scanning from Canon MG5700 series Network...
Open: Success发布于 2018-04-07 13:48:35
这比我想的要简单得多!
我的佳能多功能向Windows公开两台设备。在我的机器上,它们是:
使用的是WIA设备,而不是佳能一号。WIA装置几乎立即升温!
Fred发布的代码对于扫描非常有用,只要你使用WIA设备,它就会在使用佳能一号时崩溃。
发布于 2018-04-01 16:03:07
不久前,我在一个带有OWIN的控制台应用程序中为扫描器(也与TWAIN一起)构建了一个web服务。我想我从来没有想过采取和你一样的方法,因为我只是把它作为一个普通的web应用程序,当我注意到一个类似的问题时,我发现了一些将扫描过程本身锁在一个线程上的例子。
基本上,我认为您不需要[STAThread]属性或ThreadPool。如果你想让你的控制台应用程序保持响应性,你可以保留ThreadPool。
如果我记得正确,还需要设备ID来获取正确的数据源。我已经修改了我的一些代码(与设置扫描配置文件相关的部分更多,这是非常天真的内联),尝试这样做(不管有没有ThreadPool):
class Program
{
static void Main(string[] args)
{
var scanner = new TwainScanner();
scanner.Scan("your device id");
Console.ReadLine();
}
}
public sealed class CustomTwainSession : TwainSession
{
private Exception _error;
private bool _cancel;
private ReturnCode _returnCode;
private DataSource _dataSource;
private Bitmap _image;
public Exception Error => _error;
public bool IsSuccess => _error == null && _returnCode == ReturnCode.Success;
public Bitmap Bitmap => _image;
static CustomTwainSession()
{
PlatformInfo.Current.PreferNewDSM = false;
}
public CustomTwainSession(): base(TwainScanner.TwainAppId)
{
_cancel = false;
TransferReady += OnTransferReady;
DataTransferred += OnDataTransferred;
TransferError += OnTransferError;
}
public void Start(string deviceId)
{
try
{
_returnCode = Open();
if (_returnCode == ReturnCode.Success)
{
_dataSource = this.FirstOrDefault(x => x.Name == deviceId);
if (_dataSource != null)
{
_returnCode = _dataSource.Open();
if (_returnCode == ReturnCode.Success)
{
_returnCode = _dataSource.Enable(SourceEnableMode.NoUI, false, IntPtr.Zero);
}
}
else
{
throw new Exception($"Device {deviceId} not found.");
}
}
}
catch (Exception ex)
{
_error = ex;
}
if (_dataSource != null && IsSourceOpen)
{
_dataSource.Close();
}
if (IsDsmOpen)
{
Close();
}
}
private void OnTransferReady(object sender, TransferReadyEventArgs e)
{
_dataSource.Capabilities.CapFeederEnabled.SetValue(BoolType.False);
_dataSource.Capabilities.CapDuplexEnabled.SetValue(BoolType.False);
_dataSource.Capabilities.ICapPixelType.SetValue(PixelType.RGB);
_dataSource.Capabilities.ICapUnits.SetValue(Unit.Inches);
TWImageLayout imageLayout;
_dataSource.DGImage.ImageLayout.Get(out imageLayout);
imageLayout.Frame = new TWFrame
{
Left = 0,
Right = 210 * 0.393701f,
Top = 0,
Bottom = 297 * 0.393701f
};
_dataSource.DGImage.ImageLayout.Set(imageLayout);
_dataSource.Capabilities.ICapXResolution.SetValue(150);
_dataSource.Capabilities.ICapYResolution.SetValue(150);
if (_cancel)
{
e.CancelAll = true;
}
}
private void OnDataTransferred(object sender, DataTransferredEventArgs e)
{
using (var output = Image.FromStream(e.GetNativeImageStream()))
{
_image = new Bitmap(output);
}
}
private void OnTransferError(object sender, TransferErrorEventArgs e)
{
_error = e.Exception;
_cancel = true;
}
public void Dispose()
{
_image.Dispose();
}
}
public sealed class TwainScanner
{
public static TWIdentity TwainAppId { get; }
private static CustomTwainSession Session { get; set; }
static volatile object _locker = new object();
static TwainScanner()
{
TwainAppId = TWIdentity.CreateFromAssembly(DataGroups.Image | DataGroups.Control, Assembly.GetEntryAssembly());
}
public Bitmap Scan(string deviceId)
{
bool lockWasTaken = false;
try
{
if (Monitor.TryEnter(_locker))
{
lockWasTaken = true;
if (Session != null)
{
Session.Dispose();
Session = null;
}
Session = new CustomTwainSession();
Session.Start(deviceId);
return Session.Bitmap;
}
else
{
return null;
}
}
finally
{
if (lockWasTaken)
{
Monitor.Exit(_locker);
}
}
}
}正如我所回避的那样,这是几年前的事了,当时我对线程还不太了解。我可能会采取不同的做法,但现在我不能用实际的扫描仪测试它。这只是我的扫描逻辑的锁定机制,我知道它解决了无响应性问题。
其他人可以随意在这种特殊的方法中打洞;我知道这并不理想:)
发布于 2018-04-06 03:45:48
我有几点建议
https://stackoverflow.com/questions/49576237
复制相似问题