【发布时间】:2012-07-05 02:24:08
【问题描述】:
我有一台 HP Scanjet 7000(双面和 ADF 扫描仪)和一台 HP Scanjet 5500c(仅限 ADF)以及我正在开发的在 Windows 7 上使用 WIA 2.0 的扫描仪程序。
问题是代码在较旧的扫描仪型号上运行良好,但在较新的型号上,代码似乎在第一页上运行良好,然后在第二页上失败。如果我在下一行逐步执行代码;
image = (WIA.ImageFile)wiaCommonDialog.ShowTransfer(item, wiaFormatTIFF, false);
旧的扫描仪停止并等待对同一引用进行另一次调用,但新的扫描仪只是在一次连续操作中从进纸器运行它的所有页面。
我注意到,如果我在 Windows 7 中使用默认扫描程序,较新的程序会返回一个包含所有单独页面的 .tif 文件。旧版本返回单独的 .jpg 文件(每页一个)。
这向我表明,较新的扫描仪正在扫描整个进纸器,然后才准备好返回一组图像,而旧的扫描仪在扫描的每一页之间返回一个图像。
如何在代码中支持这种行为?以下是适用于旧扫描仪型号的相关代码的一部分:
public static List<Image> Scan(string scannerId)
{
List<Image> images = new List<Image>();
List<String> tmp_imageList = new List<String>();
bool hasMorePages = true;
bool useAdf = true;
bool duplex = false;
int pages = 0;
string fileName = null;
string fileName_duplex = null;
WIA.DeviceManager manager = null;
WIA.Device device = null;
WIA.DeviceInfo device_infoHolder = null;
WIA.Item item = null;
WIA.ICommonDialog wiaCommonDialog = null;
manager = new WIA.DeviceManager();
// select the correct scanner using the provided scannerId parameter
foreach (WIA.DeviceInfo info in manager.DeviceInfos)
{
if (info.DeviceID == scannerId)
{
// Find scanner to connect to
device_infoHolder = info;
break;
}
}
while (hasMorePages)
{
wiaCommonDialog = new WIA.CommonDialog();
// Connect to scanner
device = device_infoHolder.Connect();
if (device.Items[1] != null)
{
item = device.Items[1] as WIA.Item;
try
{
if ((useAdf) || (duplex))
SetupADF(device, duplex); //Sets the right properties in WIA
WIA.ImageFile image = null;
WIA.ImageFile image_duplex = null;
// scan image
image = (WIA.ImageFile)wiaCommonDialog.ShowTransfer(item, wiaFormatTIFF, false);
if (duplex)
{
image_duplex = (ImageFile)wiaCommonDialog.ShowTransfer(item, wiaFormatPNG, false);
}
// save (front) image to temp file
fileName = Path.GetTempFileName();
tmp_imageList.Add(fileName);
File.Delete(fileName);
image.SaveFile(fileName);
image = null;
// add file to images list
images.Add(Image.FromFile(fileName));
if (duplex)
{
fileName_duplex = Path.GetTempFileName();
tmp_imageList.Add(fileName_duplex);
File.Delete(fileName_duplex);
image_duplex.SaveFile(fileName_duplex);
image_duplex = null;
// add file_duplex to images list
images.Add(Image.FromFile(fileName_duplex));
}
if (useAdf || duplex)
{
hasMorePages = HasMorePages(device); //Returns true if the feeder has more pages
pages++;
}
}
catch (Exception exc)
{
throw exc;
}
finally
{
wiaCommonDialog = null;
manager = null;
item = null;
device = null;
}
}
}
device = null;
return images;
}
非常感谢您对此问题的任何帮助!我似乎无法在网上找到可行的解决方案。只是来自有同样问题的人的未答复的论坛帖子。
【问题讨论】:
-
我真希望我能看到你的“HasMorePages(device)”方法。我正在为那件作品苦苦挣扎。