using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Runtime.InteropServices;
namespace RLSB
{
///
/// err = 0 正常,非0表示错误
///
///
///
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
public delegate void NameCallBack(int err, string content );
public class libFace
{
///
/// 传图片文件路径,返回识别的标签,top决定返回前几个。
/// 一张图片可能识别出来与多个标签都匹配,而top是决定返回匹配最高的几个标签
///
///
///
///
[DllImport("FaceRecognition.dll",CallingConvention=CallingConvention.Cdecl)]
public extern static int GetNameByFile(string filename, int top, [MarshalAs(UnmanagedType.FunctionPtr)] NameCallBack cb);
///
/// 根据特证码返回识别的标签,top决定返回前几个。
///
///
///
///
[DllImport("FaceRecognition.dll",CallingConvention=CallingConvention.Cdecl)]
public extern static int GetNameByFeature(string strfeature, int top, [MarshalAs(UnmanagedType.FunctionPtr)] NameCallBack cb);
///
/// 将图片训练成人脸库与label对应
///
/// 图片路径
/// 标签名
///
[DllImport("FaceRecognition.dll",CallingConvention=CallingConvention.StdCall)]
public extern static bool AddFileLabel(string filename, string label);
//public extern static bool AddFileLabel(IntPtr filename, IntPtr label);
///
/// 返回所标签,通过“;”分割,如:ZZZ;CCC;DDD
///
///
[DllImport("FaceRecognition.dll", CallingConvention = CallingConvention.StdCall)]
public extern static int GetAllLabel([MarshalAs(UnmanagedType.FunctionPtr)] NameCallBack cb);
///
/// 返回所标签与对应的图片文件名,通过“;”分割,标签与文件名用“,”割开,如:ZZZ,333.jpg;CCC,444.jpg
/// 文件在FaceRecognition.dll所在路径下的modelimages目录下面
///
///
[DllImport("FaceRecognition.dll", CallingConvention = CallingConvention.StdCall)]
public extern static int GetAllLabelAndImage([MarshalAs(UnmanagedType.FunctionPtr)] NameCallBack cb);
///
/// 删除图片库中的素材,按标签名(会删除所有符合标签的素才)
///
///
///
[DllImport("FaceRecognition.dll", CallingConvention = CallingConvention.StdCall)]
public extern static bool DelLibByLabel( string label );
///
/// 删除图片库中的素材,按图片文件名(文件名是库自动生成的,可以通过GetAllLabelAndImage返回)
///
///
///
[DllImport("FaceRecognition.dll",CallingConvention=CallingConvention.StdCall)]
public extern static bool DelLibByFileName( string filename );
public static bool TrainFileLabel(string filen, string label)
{
return AddFileLabel(filen, label);
//IntPtr ptrPicPath = mallocIntptr(filen);
//IntPtr ptrLabel = mallocIntptr(label);
//// ret = TrainImage(ptrPicPath, ptrLabel); ;// Marshal.GetFunctionPointerForDelegate(ccb));
//bool ret=AddFileLabel(ptrPicPath, ptrLabel);
//Marshal.FreeHGlobal(ptrPicPath);
//Marshal.FreeHGlobal(ptrLabel);
//return ret;
}
//public delegate int CheckImageDG(IntPtr imagePath, CheckCallBack ccb);
//public static bool DeleteLabel(string label)
//{
// IntPtr ptrLabel = mallocIntptr(label);
// int n = DeleteLabelByName(ptrLabel);
// Marshal.FreeHGlobal(ptrLabel);
// return 1 == n;
//}
//public static int DoCheckImage(string picpath, CheckCallBack ccb)
//{
// IntPtr ptrPicPath = mallocIntptr(picpath);
// int r = CheckImage(ptrPicPath,ccb);;// Marshal.GetFunctionPointerForDelegate(ccb));
// Marshal.FreeHGlobal(ptrPicPath);
// return r;
//}
//public static int DoTrainImage(string picpath, string label)
//{
// int ret = 0;
// IntPtr ptrPicPath = mallocIntptr(picpath);
// IntPtr ptrLabel = mallocIntptr(label);
// ret = TrainImage(ptrPicPath, ptrLabel); ;// Marshal.GetFunctionPointerForDelegate(ccb));
// Marshal.FreeHGlobal(ptrPicPath);
// Marshal.FreeHGlobal(ptrLabel);
// return ret;
//}
///
/// 根据数据的长度申请非托管空间
///
/// 要申请非托管空间的数据
/// 指向非拖管空间的指针
private static IntPtr mallocIntptr(string strData)
{
//先将字符串转化成字节方式
Byte[] btData = System.Text.Encoding.Default.GetBytes(strData);
//申请非拖管空间
IntPtr m_ptr = Marshal.AllocHGlobal(btData.Length);
//给非拖管空间清0
Byte[] btZero = new Byte[btData.Length + 1]; //一定要加1,否则后面是乱码,原因未找到
Marshal.Copy(btZero, 0, m_ptr, btZero.Length);
//给指针指向的空间赋值
Marshal.Copy(btData, 0, m_ptr, btData.Length);
return m_ptr;
}
///
/// 根据长度申请非托管空间
///
/// 要申请非托管空间的大小
/// 指向非拖管空间的指针
private static IntPtr mallocIntptr(int length)
{
//申请非拖管空间
IntPtr m_ptr = Marshal.AllocHGlobal(length);
//给非拖管空间清0
Byte[] btZero = new Byte[length + 1]; //一定要加1,否则后面是乱码,原因未找到
Marshal.Copy(btZero, 0, m_ptr, btZero.Length);
//给指针指向的空间赋值
Marshal.Copy(btZero, 0, m_ptr, length);
return m_ptr;
}
}
}