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 Bitmap createBitmap(UIntPtr image) { IntPtr imageData = HGScannerLib.HGLib_GetImageData(image); HGScannerLib.HGLibImageInfo imageInfo; imageInfo.width = 0; imageInfo.height = 0; imageInfo.type = 0; imageInfo.widthStep = 0; imageInfo.origin = 0; HGScannerLib.HGLib_GetImageInfo(image, ref imageInfo); Bitmap curBitmap = null; if (imageInfo.type == 1) // BINARY { curBitmap = new Bitmap((int)imageInfo.width, (int)imageInfo.height, (int)imageInfo.widthStep, System.Drawing.Imaging.PixelFormat.Format1bppIndexed, imageData); System.Drawing.Imaging.ColorPalette palette = curBitmap.Palette; palette.Entries[0] = Color.FromArgb(0, 0, 0); palette.Entries[1] = Color.FromArgb(255, 255, 255); curBitmap.Palette = palette; } else if (imageInfo.type == 2) // GRAY { curBitmap = new Bitmap((int)imageInfo.width, (int)imageInfo.height, (int)imageInfo.widthStep, System.Drawing.Imaging.PixelFormat.Format8bppIndexed, imageData); System.Drawing.Imaging.ColorPalette palette = curBitmap.Palette; for (int i = 0; i < palette.Entries.Length; i++) { palette.Entries[i] = Color.FromArgb(i, i, i); } curBitmap.Palette = palette; } else if (imageInfo.type == 3) // BGR { curBitmap = new Bitmap((int)imageInfo.width, (int)imageInfo.height, (int)imageInfo.widthStep, System.Drawing.Imaging.PixelFormat.Format24bppRgb, imageData); } else if (imageInfo.type == 4) // RGB { curBitmap = new Bitmap((int)imageInfo.width, (int)imageInfo.height, System.Drawing.Imaging.PixelFormat.Format24bppRgb); Rectangle rect = new Rectangle(0, 0, (int)imageInfo.width, (int)imageInfo.height); System.Drawing.Imaging.BitmapData bmpData = curBitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format24bppRgb); unsafe { byte* dest = (byte*)(bmpData.Scan0); byte* src = (byte*)imageData; for (int i = 0; i < (int)imageInfo.height; i++) { for (int j = 0; j < (int)imageInfo.width; j++) { dest[i * bmpData.Stride + j * 3 + 0] = src[i * imageInfo.widthStep + j * 3 + 2]; dest[i * bmpData.Stride + j * 3 + 1] = src[i * imageInfo.widthStep + j * 3 + 1]; dest[i * bmpData.Stride + j * 3 + 2] = src[i * imageInfo.widthStep + j * 3 + 0]; } } } curBitmap.UnlockBits(bmpData); } return curBitmap; } 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 = (UInt32)Marshal.SizeOf(typeof(HGScannerLib.HGLibSaveImageParam)); saveParam.jpegQuality = 80; saveParam.tiffCompression = 4; saveParam.tiffJpegQuality = 80; saveParam.ocr = 0; HGScannerLib.HGLib_SaveImage(image, StringToUtf8("2.jpg"), ref saveParam); Bitmap curBitmap = createBitmap(image); curBitmap.Save("3.jpg"); 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)); // 设置DPI UInt32[] dpi = new UInt32[1]; dpi[0] = 133; Int32 ret4 = HGScannerLib.HGLib_SetDeviceParam(device, 27, Marshal.UnsafeAddrOfPinnedArrayElement(dpi, 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; //Bitmap curBitmap = createBitmap(image); //curBitmap.Save(fileName); HGScannerLib.HGLibSaveImageParam saveParam; saveParam.size = (UInt32)Marshal.SizeOf(typeof(HGScannerLib.HGLibSaveImageParam)); 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; } }