.net wia驱动扫描仪

  通过各种途径,将当前比较流行的驱动扫描仪封装成了一个简单实用的class,调用扫描仪时,只需要重新创建个对象即可,代码如下:
  

using System;
using System.Collections.Generic;
using System.Text;
using WIA;
using System.IO;
using System.Drawing;
using System.Windows.Forms;

namespace WMSclient.Scanner
{//手动扫描
class Wia_ScanClass
{
private class WIA_DPS_DOCUMENT_HANDLING_SELECT
{
public const uint FEEDER = 1u;
public const uint FLATBED = 2u;
}
private class WIA_DPS_DOCUMENT_HANDLING_STATUS
{
public const uint FEED_READY = 1u;
}
private class WIA_PROPERTIES
{
public const uint WIA_RESERVED_FOR_NEW_PROPS = 1024u;
public const uint WIA_DIP_FIRST = 2u;
public const uint WIA_DPA_FIRST = 1026u;
public const uint WIA_DPC_FIRST = 2050u;
public const uint WIA_DPS_FIRST = 3074u;
public const uint WIA_DPS_DOCUMENT_HANDLING_STATUS = 3087u;
public const uint WIA_DPS_DOCUMENT_HANDLING_SELECT = 3088u;
}
private class WIA_ERRORS
{
public const uint BASE_VAL_WIA_ERROR = 2149646336u;
public const uint WIA_ERROR_PAPER_EMPTY = 2149646339u;
}
public class IMAGEFORAT//图片格式
{
public const string wiaFormatBMP="{B96B3CAB-0728-11D3-9D7B-0000F81EF32E}";
public const string wiaFormatPNG="{B96B3CAF-0728-11D3-9D7B-0000F81EF32E}";
public const string wiaFormatGIF="{B96B3CB0-0728-11D3-9D7B-0000F81EF32E}";
public const string wiaFormatJPEG="{B96B3CAE-0728-11D3-9D7B-0000F81EF32E}";
public const string wiaFormatTIFF="{B96B3CB1-0728-11D3-9D7B-0000F81EF32E}";
}
public class IMAGEATTRIBUTE
{
public const string wia_scan_color_mode = "6146"; //颜色模版
public const string wia_horizontal_scan_resolution_dpi = "6147"; //水平分辨率
public const string wia_vertical_scan_resolution_dpi = "6148";//垂直分辨率
public const string wia_horizontal_scan_start_pixel = "6149";//水平像素
public const string wia_vertical_scan_start_pixel = "6150";//垂直像素
public const string wia_horizontal_scan_size_pixels = "6151";
public const string wia_vertical_scan_size_pixels = "6152";
public const string wia_scan_brightness_percents = "6154";//亮度
public const string wia_scan_contrast_percents = "6155";//对比度
}
public enum ScanColor
{
Color = 1,
         Gray,
         BlackWhite = 4
}
/// <summary>
/// 扫描开始
/// flag=0,手动设置,此时attribute必须为null;
/// flag=1,默认设置,此时attribute不为空
/// </summary>
/// <param name="flag"></param>
/// <param name="attribute"></param>
/// <returns></returns>
public Dictionary<string, Image> BeginScan(int flag, Dictionary<string, int> attribute)
{
Dictionary<string, Image> pic = new Dictionary<string, Image>();
CommonDialogClass commonDialogClass = new CommonDialogClass();
//选择扫描仪
Device device = getAndSelectDevice(commonDialogClass);
if (device != null)
{
string deviceID = device.DeviceID;
WIA.CommonDialog commonDialog = new WIA.CommonDialog();
DeviceManager deviceManager = new DeviceManagerClass();
Device device2 = null;
foreach (DeviceInfo deviceInfo in deviceManager.DeviceInfos)
{
if (deviceInfo.DeviceID == deviceID)
{
WIA.Properties properties = deviceInfo.Properties;
device2 = deviceInfo.Connect();
break;
}
}
try
{
Image image = null;
ImageFile imageFile = null;
Item item = null;
if (device2 != null)
{
switch (flag)
{
case 0: item = getHandleAdjustImageFormat(commonDialog, device2); break;
case 1: item = getAutoAdjustImageFormat(device2, attribute); break;
default: break;
}
//获取图片信息
imageFile = (ImageFile)getImageFile(commonDialog, IMAGEFORAT.wiaFormatJPEG, item);
if (imageFile!=null)
{
string fullpath = Path.GetTempFileName();
if (fullpath != null && !"".Equals(fullpath))
{
if (deleteFile(fullpath))
{
imageFile.SaveFile(fullpath);
image = Image.FromFile(fullpath);
if (image!=null)
{
pic.Add(fullpath, image);
}
else
{
MessageBox.Show("图片转换失败", "提示");
return null;
}
}
else
{
MessageBox.Show("图片删除失败", "提示");
return null;
}
}
else
{
MessageBox.Show("图片路径获取失败", "提示");
return null;
}
}
else
{
MessageBox.Show("扫描图片获取失败", "提示");
return null;
}
}
else
{
MessageBox.Show("未连接到扫描仪", "提示");
return null;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), "提示");
}
}
return pic;
}
/// <summary>
/// 删除文件
/// </summary>
/// <returns></returns>
public bool deleteFile(string filePath)
{
Boolean flag = false;
if (File.Exists(filePath))
{
File.Delete(filePath);
flag = true;
}
return flag;
}
/// <summary>
/// 选择扫描仪
/// </summary>
/// <returns></returns>
public Device getAndSelectDevice(CommonDialogClass commonDialogClass)
{
return commonDialogClass.ShowSelectDevice(WiaDeviceType.UnspecifiedDeviceType, false, false);
}
/// <summary>
/// 获取imagefile
/// </summary>
/// <returns>ImageFile</returns>
/// 
public ImageFile getImageFile(WIA.CommonDialog commonDialog,string imageFrot, Item item)
{
return (ImageFile)commonDialog.ShowTransfer(item, IMAGEFORAT.wiaFormatJPEG, false);
}
private void setItem(IItem item, object property, object value)
{
WIA.Property aProperty = item.Properties.get_Item(ref property);
aProperty.set_Value(ref value);
}
/// <summary>
/// 自动调整扫描仪图片格式:默认格式,并返回item
/// </summary>
/// <returns>Item</returns>
public Item getAutoAdjustImageFormat(Device device, Dictionary<string, int> attribute)
{
Item item = device.Items[1];
if (item!=null)
{
if (attribute.ContainsKey(IMAGEATTRIBUTE.wia_horizontal_scan_resolution_dpi))
setItem(item, IMAGEATTRIBUTE.wia_horizontal_scan_resolution_dpi, attribute[IMAGEATTRIBUTE.wia_horizontal_scan_resolution_dpi]);
if (attribute.ContainsKey(IMAGEATTRIBUTE.wia_horizontal_scan_size_pixels))
setItem(item, IMAGEATTRIBUTE.wia_horizontal_scan_size_pixels, attribute[IMAGEATTRIBUTE.wia_horizontal_scan_size_pixels]);
if (attribute.ContainsKey(IMAGEATTRIBUTE.wia_horizontal_scan_start_pixel))
setItem(item, IMAGEATTRIBUTE.wia_horizontal_scan_start_pixel, attribute[IMAGEATTRIBUTE.wia_horizontal_scan_start_pixel]);
if (attribute.ContainsKey(IMAGEATTRIBUTE.wia_scan_brightness_percents))
setItem(item, IMAGEATTRIBUTE.wia_scan_brightness_percents, attribute[IMAGEATTRIBUTE.wia_scan_brightness_percents]);
if (attribute.ContainsKey(IMAGEATTRIBUTE.wia_scan_color_mode))
{
if (attribute[IMAGEATTRIBUTE.wia_scan_color_mode]==1)
setItem(item, IMAGEATTRIBUTE.wia_scan_color_mode,ScanColor.Color);
if (attribute[IMAGEATTRIBUTE.wia_scan_color_mode] ==2)
setItem(item, IMAGEATTRIBUTE.wia_scan_color_mode, ScanColor.Gray);
if (attribute[IMAGEATTRIBUTE.wia_scan_color_mode] == 4)
setItem(item, IMAGEATTRIBUTE.wia_scan_color_mode, ScanColor.BlackWhite);
}
if (attribute.ContainsKey(IMAGEATTRIBUTE.wia_scan_contrast_percents))
setItem(item, IMAGEATTRIBUTE.wia_scan_contrast_percents, attribute[IMAGEATTRIBUTE.wia_scan_contrast_percents]);
if (attribute.ContainsKey(IMAGEATTRIBUTE.wia_vertical_scan_resolution_dpi))
setItem(item, IMAGEATTRIBUTE.wia_vertical_scan_resolution_dpi, attribute[IMAGEATTRIBUTE.wia_vertical_scan_resolution_dpi]);
if (attribute.ContainsKey(IMAGEATTRIBUTE.wia_vertical_scan_size_pixels))
setItem(item, IMAGEATTRIBUTE.wia_vertical_scan_size_pixels, attribute[IMAGEATTRIBUTE.wia_vertical_scan_size_pixels]);
if (attribute.ContainsKey(IMAGEATTRIBUTE.wia_vertical_scan_start_pixel))
setItem(item, IMAGEATTRIBUTE.wia_vertical_scan_start_pixel, attribute[IMAGEATTRIBUTE.wia_vertical_scan_start_pixel]);
}
return item;
}
/// <summary>
/// 手动调节格式图片并返回item
/// </summary>
/// <returns>Item</returns>
public Item getHandleAdjustImageFormat(WIA.CommonDialog commonDialog, Device device)
{
return commonDialog.ShowSelectItems(device, WIA.WiaImageIntent.TextIntent, WIA.WiaImageBias.MinimizeSize, false, true, false)[1];
}
/// <summary>
/// 重新调整图片格式
/// </summary>
/// <param name="item"></param>
/// <param name="commonDialog"></param>
/// <returns></returns>
public void AgainAdjustImageFormat(WIA.CommonDialog commonDialog, Item item)
{
commonDialog.ShowItemProperties(item, false);
}

/// <summary>
/// 显示图片信息
/// </summary>
/// <param name="imageFile"></param>
/// <param name="commonDialog"></param>
/// <returns></returns>
public void ShowImage(WIA.CommonDialog commonDialog, ImageFile imageFile)
{
commonDialog.ShowPhotoPrintingWizard(imageFile);
}

/// <summary>
/// ImageFile 转换成string
/// </summary>
/// <param name="imageFile"></param>
/// <returns></returns>
public string imageFileToString(ImageFile imageFile)
{
string imageString = string.Empty;
//将获取到得图像转为Image
            if (imageFile != null)
{
var buffer = imageFile.FileData.get_BinaryData() as byte[];
MemoryStream ms = new MemoryStream();
ms.Write(buffer, 0, buffer.Length);
imageString = Convert.ToBase64String(buffer);
}
return imageString;
}
public Image imageFileToImage(ImageFile imageFile)
{
Image imgReturn = null;
//将获取到得图像转为Image
            if (imageFile != null)
{
var buffer = imageFile.FileData.get_BinaryData() as byte[];
MemoryStream ms = new MemoryStream();
ms.Write(buffer, 0, buffer.Length);
imgReturn = Image.FromStream(ms);
}
return imgReturn;
}
/// <summary>
/// Image 转换成string
/// </summary>
/// <param name="image"></param>
/// <returns></returns>
public string imageToString(Image image)
{
string pic = null;
try
{
MemoryStream ms = new MemoryStream();
//将图片保存成流
image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] arr = new byte[ms.Length];
ms.Position = 0;
ms.Read(arr, 0, (int)ms.Length);
ms.Close();
ms.Dispose();
pic = Convert.ToBase64String(arr);
}
catch (Exception)
{
return "Fail to change bitmap to string!";
}
return pic;
}
/// <summary>
/// 图片另存为
/// </summary>
/// <param name="image"></param>
/// <returns></returns>
private System.Drawing.Bitmap curBitmap;
public void SaveAsTo(string curFileName)
{
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "BMP文件 (*.bmp) | *.bmp|Gif文件 (*.gif) | *.gif|JPEG文件 (*.jpg) | *.jpg|PNG文件 (*.png) | *.png";
saveFileDialog1.FilterIndex = 2;
saveFileDialog1.RestoreDirectory = true;
saveFileDialog1.Title = "保存为";
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
curBitmap = (Bitmap)Image.FromFile(curFileName);
string fileName = saveFileDialog1.FileName;
curBitmap.Save(fileName, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
/// <summary>
/// 删除临时文件
/// </summary>
/// <param name="image"></param>
/// <param name="path"></param>
/// <returns></returns>
public bool deleteTemp(Image image,string path)
{
bool result = false;
if (image!=null)
{
image.Dispose();
File.Delete(path);
result = true;
}
return result;
}
}
}

