282 lines
11 KiB
C#
282 lines
11 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Data;
|
|
using System.Drawing;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Windows.Forms;
|
|
using System.Runtime.InteropServices;
|
|
using System.Globalization;
|
|
using System.Timers;
|
|
using System.Threading;
|
|
|
|
namespace WindowsFormsApp1
|
|
{
|
|
public partial class Form1 : Form
|
|
{
|
|
public Form1()
|
|
{
|
|
InitializeComponent();
|
|
}
|
|
|
|
private void button1_Click(object sender, EventArgs e)
|
|
{
|
|
UIntPtr image = HGScannerLib.HGLib_LoadImage(StringToUtf8("1.jpg"));
|
|
if (UIntPtr.Zero != image)
|
|
{
|
|
HGScannerLib.HGLibSaveImageParam saveParam;
|
|
saveParam.size = 20;
|
|
saveParam.jpegQuality = 80;
|
|
saveParam.tiffCompression = 4;
|
|
saveParam.tiffJpegQuality = 80;
|
|
saveParam.ocr = 0;
|
|
HGScannerLib.HGLib_SaveImage(image, StringToUtf8("2.jpg"), ref saveParam);
|
|
HGScannerLib.HGLib_ReleaseImage(image);
|
|
}
|
|
|
|
HGScannerLib.HGLibDeviceHotPlugEventFunc fun = new HGScannerLib.HGLibDeviceHotPlugEventFunc(DeviceHotPlugEventFunc);
|
|
HGScannerLib.HGLib_InitDevice(fun, this.Handle);
|
|
Thread.Sleep(500);
|
|
|
|
IntPtr deviceNameList = HGScannerLib.HGLib_GetDeviceNameList();
|
|
if (IntPtr.Zero != deviceNameList)
|
|
{
|
|
// 获取设备列表
|
|
int i = 0;
|
|
IntPtr deviceName = Marshal.ReadIntPtr(deviceNameList, 0);
|
|
while (IntPtr.Zero != deviceName)
|
|
{
|
|
String deviceNameText = Utf8ToString(deviceName);
|
|
++i;
|
|
deviceName = Marshal.ReadIntPtr(deviceNameList, Marshal.SizeOf(typeof(IntPtr)) * i);
|
|
}
|
|
|
|
// 打开第一个设备
|
|
UIntPtr device = HGScannerLib.HGLib_OpenDevice(Marshal.ReadIntPtr(deviceNameList, 0));
|
|
if (UIntPtr.Zero != device)
|
|
{
|
|
// 获取序列号
|
|
Byte[] sn = new Byte[60];
|
|
IntPtr snAddr = Marshal.UnsafeAddrOfPinnedArrayElement(sn, 0);
|
|
HGScannerLib.HGLib_GetDeviceSN(device, snAddr, 60);
|
|
String snText = Utf8ToString(snAddr);
|
|
|
|
// 获取固件版本号
|
|
Byte[] fwVer = new Byte[60];
|
|
IntPtr fwVerAddr = Marshal.UnsafeAddrOfPinnedArrayElement(fwVer, 0);
|
|
HGScannerLib.HGLib_GetDeviceFWVersion(device, fwVerAddr, 60);
|
|
String fwVerText = Utf8ToString(fwVerAddr);
|
|
|
|
// 设置待纸扫描
|
|
Int32[] dzsm = new Int32[1];
|
|
dzsm[0] = 1;
|
|
Int32 ret1 = HGScannerLib.HGLib_SetDeviceParam(device, 67,
|
|
Marshal.UnsafeAddrOfPinnedArrayElement(dzsm, 0));
|
|
|
|
// 设置旋转90度
|
|
UInt32[] rotate = new UInt32[1];
|
|
rotate[0] = 67;
|
|
Int32 ret2 = HGScannerLib.HGLib_SetDeviceParam(device, 59,
|
|
Marshal.UnsafeAddrOfPinnedArrayElement(rotate, 0));
|
|
|
|
// 设置伽马值
|
|
Double[] gamma = new Double[1];
|
|
gamma[0] = 1.0;
|
|
Int32 ret3 = HGScannerLib.HGLib_SetDeviceParam(device, 35,
|
|
Marshal.UnsafeAddrOfPinnedArrayElement(gamma, 0));
|
|
|
|
// 获取旋转的配置
|
|
IntPtr rotateParam = HGScannerLib.HGLib_GetDeviceParam(device, 59);
|
|
if (IntPtr.Zero != rotateParam)
|
|
{
|
|
HGScannerLib.HGLibDeviceParam param = (HGScannerLib.HGLibDeviceParam)
|
|
Marshal.PtrToStructure(rotateParam, typeof(HGScannerLib.HGLibDeviceParam));
|
|
|
|
// print param
|
|
PrintParam(ref param);
|
|
|
|
HGScannerLib.HGLib_ReleaseDeviceParam(rotateParam);
|
|
}
|
|
|
|
// 获取所有配置
|
|
UInt32 groupCount = 0;
|
|
IntPtr groupParamList = HGScannerLib.HGLib_GetDeviceParamGroupList(device, ref groupCount);
|
|
if (IntPtr.Zero != groupParamList)
|
|
{
|
|
for (int groupIdx = 0; groupIdx < (int)groupCount; ++groupIdx)
|
|
{
|
|
HGScannerLib.HGLibDeviceParamGroup group = (HGScannerLib.HGLibDeviceParamGroup)
|
|
Marshal.PtrToStructure(groupParamList + Marshal.SizeOf(typeof(HGScannerLib.HGLibDeviceParamGroup)) * groupIdx,
|
|
typeof(HGScannerLib.HGLibDeviceParamGroup));
|
|
|
|
// 组名
|
|
UInt32 groupName = group.group;
|
|
// 组内配置的数量
|
|
UInt32 paramCount = group.paramCount;
|
|
|
|
for (int paramIdx = 0; paramIdx < group.paramCount; ++paramIdx)
|
|
{
|
|
HGScannerLib.HGLibDeviceParam param = (HGScannerLib.HGLibDeviceParam)
|
|
Marshal.PtrToStructure(group.param + Marshal.SizeOf(typeof(HGScannerLib.HGLibDeviceParam)) * paramIdx,
|
|
typeof(HGScannerLib.HGLibDeviceParam));
|
|
|
|
// print param
|
|
PrintParam(ref param);
|
|
}
|
|
}
|
|
|
|
HGScannerLib.HGLib_ReleaseDeviceParamGroupList(groupParamList, groupCount);
|
|
}
|
|
|
|
// 扫描
|
|
m_break = false;
|
|
HGScannerLib.HGLibDeviceScanEventFunc eventFunc = new HGScannerLib.HGLibDeviceScanEventFunc(DeviceScanEventFunc);
|
|
HGScannerLib.HGLibDeviceScanImageFunc imageFunc = new HGScannerLib.HGLibDeviceScanImageFunc(DeviceScanImageFunc);
|
|
Int32 scanRet = HGScannerLib.HGLib_StartDeviceScan(device, eventFunc, this.Handle, imageFunc, this.Handle);
|
|
if (0 != scanRet)
|
|
{
|
|
while (!m_break)
|
|
{
|
|
Thread.Sleep(100);
|
|
}
|
|
|
|
HGScannerLib.HGLib_StopDeviceScan(device);
|
|
}
|
|
|
|
HGScannerLib.HGLib_CloseDevice(device);
|
|
}
|
|
|
|
HGScannerLib.HGLib_ReleaseDeviceNameList(deviceNameList);
|
|
}
|
|
|
|
HGScannerLib.HGLib_DeinitDevice();
|
|
}
|
|
|
|
public void DeviceHotPlugEventFunc(UInt32 evt, IntPtr deviceName, IntPtr param)
|
|
{
|
|
String devNameText = Utf8ToString(deviceName);
|
|
}
|
|
|
|
public void DeviceScanEventFunc(UIntPtr device, UInt32 evt, Int32 err, IntPtr info, IntPtr param)
|
|
{
|
|
if (evt == 2)
|
|
{
|
|
m_break = true;
|
|
}
|
|
else if (3 == evt)
|
|
{
|
|
String infoText = Utf8ToString(info);
|
|
}
|
|
}
|
|
|
|
public void DeviceScanImageFunc(UIntPtr device, UIntPtr image, IntPtr param)
|
|
{
|
|
String fileName = String.Format("Scan_{0}.jpg", m_scanCount);
|
|
++m_scanCount;
|
|
|
|
HGScannerLib.HGLibSaveImageParam saveParam;
|
|
saveParam.size = 20;
|
|
saveParam.jpegQuality = 80;
|
|
saveParam.tiffCompression = 4;
|
|
saveParam.tiffJpegQuality = 80;
|
|
saveParam.ocr = 0;
|
|
HGScannerLib.HGLib_SaveImage(image, StringToUtf8(fileName), ref saveParam);
|
|
}
|
|
|
|
public String Utf8ToString(IntPtr str)
|
|
{
|
|
if (IntPtr.Zero == str)
|
|
{
|
|
return "";
|
|
}
|
|
|
|
int len = 0;
|
|
while (0 != Marshal.ReadByte(str, len))
|
|
{
|
|
++len;
|
|
}
|
|
|
|
if (0 == len)
|
|
{
|
|
return "";
|
|
}
|
|
|
|
Byte[] utf8 = new Byte[len];
|
|
Marshal.Copy(str, utf8, 0, len);
|
|
return Encoding.UTF8.GetString(utf8);
|
|
}
|
|
|
|
public IntPtr StringToUtf8(String str)
|
|
{
|
|
if (str.Length == 0)
|
|
{
|
|
return IntPtr.Zero;
|
|
}
|
|
|
|
Byte[] src = Encoding.UTF8.GetBytes(str);
|
|
Byte[] dst = new Byte[src.Length + 1];
|
|
for (int i = 0; i < src.Length; ++i)
|
|
dst[i] = src[i];
|
|
dst[dst.Length - 1] = 0;
|
|
return Marshal.UnsafeAddrOfPinnedArrayElement(dst, 0);
|
|
}
|
|
|
|
public void PrintParam(ref HGScannerLib.HGLibDeviceParam param)
|
|
{
|
|
// 配置名
|
|
UInt32 option = param.option;
|
|
|
|
if (1 == param.type) // 整型
|
|
{
|
|
Int32 value = param.typeValue.intValue;
|
|
}
|
|
else if (2 == param.type) // 枚举
|
|
{
|
|
UInt32 value = param.typeValue.enumValue;
|
|
}
|
|
else if (3 == param.type) // 双精度浮点
|
|
{
|
|
Double value = param.typeValue.doubleValue;
|
|
}
|
|
else if (4 == param.type) // BOOL
|
|
{
|
|
Int32 value = param.typeValue.boolValue;
|
|
}
|
|
|
|
if (param.rangeType == 1) // 整型列表
|
|
{
|
|
Int32[] intValueList = new Int32[param.rangeTypeValue.intValueList.count];
|
|
Marshal.Copy(param.rangeTypeValue.intValueList.value, intValueList, 0, (int)param.rangeTypeValue.intValueList.count);
|
|
}
|
|
else if (param.rangeType == 2) // 枚举列表
|
|
{
|
|
Int32[] enumValueList = new Int32[param.rangeTypeValue.enumValueList.count];
|
|
Marshal.Copy(param.rangeTypeValue.enumValueList.value, enumValueList, 0, (int)param.rangeTypeValue.enumValueList.count);
|
|
}
|
|
else if (param.rangeType == 3) // 双精度浮点列表
|
|
{
|
|
Double[] doubleValueList = new Double[param.rangeTypeValue.doubleValueList.count];
|
|
Marshal.Copy(param.rangeTypeValue.doubleValueList.value, doubleValueList, 0, (int)param.rangeTypeValue.doubleValueList.count);
|
|
}
|
|
else if (param.rangeType == 4) // 整型范围
|
|
{
|
|
// 最小值
|
|
Int32 minVal = param.rangeTypeValue.intValueRange.minValue;
|
|
// 最大值
|
|
Int32 maxVal = param.rangeTypeValue.intValueRange.maxValue;
|
|
}
|
|
else if (param.rangeType == 5) // 双精度浮点范围
|
|
{
|
|
// 最小值
|
|
Double minVal = param.rangeTypeValue.doubleValueRange.minValue;
|
|
// 最大值
|
|
Double maxVal = param.rangeTypeValue.doubleValueRange.maxValue;
|
|
}
|
|
}
|
|
|
|
public UInt32 m_scanCount = 1;
|
|
public Boolean m_break = false;
|
|
}
|
|
}
|