• MXOTDLL.dll C#


    从几天公司从中正生物采购了一些指纹设备,要用到自己的系统里面,指纹供应商提供的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
    {
    /// <summary>
    /// 指纹验证程序 lyhuc
    /// </summary>
    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];
    }

    /// <summary>
    /// 是否连接USB
    /// </summary>
    public bool IsConnection
    {
    get { return (IsMxUsbDevice() == 0) ? true : false; }
    }


    /// <summary>
    /// 取消正在进行获取指纹图像操作
    /// </summary>
    public void CancelGetImage()
    {
    mxCancelGetImage();
    }

    /// <summary>
    /// 是否正在进行获取指纹图像操作
    /// </summary>
    public bool IsGetImage()
    {
    if (IsMxGetImage() == 0) return true;
    return false;
    }

    /// <summary>
    /// 获取版本信息
    /// </summary>
    public string GetDeviceVersion()
    {
    string version = "";
    mxGetDeviceVersion(version);
    return version;
    }

    /// <summary>
    /// 从指纹仪中,获取指纹图像。
    /// </summary>
    /// <param name="imgpath">图像路径</param>
    /// <param name="timeOut">超时时间</param>
    /// <returns></returns>
    public Image GetImage(Size size)
    {
    if (GetFingerCode()!=null)
    {
    return ToGrayBitmap(imageBuf, size);
    }

    return null;
    }

    /// <summary>
    /// 得到指纹特征
    /// </summary>
    public byte[] Tz
    {
    get { return GetTzBase64(imageBuf); }
    }

    /// <summary>
    /// 得到指纹特征
    /// </summary>
    /// <param name="byteArrayIn"></param>
    /// <returns></returns>
    private byte[] GetTzBase64(byte[] buf)
    {
    byte[] tzBuf = new byte[344];
    mxGetTzBase64(buf, tzBuf);
    return tzBuf;
    }

    /// <summary>
    /// 得到指纹byte数组
    /// </summary>
    /// <returns></returns>
    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;
    }

    /// <summary>
    /// 从当前指纹中匹配对应的BASE64
    /// </summary>
    /// <param name="tz"></param>
    /// <returns></returns>
    public bool FingerMatch(byte[] tz )
    {
    return FingerMatchBase64(GetTzBase64(GetFingerCode()), tz, 3);
    }

    /// <summary>
    /// 从两个特征中判断是否是同一个指纹
    /// </summary>
    /// <param name="tz"></param>
    /// <returns></returns>
    public bool FingerMatch(byte[] tz1, byte[] tz2)
    {
    return FingerMatchBase64(tz1, tz2, 3);
    }


    /// <summary>
    /// 匹配数据库中的每行特征
    /// </summary>
    /// <param name="mb"></param>
    /// <param name="tz"></param>
    /// <param name="level"></param>
    /// <returns></returns>
    private bool FingerMatchBase64(byte[] mb, byte[] tz, int level = 3)
    {
    bool result=mxFingerMatchBase64(mb, tz, level)==0?true:false;
    return result;
    }

    /// <summary>
    /// 将Bytes转换成Bitmap
    /// </summary>
    /// <param name="bmpBytes"></param>
    /// <param name="imageSize"></param>
    /// <returns></returns>
    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;
    }


    /// <summary>
    /// 将一个字节数组转换为8bit灰度位图
    /// </summary>
    /// <param name="rawValues">显示字节数组</param>
    /// <param name="width">图像宽度</param>
    /// <param name="height">图像高度</param>
    /// <returns>位图</returns>
    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 = 0; x < height; x++)
    {
    //// 下面的循环节是模拟行扫描
    for (int y = 0; y < width; y++)
    {
    pixelValues[posScan
    ++] = rawValues[posReal++];
    }
    posScan
    += offset; //行扫描结束,要将目标位置指针移过那段“间隙”
    }

    //// 用Marshal的Copy方法,将刚才得到的内存字节数组复制到BitmapData中
    System.Runtime.InteropServices.Marshal.Copy(pixelValues, 0, iptr, scanBytes);
    bmp.UnlockBits(bmpData);
    // 解锁内存区域

    //// 下面的代码是为了修改生成位图的索引表,从伪彩修改为灰度
    ColorPalette tempPalette;
    using (Bitmap tempBmp = new Bitmap(1, 1, PixelFormat.Format8bppIndexed))
    {
    tempPalette
    = tempBmp.Palette;
    }
    for (int i = 0; i < 256; i++)
    {
    tempPalette.Entries[i]
    = Color.FromArgb(i, i, i);
    }

    bmp.Palette
    = tempPalette;

    //// 算法到此结束,返回结果
    return bmp;
    }


    /// <summary>
    /// 从三个指纹特征中合并指纹模板
    /// </summary>
    /// <returns></returns>
    public byte[] GetUoinMBBase64(byte[] a,byte[] b,byte[] c)
    {
    byte[] mb = new byte[344];
    mxGetMBBase64(a, b, c, mb);

    return mb;
    }

    /// <summary>
    /// 将字符串转为byte数组
    /// </summary>
    /// <param name="s"></param>
    /// <returns></returns>
    public byte[] ConvertStringToByteArrary(string s)
    {
    return System.Text.Encoding.Default.GetBytes(s);
    }


    /// <summary>
    /// 将byte数组转为字符串
    /// </summary>
    /// <param name="tz"></param>
    /// <returns></returns>
    public string ConvertByteArraryToString(byte[] tz)
    {
    return System.Text.Encoding.Default.GetString(tz);
    }

    /**/
    /// <summary>
    /// 变成黑白图
    /// </summary>
    /// <param name="bmp">原始图</param>
    /// <param name="mode">模式。0:加权平均 1:算数平均</param>
    /// <returns></returns>
    private Bitmap ToGray(byte[] bmpBytes, Size imageSize, int mode = 1)
    {
    Bitmap bmp
    = new Bitmap(imageSize.Width, imageSize.Height, PixelFormat.Format24bppRgb);

    byte[] newBytes = new byte[256 * 304];
    int w = bmp.Width;
    int h = bmp.Height;



    try
    {
    byte newColor = 0;
    BitmapData srcData
    = bmp.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
    Marshal.Copy(bmpBytes,
    0, srcData.Scan0, bmpBytes.Length);
    unsafe
    {
    byte* p = (byte*)srcData.Scan0.ToPointer();
    for (int y = 0; y < h; y++)
    {
    for (int x = 0; x < w; x++)
    {

    if (mode == 0) // 加权平均
    {
    newColor
    = (byte)((float)p[0] * 0.114f + (float)p[1] * 0.587f + (float)p[2] * 0.299f);
    }
    else    // 算数平均
    {
    newColor
    = (byte)((float)(p[0] + p[1] + p[2]) / 3.0f);
    }
    p[
    0] = newColor;
    p[
    1] = newColor;
    p[
    2] = newColor;

    p
    += 3;
    }
    p
    += srcData.Stride - w * 3;
    }
    bmp.UnlockBits(srcData);
    return bmp;
    }
    }
    catch
    {
    return null;
    }

    }

    }
    }
  • 相关阅读:
    VBA通配符(*, ?, ~)
    pycharm更换包下载镜像
    python在使用pip安装模块的时候下载速度很慢的问题
    java标识符
    C语言||作业01 结构:通讯录
    C语言寒假大作战04
    C语言寒假大作战03
    C语言寒假大作战02
    c语言寒假大作战
    C语言I作业12—学期总结
  • 原文地址:https://www.cnblogs.com/hubj/p/1988214.html
Copyright © 2020-2023  润新知