1 C++ Dll动态库中接口及回调函数定义
/****************************************************************************
Function : Callback to get priview data
Parameters: pPreviewData - [out] preview data pointer [allocated in the DLL]
nWidth - [out] preview data width pointer [allocated in the DLL]
nHeight - [out] preview data height pointer [allocated in the DLL]
*****************************************************************************/
typedef void(__stdcall* CallbackFinger)(unsigned char *pPreviewData,
int *nWidth, int *nHeight);
/****************************************************************************
Function : Get fingerprint image
Parameters: nPrintType - [in] Capture Type.
nMissNum - [in] Number of missing fingers.
nTimeOutMs - [in] Timeout time, unit: Ms.
pFingerData - [out] Image data pointer.
nFingerWidth - [out] Image Width
nFingerHeight - [out] Image Height
pCallback - [out] Callback data. (if null, no preview)
Return : 0 - success
Other - failed
*****************************************************************************/
int __stdcall captureFinger(int nPrintType,int nMissNum,int nTimeOutMs,
unsigned char *pFingerData, int *nFingerWidth, int *nFingerHeight,
CallbackFinger pData);
/************************************************************************
Function : Open the device
Parameters: szModelPath - [in] Model file path(without include Chinese),
if null, model file is in current directory
Return : 0 - success
Other - failed
************************************************************************/
int __stdcall openDevice(char* szModelPath=NULL);
/************************************************************************
Function : Close the device
Parameters:
Return : 0 - success
Other - failed
************************************************************************/
int __stdcall closeDevice();
2 C#实现
2.1 调用dll的接口类
using System;
using System.Runtime.InteropServices;
namespace FAP60Demo
{
//定义回调函数的委托
public delegate void CallbackDelegate(IntPtr pPreviewData, int[] w, int[] h);
class mxDevFunc
{
[DllImport("mxFAP60.dll", EntryPoint = "openDevice")]
public static extern int openDevice(string szModelPath);
[DllImport("mxFAP60.dll", EntryPoint = "closeDevice")]
public static extern int closeDevice();
[DllImport("mxFAP60.dll", EntryPoint = "captureFinger")]
public static extern int captureFinger(int printType, int nMissNum, int unTimeOutMs,
byte[] pFingerData, int[] w,int[] h, CallbackDelegate call);
}
}
2.2 回调函数的实现
public void CallbackFunc(IntPtr pPreviewData, int[] w, int[] h)
{
int len = w[0] * h[0];
byte[] fingerData = new byte[len];
Marshal.Copy(pPreviewData, fingerData, 0, len);
// 处理 fingerData 里的内容
Bitmap bmap = AssistantFunc.CreateBitmap(fingerData, w[0], h[0]);
Image img = Image.FromHbitmap(bmap.GetHbitmap());
pictureBox_preview.Image = img;
pictureBox_preview.Refresh();
}
2.3 测试代码
public int testCallback()
{
int captureType = 0;
int nMissNum = 0;
int unTimeout = 10 * 1000;
byte[] pFingerData = new byte[1600 * 1500];
int[] w = new int[1];
int[] h = new int[1];
CallbackDelegate myDelegate = new CallbackDelegate(CallbackFunc);
int nRet = mxDevFunc.captureFinger(captureType, nMissNum, unTimeout,
pFingerData, w, h, myDelegate);
if (nRet == 0)
{
// 处理 fingerData 里的内容
Bitmap bmap = AssistantFunc.CreateBitmap(pFingerData, w[0], h[0]);
Image img = Image.FromHbitmap(bmap.GetHbitmap());
pictureBox_preview.Image = img;
pictureBox_preview.Refresh();
}
return nRet;
}