从几天公司从中正生物采购了一些指纹设备,要用到自己的系统里面,指纹供应商提供的DLL是C++写的,我用C# 重新写了一下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.ComponentModel;
namespace EC.Shop.WindowsForms
{
///
/// 指纹验证程序 lyhuc
///
public class FingerprintHelper
{
[DllImport("MXOTDLL.dll")]
public static extern int IsMxUsbDevice();
[DllImport("MXOTDLL.dll", EntryPoint = "mxUsbGetImage", CharSet = CharSet.Auto)]
public static extern int mxUsbGetImage(byte[] ImageBuf, long nTimeOut);
[DllImport("MXOTDLL.dll")]
public static extern void mxCancelGetImage();
[DllImport("MXOTDLL.dll")]
public static extern int IsMxGetImage();
[DllImport("MXOTDLL.dll")]
public static extern void mxGetDeviceVersion(string szVersion);
[DllImport("MXOTDLL.dll")]
public static extern int mxWriteDevSN(string pData, int nLength);
[DllImport("MXOTDLL.dll")]
public static extern int mxReadDevSN(string pData, int nLength);
[DllImport("MXOTDLL.dll")]
public static extern int mxGetMBBase64(byte[] tzBuf1, byte[] tzBuf2, byte[] tzBuf3, byte[] mbBuf);
[DllImport("MXOTDLL.dll")]
public static extern int mxGetTzBase64(byte[] input, byte[] tzBuf);
[DllImport("MXOTDLL.dll")]
public static extern int mxFingerMatchBase64(byte[] mb, byte[] tz, int level);
private byte[] imageBuf;
public byte[] FingerByteArray
{
get { return imageBuf; }
}
public FingerprintHelper()
{
imageBuf = new Byte[256 * 304];
}
///
/// 是否连接USB
///
public bool IsConnection
{
get { return (IsMxUsbDevice() == 0) ? true : false; }
}
///
/// 取消正在进行获取指纹图像操作
///
public void CancelGetImage()
{
mxCancelGetImage();
}
///
/// 是否正在进行获取指纹图像操作
///
public bool IsGetImage()
{
if (IsMxGetImage() == 0) return true;
return false;
}
///
/// 获取版本信息
///
public string GetDeviceVersion()
{
string version = "";
mxGetDeviceVersion(version);
return version;
}
///
/// 从指纹仪中,获取指纹图像。
///
/// 图像路径
/// 超时时间
///
public Image GetImage(Size size)
{
if (GetFingerCode()!=null)
{
return ToGrayBitmap(imageBuf, size);
}
return null;
}
///
/// 得到指纹特征
///
public byte[] Tz
{
get { return GetTzBase64(imageBuf); }
}
///
/// 得到指纹特征
///
///
///
private byte[] GetTzBase64(byte[] buf)
{
byte[] tzBuf = new byte[344];
mxGetTzBase64(buf, tzBuf);
return tzBuf;
}
///
/// 得到指纹byte数组
///
///
public byte[] GetFingerCode()
{
int ret;
string str = "";
ret = mxUsbGetImage(imageBuf, 0);
switch (ret)
{
case 0:
str = "采集图像成功!";
break;
case -1:
str = "打开USB设备失败!";
break;
case -2:
str = "用户取消操作!";
break;
case -3:
str = "等待手指超时!";
break;
case -4:
str = "采集图像失败!";
break;
default:
str = "异常!";
break;
}
if (ret == 0)
{
return imageBuf;
}
return null;
}
///
/// 从当前指纹中匹配对应的BASE64
///
///
///
public bool FingerMatch(byte[] tz )
{
return FingerMatchBase64(GetTzBase64(GetFingerCode()), tz, 3);
}
///
/// 从两个特征中判断是否是同一个指纹
///
///
///
public bool FingerMatch(byte[] tz1, byte[] tz2)
{
return FingerMatchBase64(tz1, tz2, 3);
}
///
/// 匹配数据库中的每行特征
///
///
///
///
///
private bool FingerMatchBase64(byte[] mb, byte[] tz, int level = 3)
{
bool result=mxFingerMatchBase64(mb, tz, level)==0?true:false;
return result;
}
///
/// 将Bytes转换成Bitmap
///
///
///
///
private unsafe Bitmap BytesToBmp(byte[] bmpBytes, Size imageSize)
{
Bitmap bmp = new Bitmap(imageSize.Width, imageSize.Height);
BitmapData bData = bmp.LockBits(new Rectangle(0, 0, imageSize.Width, imageSize.Height),
ImageLockMode.ReadWrite,
PixelFormat.Format24bppRgb);
// Copy the bytes to the bitmap object
Marshal.Copy(bmpBytes, 0, bData.Scan0, bmpBytes.Length);
bmp.UnlockBits(bData);
return bmp;
}
///
/// 将一个字节数组转换为8bit灰度位图
///
/// 显示字节数组
/// 图像宽度
/// 图像高度
/// 位图
public Bitmap ToGrayBitmap(byte[] rawValues, Size imageSize)
{
int width = imageSize.Width;
int height = imageSize.Height;
//// 申请目标位图的变量,并将其内存区域锁定
Bitmap bmp = new Bitmap(width, height, PixelFormat.Format8bppIndexed);
BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, width, height),
ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed);
//// 获取图像参数
int stride = bmpData.Stride; // 扫描线的宽度
int offset = stride - width; // 显示宽度与扫描线宽度的间隙
IntPtr iptr = bmpData.Scan0; // 获取bmpData的内存起始位置
int scanBytes = stride * height; // 用stride宽度,表示这是内存区域的大小
//// 下面把原始的显示大小字节数组转换为内存中实际存放的字节数组
int posScan = 0, posReal = 0; // 分别设置两个位置指针,指向源数组和目标数组
byte[] pixelValues = new byte[scanBytes]; //为目标数组分配内存
for (int x &#61; 0; x < height; x&#43;&#43;)
{
//// 下面的循环节是模拟行扫描
for (int y &#61; 0; y < width; y&#43;&#43;)
{
pixelValues[posScan&#43;&#43;] &#61; rawValues[posReal&#43;&#43;];
}
posScan &#43;&#61; offset; //行扫描结束&#xff0c;要将目标位置指针移过那段“间隙”
}
//// 用Marshal的Copy方法&#xff0c;将刚才得到的内存字节数组复制到BitmapData中
System.Runtime.InteropServices.Marshal.Copy(pixelValues, 0, iptr, scanBytes);
bmp.UnlockBits(bmpData); // 解锁内存区域
//// 下面的代码是为了修改生成位图的索引表&#xff0c;从伪彩修改为灰度
ColorPalette tempPalette;
using (Bitmap tempBmp &#61; new Bitmap(1, 1, PixelFormat.Format8bppIndexed))
{
tempPalette &#61; tempBmp.Palette;
}
for (int i &#61; 0; i < 256; i&#43;&#43;)
{
tempPalette.Entries[i] &#61; Color.FromArgb(i, i, i);
}
bmp.Palette &#61; tempPalette;
//// 算法到此结束&#xff0c;返回结果
return bmp;
}
///
/// 从三个指纹特征中合并指纹模板
///
///
public byte[] GetUoinMBBase64(byte[] a,byte[] b,byte[] c)
{
byte[] mb &#61; new byte[344];
mxGetMBBase64(a, b, c, mb);
return mb;
}
///
/// 将字符串转为byte数组
///
///
///
public byte[] ConvertStringToByteArrary(string s)
{
return System.Text.Encoding.Default.GetBytes(s);
}
///
/// 将byte数组转为字符串
///
///
///
public string ConvertByteArraryToString(byte[] tz)
{
return System.Text.Encoding.Default.GetString(tz);
}
/**/
///
/// 变成黑白图
///
/// 原始图
/// 模式。0:加权平均 1:算数平均
///
private Bitmap ToGray(byte[] bmpBytes, Size imageSize, int mode &#61; 1)
{
Bitmap bmp &#61; new Bitmap(imageSize.Width, imageSize.Height, PixelFormat.Format24bppRgb);
byte[] newBytes &#61; new byte[256 * 304];
int w &#61; bmp.Width;
int h &#61; bmp.Height;
try
{
byte newColor &#61; 0;
BitmapData srcData &#61; bmp.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
Marshal.Copy(bmpBytes, 0, srcData.Scan0, bmpBytes.Length);
unsafe
{
byte* p &#61; (byte*)srcData.Scan0.ToPointer();
for (int y &#61; 0; y < h; y&#43;&#43;)
{
for (int x &#61; 0; x < w; x&#43;&#43;)
{
if (mode &#61;&#61; 0) // 加权平均
{
newColor &#61; (byte)((float)p[0] * 0.114f &#43; (float)p[1] * 0.587f &#43; (float)p[2] * 0.299f);
}
else // 算数平均
{
newColor &#61; (byte)((float)(p[0] &#43; p[1] &#43; p[2]) / 3.0f);
}
p[0] &#61; newColor;
p[1] &#61; newColor;
p[2] &#61; newColor;
p &#43;&#61; 3;
}
p &#43;&#61; srcData.Stride - w * 3;
}
bmp.UnlockBits(srcData);
return bmp;
}
}
catch
{
return null;
}
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.ComponentModel;
namespace EC.Shop.WindowsForms
{
///
/// 指纹验证程序 lyhuc
///
public class FingerprintHelper
{
[DllImport("MXOTDLL.dll")]
public static extern int IsMxUsbDevice();
[DllImport("MXOTDLL.dll", EntryPoint &#61; "mxUsbGetImage", CharSet &#61; CharSet.Auto)]
public static extern int mxUsbGetImage(byte[] ImageBuf, long nTimeOut);
[DllImport("MXOTDLL.dll")]
public static extern void mxCancelGetImage();
[DllImport("MXOTDLL.dll")]
public static extern int IsMxGetImage();
[DllImport("MXOTDLL.dll")]
public static extern void mxGetDeviceVersion(string szVersion);
[DllImport("MXOTDLL.dll")]
public static extern int mxWriteDevSN(string pData, int nLength);
[DllImport("MXOTDLL.dll")]
public static extern int mxReadDevSN(string pData, int nLength);
[DllImport("MXOTDLL.dll")]
public static extern int mxGetMBBase64(byte[] tzBuf1, byte[] tzBuf2, byte[] tzBuf3, byte[] mbBuf);
[DllImport("MXOTDLL.dll")]
public static extern int mxGetTzBase64(byte[] input, byte[] tzBuf);
[DllImport("MXOTDLL.dll")]
public static extern int mxFingerMatchBase64(byte[] mb, byte[] tz, int level);
private byte[] imageBuf;
public byte[] FingerByteArray
{
get { return imageBuf; }
}
public FingerprintHelper()
{
imageBuf &#61; new Byte[256 * 304];
}
///
/// 是否连接USB
///
public bool IsConnection
{
get { return (IsMxUsbDevice() &#61;&#61; 0) ? true : false; }
}
///
/// 取消正在进行获取指纹图像操作
///
public void CancelGetImage()
{
mxCancelGetImage();
}
///
/// 是否正在进行获取指纹图像操作
///
public bool IsGetImage()
{
if (IsMxGetImage() &#61;&#61; 0) return true;
return false;
}
///
/// 获取版本信息
///
public string GetDeviceVersion()
{
string version &#61; "";
mxGetDeviceVersion(version);
return version;
}
///
/// 从指纹仪中&#xff0c;获取指纹图像。
///
/// 图像路径
/// 超时时间
///
public Image GetImage(Size size)
{
if (GetFingerCode()!&#61;null)
{
return ToGrayBitmap(imageBuf, size);
}
return null;
}
///
/// 得到指纹特征
///
public byte[] Tz
{
get { return GetTzBase64(imageBuf); }
}
///
/// 得到指纹特征
///
///
///
private byte[] GetTzBase64(byte[] buf)
{
byte[] tzBuf &#61; new byte[344];
mxGetTzBase64(buf, tzBuf);
return tzBuf;
}
///
/// 得到指纹byte数组
///
///
public byte[] GetFingerCode()
{
int ret;
string str &#61; "";
ret &#61; mxUsbGetImage(imageBuf, 0);
switch (ret)
{
case 0:
str &#61; "采集图像成功!";
break;
case -1:
str &#61; "打开USB设备失败!";
break;
case -2:
str &#61; "用户取消操作!";
break;
case -3:
str &#61; "等待手指超时!";
break;
case -4:
str &#61; "采集图像失败!";
break;
default:
str &#61; "异常!";
break;
}
if (ret &#61;&#61; 0)
{
return imageBuf;
}
return null;
}
///
/// 从当前指纹中匹配对应的BASE64
///
///
///
public bool FingerMatch(byte[] tz )
{
return FingerMatchBase64(GetTzBase64(GetFingerCode()), tz, 3);
}
///
/// 从两个特征中判断是否是同一个指纹
///
///
///
public bool FingerMatch(byte[] tz1, byte[] tz2)
{
return FingerMatchBase64(tz1, tz2, 3);
}
///
/// 匹配数据库中的每行特征
///
///
///
///
///
private bool FingerMatchBase64(byte[] mb, byte[] tz, int level &#61; 3)
{
bool result&#61;mxFingerMatchBase64(mb, tz, level)&#61;&#61;0?true:false;
return result;
}
///
/// 将Bytes转换成Bitmap
///
///
///
///
private unsafe Bitmap BytesToBmp(byte[] bmpBytes, Size imageSize)
{
Bitmap bmp &#61; new Bitmap(imageSize.Width, imageSize.Height);
BitmapData bData &#61; bmp.LockBits(new Rectangle(0, 0, imageSize.Width, imageSize.Height),
ImageLockMode.ReadWrite,
PixelFormat.Format24bppRgb);
// Copy the bytes to the bitmap object
Marshal.Copy(bmpBytes, 0, bData.Scan0, bmpBytes.Length);
bmp.UnlockBits(bData);
return bmp;
}
///
/// 将一个字节数组转换为8bit灰度位图
///
/// 显示字节数组
/// 图像宽度
/// 图像高度
///
public Bitmap ToGrayBitmap(byte[] rawValues, Size imageSize)
{
int width &#61; imageSize.Width;
int height &#61; imageSize.Height;
//// 申请目标位图的变量&#xff0c;并将其内存区域锁定
Bitmap bmp &#61; new Bitmap(width, height, PixelFormat.Format8bppIndexed);
BitmapData bmpData &#61; bmp.LockBits(new Rectangle(0, 0, width, height),
ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed);
//// 获取图像参数
int stride &#61; bmpData.Stride; // 扫描线的宽度
int offset &#61; stride - width; // 显示宽度与扫描线宽度的间隙
IntPtr iptr &#61; bmpData.Scan0; // 获取bmpData的内存起始位置
int scanBytes &#61; stride * height; // 用stride宽度&#xff0c;表示这是内存区域的大小
//// 下面把原始的显示大小字节数组转换为内存中实际存放的字节数组
int posScan &#61; 0, posReal &#61; 0; // 分别设置两个位置指针&#xff0c;指向源数组和目标数组
byte[] pixelValues &#61; new byte[scanBytes]; //为目标数组分配内存
for (int x &#61; 0; x < height; x&#43;&#43;)
{
//// 下面的循环节是模拟行扫描
for (int y &#61; 0; y < width; y&#43;&#43;)
{
pixelValues[posScan&#43;&#43;] &#61; rawValues[posReal&#43;&#43;];
}
posScan &#43;&#61; offset; //行扫描结束&#xff0c;要将目标位置指针移过那段“间隙”
}
//// 用Marshal的Copy方法&#xff0c;将刚才得到的内存字节数组复制到BitmapData中
System.Runtime.InteropServices.Marshal.Copy(pixelValues, 0, iptr, scanBytes);
bmp.UnlockBits(bmpData); // 解锁内存区域
//// 下面的代码是为了修改生成位图的索引表&#xff0c;从伪彩修改为灰度
ColorPalette tempPalette;
using (Bitmap tempBmp &#61; new Bitmap(1, 1, PixelFormat.Format8bppIndexed))
{
tempPalette &#61; tempBmp.Palette;
}
for (int i &#61; 0; i < 256; i&#43;&#43;)
{
tempPalette.Entries[i] &#61; Color.FromArgb(i, i, i);
}
bmp.Palette &#61; tempPalette;
//// 算法到此结束&#xff0c;返回结果
return bmp;
}
///
/// 从三个指纹特征中合并指纹模板
///
///
public byte[] GetUoinMBBase64(byte[] a,byte[] b,byte[] c)
{
byte[] mb &#61; new byte[344];
mxGetMBBase64(a, b, c, mb);
return mb;
}
///
/// 将字符串转为byte数组
///
///
///
public byte[] ConvertStringToByteArrary(string s)
{
return System.Text.Encoding.Default.GetBytes(s);
}
///
/// 将byte数组转为字符串
///
///
///
public string ConvertByteArraryToString(byte[] tz)
{
return System.Text.Encoding.Default.GetString(tz);
}
/**/
///
/// 变成黑白图
///
/// 原始图
/// 模式。0:加权平均 1:算数平均
///
private Bitmap ToGray(byte[] bmpBytes, Size imageSize, int mode &#61; 1)
{
Bitmap bmp &#61; new Bitmap(imageSize.Width, imageSize.Height, PixelFormat.Format24bppRgb);
byte[] newBytes &#61; new byte[256 * 304];
int w &#61; bmp.Width;
int h &#61; bmp.Height;
try
{
byte newColor &#61; 0;
BitmapData srcData &#61; bmp.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
Marshal.Copy(bmpBytes, 0, srcData.Scan0, bmpBytes.Length);
unsafe
{
byte* p &#61; (byte*)srcData.Scan0.ToPointer();
for (int y &#61; 0; y < h; y&#43;&#43;)
{
for (int x &#61; 0; x < w; x&#43;&#43;)
{
if (mode &#61;&#61; 0) // 加权平均
{
newColor &#61; (byte)((float)p[0] * 0.114f &#43; (float)p[1] * 0.587f &#43; (float)p[2] * 0.299f);
}
else // 算数平均
{
newColor &#61; (byte)((float)(p[0] &#43; p[1] &#43; p[2]) / 3.0f);
}
p[0] &#61; newColor;
p[1] &#61; newColor;
p[2] &#61; newColor;
p &#43;&#61; 3;
}
p &#43;&#61; srcData.Stride - w * 3;
}
bmp.UnlockBits(srcData);
return bmp;
}
}
catch
{
return null;
}
}
}
}