wia驱动扫描仪的更多相关文章

  1. 在C#中使用WIA获取扫描仪数据

    WIA(Windows Image Acquire,最新版本2.0)是Windows中一组从设备中捕获图像的标准API集合,它可以从设备(例如扫描仪.数码相机)中获取静态图像,以及管理这些设备.它既是 ...

  2. 在C#中使用WIA获取扫描仪数据(利用Filter处理图片)

    WIA Automation Layer不仅能从设备中捕获照片,还能进行简单的处理.当WIA Automation Layer从设备中捕获照片,保存为一个ImageFile对象,我们可以通过访问该Im ...

  3. [原创] WINDOWS 7 精简教程之驱动精简 可用于64和32

    追风神话 发表于 2014-9-1 11:35:56   https://www.itsk.com/forum.php?mod=viewthread&tid=334491&highli ...

  4. 扫描仪API接入大全:Twain, WIA 或 两者不具有.[换个思路 春暖花开]

    最近做和扫描仪接入的扫描应用程序,深深感觉到了来自底层设备和WINDOWS协议的恶意.软件专业,对计算机深层次通知机制和协议,以及与之相连接的机器的接入协议和一些参数,当时并木有认真学习和了解,前一阵 ...

  5. Win10/UWP 让你的App使用上扫描仪

    UWP的扫描仪功能现在被微软划分到了[Windows Desktop Extensions for the UWP]中,如果要使用扫描仪扫描图片到自己的App中,首先我们要添加[Windows Des ...

  6. 关于紫光a5扫描仪的安装

    同事需要扫描写东西,从别的机器上搬来紫光a5的扫描仪,不会安装,需要帮忙. 插上扫描仪,win7提示发现新硬件,开始自动安装驱动.等了一会儿,提示无法安装,看来得手工寻找驱动来安装了.上网搜索a5的驱 ...

  7. C#:使用Twain协议实现扫描仪连续扫描

    如果用Twain协议实现一次扫描多张图片?相信有接触过Twain协议与扫描仪通信的朋友都遇到过这样的问题.按照网上的例子,在连续送纸的扫描仪中,调用一次却只能扫描一张图片,怎么破? 关于这个问题 我研 ...

  8. MDT 2010驱动管理新方法。

    参考:https://4sysops.com/archives/driver-deployment-with-microsoft-deployment-toolkit-mdt-part-1-os-de ...

  9. [Windows驱动开发](二)基础知识——数据结构

    本节主要介绍驱动开发的一些基础知识. 1. 驱动程序的基本组成 1.1. 最经常见到的数据结构 a. DRIVER_OBJECT驱动对象 // WDK中对驱动对象的定义 // 每个驱动程序都会有一个唯 ...

随机推荐

  1. MySQL备份与恢复.md

    备份与恢复使用的命令 mysqldump 常用选项 -A, --all-databases:导出全部数据库 -B, --databases:导出几个数据库.参数后面所有名字参量都被看作数据库名. -- ...

  2. python提示警告InsecureRequestWarning

    在Python3中使用以下代码报错: import requests response = requests.get(url='', verify=False) 错误代码如下: InsecureReq ...

  3. Mapreduce运行过程分析(基于Hadoop2.4)——(二)

    4.3 Map类    创建Map类和map函数.map函数是org.apache.hadoop.mapreduce.Mapper类中的定义的,当处理每一个键值对的时候,都要调用一次map方法,用户须 ...

  4. oracle 数据库 NLS_CHARACTERSET 字符集的修改

    修改Oracle 数据库字符集总结:修改方式大约有3种:方法一: (最安全的方法)数据库创建以后,如果需要修改字符集,通常需要重建数据库,通过导入导出的方式来转换.方法二: (最常用的方法)通过 &q ...

  5. leetcode 460. LFU Cache

    hash:存储的key.value.freq freq:存储的freq.key,也就是说出现1次的所有key在一起,用list连接 class LFUCache { public: LFUCache( ...

  6. stl vector、红黑树、set、multiset、map、multimap、迭代器失效、哈希表(hash_table)、hashset、hashmap、unordered_map、list

    stl:即标准模板库,该库包含了诸多在计算机科学领域里所常用的基本数据结构和基本算法 六大组件: 容器.迭代器.算法.仿函数.空间配置器.迭代适配器 迭代器:迭代器(iterator)是一种抽象的设计 ...

  7. git回答整理

    1.git常用命令 首先明确:git有工作区.暂存区.版本库,工作区是电脑里能看到的目录 创建仓库: git init newrepo,使用我们指定目录作为Git仓库(初始化后,会在newrepo目录 ...

  8. 分享四个 Linux 上的网络信息嗅探工具

    在计算机网络中,数据是暴露的,因为数据包传输是无法隐藏的,所以让我们来使用 whois.dig.nmcli和 nmap 这四个工具来嗅探网络吧. 请注意,不要在不属于自己的网络上运行 nmap ,因为 ...

  9. Python - for 循环

    Python 3  - for 循环 这次将为大家介绍 Python 3 中的 for 循环语句的使用 for 循环的一般格式如下: for <variable> in <seque ...

  10. 我的$OI$

    我只是懒得写日记啦\(\color{pink}{qwq}\) //11月8日 啊--终于要\(NOIp\)了,为此期盼了好久.紧张了好久的我,不知道会迎来怎样的结果. 我只知道这段回忆是值得保留封存的 ...