You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
212 lines
9.6 KiB
212 lines
9.6 KiB
using AksWebBrowser.Common; |
|
using AKSWebBrowser.Commen; |
|
using CPF.Linux; |
|
using CPF.Mac.CoreGraphics; |
|
using CPF.Windows; |
|
using System; |
|
using System.Collections.Generic; |
|
using System.Globalization; |
|
using System.IO; |
|
using System.Linq; |
|
using System.Runtime.CompilerServices; |
|
using System.Runtime.InteropServices; |
|
using System.Text; |
|
using System.Threading; |
|
using System.Threading.Tasks; |
|
|
|
namespace AksWebBrowser.Devices |
|
{ |
|
public class LhtCF |
|
{ |
|
public static string pathfile = string.Empty; |
|
libLhtCFs.DetectCallBack pDetectCallBack = null; |
|
private delegate int DetectCallBackDelegate(int nResultID); |
|
public LhtCF() |
|
{ |
|
|
|
} |
|
|
|
/// <summary> |
|
/// 人脸比对 |
|
/// </summary> |
|
/// <param name="code"></param> |
|
/// <returns></returns> |
|
public string LhtCFFace(string code, string _pathfile) |
|
{ |
|
int status = -1; |
|
try |
|
{ |
|
pathfile = _pathfile; |
|
status = libLhtCFs.CF_Init(); |
|
if (status == 0) |
|
{ |
|
//设置检测回调 |
|
status = libLhtCFs.CF_SetDetectCallBack(DetectCallBackPro, 0); //设置回调 |
|
status = libLhtCFs.CF_OpenCamera(); |
|
if (status == 0) |
|
{ |
|
Task.Run(() => |
|
{ |
|
status = libLhtCFs.CF_CreatWindow(0, 0, 300, 800, 600, 500); |
|
if (status == 0) |
|
{ |
|
status = libLhtCFs.CF_StartLiveDetectEx(10000); |
|
} |
|
}); |
|
@event6.WaitOne(); |
|
status = hdStatus; |
|
Log.Info("人脸比对结果:" + status); |
|
} |
|
} |
|
} |
|
catch (Exception ex) |
|
{ |
|
//反初始化 |
|
libLhtCFs.CF_Uninit(); |
|
Log.Info("双目摄像头采集人脸异常:" + ex.Message); |
|
} |
|
return status.ToString(); |
|
} |
|
private static AutoResetEvent @event6 = new AutoResetEvent(false); |
|
private static int hdStatus = -1; |
|
//检测回调函数 |
|
public void DetectCallBackPro(int nResultID, int lpParam) |
|
{ |
|
if (nResultID == 100) |
|
{ |
|
try |
|
{ |
|
string time = DateTime.Now.ToString("yyyyMMddHHmmss", DateTimeFormatInfo.InvariantInfo); |
|
string dirpath = Utils.getSystemPaht() + @"/wwwroot/Face"; |
|
if (!Directory.Exists(dirpath)) |
|
{ |
|
Directory.CreateDirectory(dirpath); |
|
} |
|
var tempFile = dirpath + "/" + time + ".jpg"; |
|
hdStatus = libLhtCFs.CF_GetImage(0, Encoding.Default.GetBytes(tempFile)); |
|
byte[] faceA = File.ReadAllBytes(pathfile); |
|
byte[] faceB = File.ReadAllBytes(tempFile); |
|
int score = 0; |
|
hdStatus = libLhtCFs.CF_FaceCompare(faceA, faceB, ref score); |
|
if (score > 60 && hdStatus == 0) |
|
{ |
|
hdStatus = nResultID; |
|
} |
|
else |
|
{ |
|
hdStatus = -1; |
|
} |
|
////停止活体检测 |
|
//libLhtCFs.CF_StopLiveDetect(); |
|
////关闭视频预览窗口 |
|
//libLhtCFs.CF_CloseWindow(0); |
|
////关闭摄像头 |
|
//libLhtCFs.CF_CloseCamera(); |
|
//反初始化 |
|
libLhtCFs.CF_Uninit(); |
|
} |
|
catch (Exception ex) |
|
{ |
|
////停止活体检测 |
|
//libLhtCFs.CF_StopLiveDetect(); |
|
////关闭视频预览窗口 |
|
//libLhtCFs.CF_CloseWindow(0); |
|
////关闭摄像头 |
|
//libLhtCFs.CF_CloseCamera(); |
|
//反初始化 |
|
libLhtCFs.CF_Uninit(); |
|
Log.Info("检测回调函数异常:" + ex.Message); |
|
} |
|
@event6.Set(); |
|
} |
|
} |
|
} |
|
|
|
class libLhtCFs |
|
{ |
|
public static int FeatureSize = 5120; |
|
// 检测回调函数定义 |
|
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] |
|
public delegate void DetectCallBack(int nEventId, int lpContext); |
|
|
|
//RGB数据回调函数定义 |
|
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] |
|
public delegate void GetVideoStream(IntPtr buf, int width, int height); |
|
|
|
//mjpeg数据回调函数定义 |
|
[System.Runtime.InteropServices.UnmanagedFunctionPointerAttribute(System.Runtime.InteropServices.CallingConvention.Cdecl)] |
|
public delegate void GetMjpegStream(byte[] mjpegData, int dataLen); |
|
|
|
//初始化 |
|
[DllImport("libLhtCF", EntryPoint = "CF_Init", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)] |
|
public static extern int CF_Init(); |
|
|
|
//反初始化函数 |
|
[DllImport("libLhtCF", EntryPoint = "CF_Uninit", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)] |
|
public static extern int CF_Uninit(); |
|
|
|
//反初始化函数 |
|
[DllImport("libLhtCF", EntryPoint = "CF_GetCameraStatus", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)] |
|
public static extern int CF_GetCameraStatus(int status); |
|
|
|
//设置检测回调函数 |
|
[DllImport("libLhtCF", CallingConvention = CallingConvention.Cdecl)] |
|
public static extern int CF_SetDetectCallBack(DetectCallBack pCB, int lpContext); |
|
|
|
//设置彩色摄像头视频流回调 |
|
[DllImport("libLhtCF", EntryPoint = "SetCamADataCallback", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)] |
|
public static extern int SetCamADataCallback(GetVideoStream cb); |
|
|
|
//设置红外摄像头视频流回调 |
|
[DllImport("libLhtCF", EntryPoint = "SetCamBDataCallback", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)] |
|
public static extern int SetCamBDataCallback(GetVideoStream cb); |
|
|
|
//设置彩色摄像头mjpeg数据流回调 |
|
[DllImport("libLhtCF", EntryPoint = "SetCamADataCallbackEx", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)] |
|
public static extern int SetCamADataCallbackEx(GetMjpegStream cb); |
|
|
|
//设置红外摄像头mjpeg数据流回调 |
|
[DllImport("libLhtCF", EntryPoint = "SetCamBDataCallbackEx", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)] |
|
public static extern int SetCamBDataCallbackEx(GetMjpegStream cb); |
|
|
|
//设置分辨率 |
|
[DllImport("libLhtCF", EntryPoint = "CF_SetResolution", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)] |
|
public static extern int CF_SetResolution(int width, int height); |
|
|
|
//打开摄像头 |
|
[DllImport("libLhtCF", EntryPoint = "CF_OpenCamera", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)] |
|
public static extern int CF_OpenCamera(); |
|
|
|
//关闭摄像头 |
|
[DllImport("libLhtCF", EntryPoint = "CF_CloseCamera", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)] |
|
public static extern int CF_CloseCamera(); |
|
|
|
//开始活体检测 |
|
[DllImport("libLhtCF", EntryPoint = "CF_StartLiveDetect", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)] |
|
public static extern int CF_StartLiveDetect(); |
|
|
|
//活体检测超时时间 |
|
[DllImport("libLhtCF", EntryPoint = "CF_StartLiveDetectEx", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)] |
|
public static extern int CF_StartLiveDetectEx(int timeout); |
|
|
|
//停止活体检测 |
|
[DllImport("libLhtCF", EntryPoint = "CF_StopLiveDetect", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)] |
|
public static extern int CF_StopLiveDetect(); |
|
|
|
//获取采集图像 |
|
[DllImport("libLhtCF", CallingConvention = CallingConvention.Cdecl)] |
|
public extern static int CF_GetImage(int nImageType, byte[] FilePath); |
|
|
|
//人脸比对 |
|
[DllImport("libLhtCF", CallingConvention = CallingConvention.Cdecl)] |
|
public extern static int CF_FaceCompare(byte[] picPathA, byte[] picPathB, ref int score); |
|
|
|
//创建视频预览窗口 |
|
[DllImport("libLhtCF", EntryPoint = "CF_CreatWindow", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)] |
|
public static extern int CF_CreatWindow(int nWndType, long hWnd, int iX, int iY, int iWidth, int iHeight); |
|
|
|
//关闭视频预览窗口 |
|
[DllImport("libLhtCF", EntryPoint = "CF_CloseWindow", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)] |
|
public static extern int CF_CloseWindow(int nWndType); |
|
} |
|
}
|
|
|