如何检查扫描仪是否已插入(C#、.NET TWAIN)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/100284/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I check if the scanner is plugged in (C#, .NET TWAIN)
提问by SelvirK
I'm using the .NET TWAIN code from http://www.codeproject.com/KB/dotnet/twaindotnet.aspx?msg=1007385#xx1007385xxin my application. When I try to scan an image when the scanner is not plugged in, the application freezes.
我在我的应用程序中使用来自http://www.codeproject.com/KB/dotnet/twaindotnet.aspx?msg=1007385#xx1007385xx的 .NET TWAIN 代码。当我在未插入扫描仪的情况下尝试扫描图像时,应用程序会冻结。
How can I check if the device is plugged in, using the TWAIN driver?
如何使用 TWAIN 驱动程序检查设备是否已插入?
采纳答案by Spike0xff
Maybe I'm taking the question too literally, but using the TWAIN API, it is not possible to check if a device is plugged in i.e. connected and powered on. The TWAIN standard does define a capability for this purpose called CAP_DEVICEONLINE, but this feature is so poorly conceived and so few drivers implement it correctly that it is useless in practice.
也许我从字面上理解这个问题,但是使用 TWAIN API,无法检查设备是否已插入,即是否已连接并已通电。TWAIN 标准确实为此目的定义了一个称为 CAP_DEVICEONLINE 的功能,但是这个功能的构思很差,而且很少有驱动程序正确实现它,因此在实践中它是无用的。
The closest you can get is this: Open the device (MSG_OPENDS): Almost all drivers will check for device-ready when they are opened, and will display an error dialog to the user. There is no TWAIN mechanism for suppressing or detecting this dialogSome drivers will allow the user to correct the problem and continue, in which case you (your app) will never know there was a problem. Some drivers will allow the user to cancel, in which case the MSG_OPENDS operation will fail, probably returning TWRC_CANCEL but maybe TWRC_FAILURE
最接近的是: 打开设备 (MSG_OPENDS):几乎所有驱动程序在打开时都会检查设备是否就绪,并向用户显示错误对话框。没有用于抑制或检测此对话框的 TWAIN 机制某些驱动程序将允许用户纠正问题并继续,在这种情况下,您(您的应用程序)永远不会知道存在问题。某些驱动程序将允许用户取消,在这种情况下 MSG_OPENDS 操作将失败,可能返回 TWRC_CANCEL 但也可能返回 TWRC_FAILURE
A few TWAIN drivers will open without error even though the device is off-line. Such a driver mayreturn FALSE to a query of CAP_DEVICEONLINE. Such a driver will probably do the device-online check when you enable the device with MSG_ENABLEDS, and then if the device is not on-line, you get the error dialog to the user, and so on as above.
即使设备处于脱机状态,一些 TWAIN 驱动程序也可以正常打开。这样的驱动程序可能会向 CAP_DEVICEONLINE 查询返回 FALSE。当您使用 MSG_ENABLEDS 启用设备时,此类驱动程序可能会执行设备在线检查,然后如果设备未在线,您会向用户显示错误对话框,依此类推。
Aside and IMPO: WIA is 'more modern' but also much less comprehensive for scanning than TWAIN, and in my experience unusable for multipage scanning from a document feeder. WIA's designers and maintainers seem not to understand or care about scanners other than low-end consumer flatbeds. It's good for cameras.
旁白和 IMPO:WIA 比 TWAIN“更现代”,但在扫描方面也没有 TWAIN 全面,而且根据我的经验,无法从文档进纸器进行多页扫描。WIA 的设计人员和维护人员似乎并不了解或关心低端消费级平板以外的扫描仪。这对相机有好处。
回答by Veldmuis
I started of with the same source code that you downloaded from CodeProject, but moved most of the code in MainFrame.cs that initiates the scanning to a Scanner class. In order to check for scan errors I call the following method in stead of calling Twain.Acquire directly:
我从您从 CodeProject 下载的相同源代码开始,但将 MainFrame.cs 中启动扫描的大部分代码移动到 Scanner 类。为了检查扫描错误,我调用以下方法而不是直接调用 Twain.Acquire:
enum AcquireResult
{
OK = 0,
InitFailed = 1,
DeviceIDFailed = 2,
CapabilityFailed = 3,
UserInterfaceError = 4
}
private void StartScan()
{
if (!_msgFilter)
{
_parent.Enabled = false;
_msgFilter = true;
Application.AddMessageFilter(this);
}
AcquireResult ar = _twain.Acquire();
if (ar != AcquireResult.OK)
{
EndingScan();
switch (ar)
{
case AcquireResult.CapabilityFailed:
throw new Exception("Scanner capability setup failed");
case AcquireResult.DeviceIDFailed:
throw new Exception("Unable to determine device identity");
case AcquireResult.InitFailed:
throw new Exception("Scanner initialisation failed");
case AcquireResult.UserInterfaceError:
throw new Exception("Error with the Twain user interface");
default:
throw new Exception("Document scanning failed");
}
}
}
I usually initiate the scan event on a seperate thread in order for the app not to freeze while scanning is in progress.
我通常在单独的线程上启动扫描事件,以便应用程序在扫描过程中不会冻结。
回答by Spike0xff
just add this code on your TwainCommand (cmd)
只需将此代码添加到您的 TwainCommand (cmd)
case TwainCommand.Null:
{
EndingScan();
tw.CloseSrc();
Msgbox("There is no device or the scannning has been cancelled.");
break;
}
this will appear if the systems detect no device or the scanning has been cancelled.
如果系统未检测到设备或扫描已取消,则会出现此信息。
回答by Mariusz Szefera
i try do this but dont work good with TWAIN mybe try WIA
我尝试这样做,但不适合 TWAIN mybe 尝试 WIA
mybe try this:
我可以试试这个:
on buton run scanner
按钮运行扫描仪
timer1.Interval = 30000;
switch (cmd)
{
case TwainCommand.TransferReady:
{
..........
}
default:
{
timer1.Start();
break;
}
on event timer tick
在事件计时器滴答声中
{
EndingScan();
tw.CloseSrc();
}
回答by Dan Elm
You can check in the registry. In:
您可以在注册表中查看。在:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{6bdd1fc6-810f-11d0-bec7-08002be2092f}
each scanner that's ever been detected is enumerated there in the subkeys.
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{6bdd1fc6-810f-11d0-bec7-08002be2092f}
曾经检测到的每个扫描仪都在子项中枚举。
Starting with 0000
, go through and check if the CreateFileName
value is blank or has data.
从 开始0000
,通过并检查CreateFileName
值是否为空或有数据。
If it has data, it's a connected scanner, if it's blank, it's not connected.
如果有数据,则表示已连接扫描仪,如果为空白,则表示未连接。