using AksWebBrowser.Common;
using AKSWebBrowser.Commen;
using CPF.Linux;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using static CPF.Mac.CoreMedia.CMTime;
namespace AksWebBrowser.Devices
{
public class IDCard
{
public static long icdev = -1;
public IDCard()
{
//打开设备
icdev = LibClass.ICReaderOpenUsbByFD(0);
//读序列号
byte[] pVer = new byte[64];
LibClass.ICReaderReadDevSnr(Convert.ToInt32(icdev), 20, pVer);
//获取版本
LibClass.ICReaderGetVer(Convert.ToInt32(icdev), pVer);
//蜂鸣器
LibClass.ICReaderBeep(icdev, 1, 1, 1);
}
///
/// 获取身份证
///
///
public string getIdCard(string code)
{
try
{
if (icdev > 0)
{
IDCardModel iDCardModel = new IDCardModel();
int iLen = 0;
byte[] id = new byte[260];
int status = LibClass.GetIDCardUID(Convert.ToInt32(icdev), ref iLen, id);
byte[] pID = new byte[64];
LibClass.Hex2Asc(id, pID, iLen);
byte[] CHMsg = new byte[260];
byte[] PHMsg = new byte[1032];
byte[] FPMsg = new byte[1032];
int cLen = 0, pLen = 0, fLen = 0;
status = LibClass.ReadIDCardBaseMsg(Convert.ToInt32(icdev), 1, CHMsg, ref cLen, PHMsg, ref pLen, FPMsg, ref fLen);
if (status == 0)
{
//获取图片
byte[] FileName = new byte[65536];
status = LibClass.ParseIDCardPic(PHMsg, 1024, 3, FileName);
if (status == 0)
{
string txt = Encoding.UTF8.GetString(FileName);
iDCardModel.ImageBase64 = "data:image/jpg;base64," + txt.Substring(0, (txt.IndexOf("=") + 1));
}
byte[] sCHMsg = new byte[520];
int itype = 0;
status = LibClass.ParseIDCardInfo(CHMsg, cLen, ref itype, sCHMsg);
if (status == 0)
{
string[] baseinfo = Encoding.UTF8.GetString(sCHMsg).Split("|");
iDCardModel.Name = baseinfo[0];
iDCardModel.Sex = baseinfo[1];
iDCardModel.Nation = baseinfo[2];
iDCardModel.BirthDay = baseinfo[3];
iDCardModel.Addr = baseinfo[4];
iDCardModel.Id = baseinfo[5];
iDCardModel.Regorg = baseinfo[6];
iDCardModel.StartDate = baseinfo[7];
iDCardModel.EndDate = baseinfo[8];
}
var OBJ = new
{
Data = iDCardModel
};
//关闭
LibClass.ICReaderClose(icdev);
return "{\"keycode\":\"" + code + "\",\"message\":\"seccse\",\"code\":\"200\",\"status\":true,\"data\":" + JsonConvert.SerializeObject(OBJ) + "}";
}
else
{
//关闭
LibClass.ICReaderClose(icdev);
return "{\"keycode\":\"" + code + "\",\"message\":\"fali\",\"code\":\"400\",\"status\":false,\"data\":\"" + "读取身份证失败" + "\"}";
}
}
else
{
//关闭
LibClass.ICReaderClose(icdev);
return "{\"keycode\":\"" + code + "\",\"message\":\"fali\",\"code\":\"400\",\"status\":false,\"data\":\"" + "读取身份证失败" + "\"}";
}
}
catch (Exception ex)
{
Log.Info("获取身份证异常:" + ex.Message);
if (icdev != -1)
{
LibClass.ICReaderClose(icdev);
}
return "{\"keycode\":\"" + code + "\",\"message\":\"fali\",\"code\":\"400\",\"status\":false,\"data\":\"" + ex.Message + "\"}";
}
}
// 转化字节码
public static string BytesToString(byte[] characters)
{
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
var encoding = Encoding.UTF8;
string constructedString = encoding.GetString(characters);
constructedString = constructedString.Trim(new char[] { '\'', '\"', '\\', '\0', '\a', '\b', '\f', '\n', '\r', '\t', '\v' });
return (constructedString);
}
}
}
class LibClass
{
///
/// //打开设备 默认成0
///
///
///
[DllImport("libtmz", EntryPoint = "ICReaderOpenUsbByFD", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)]
public static extern long ICReaderOpenUsbByFD(int uiFD);
///
/// //关闭设备
///
///
///
[DllImport("libtmz", EntryPoint = "ICReaderClose", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)]
public static extern int ICReaderClose(long icdev);
/*
* 0 正常
1 接触卡通道异常
2 非接卡通道异常
3 接触卡和非接卡通道异常
4 安全模块通道异常
5 接触卡和安全模块通道异常
6 非接卡和安全模块通道异常
7 接触卡、非接卡和安全模块通道异常
*/
[DllImport("libtmz", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
public static extern int ICReaderDevStatus(long icdev, ref int uStatus); //获取设备状态
//uLEDCtrl,默认 0;低半字节:0-关,1-开,2-闪烁;如控制灯 2 开: 0x21
//uDelay:闪烁间隔时间,单位:50ms,如 1 代表闪烁间隔时间为 50ms.
[DllImport("libtmz", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
public static extern int ICReaderLEDCtrl(long icdev, int uLEDCtrl, int uDelay); //灯光状态
//获取身份证 UID
[DllImport("libtmz", EntryPoint = "GetIDCardUID", SetLastError = true,
CharSet = CharSet.Auto, ExactSpelling = false,
CallingConvention = CallingConvention.StdCall)]
//说明: 获取身份证UID
public static extern int GetIDCardUID(int icdev, ref int uLen, [MarshalAs(UnmanagedType.LPArray)] byte[] pIDData);
[DllImport("libtmz", EntryPoint = "ReadIDCardBaseMsg", SetLastError = true,
CharSet = CharSet.Auto, ExactSpelling = false,
CallingConvention = CallingConvention.StdCall)]
//说明: 获取身份证原始数据
public static extern int ReadIDCardBaseMsg(int icdev, int iFlag, [MarshalAs(UnmanagedType.LPArray)] byte[] pucCHMsg, ref int puiCHMsgLen, [MarshalAs(UnmanagedType.LPArray)] byte[] pucPHMsg, ref int puiPHMsgLen, [MarshalAs(UnmanagedType.LPArray)] byte[] pucFPMsg, ref int puiFPMsgLen);
//蜂鸣器
[DllImport("libtmz", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
public static extern int ICReaderBeep(long icdev, int uMsec, int uDelay, int uNum);
[DllImport("libtmz", EntryPoint = "ICReaderReadDevSnr", SetLastError = true,
CharSet = CharSet.Auto, ExactSpelling = false,
CallingConvention = CallingConvention.StdCall)]
//说明: 获取设备序列号
public static extern int ICReaderReadDevSnr(int icdev, int rLen, [MarshalAs(UnmanagedType.LPArray)] byte[] pVer);
[DllImport("libtmz", EntryPoint = "ICReaderGetVer", SetLastError = true,
CharSet = CharSet.Auto, ExactSpelling = false,
CallingConvention = CallingConvention.StdCall)]
//说明: 获取设备版本号
public static extern int ICReaderGetVer(int icdev, [MarshalAs(UnmanagedType.LPArray)] byte[] pVer);
//说明: 解析身份证文字信息
[DllImport("libtmz", EntryPoint = "ParseIDCardInfo", SetLastError = true,
CharSet = CharSet.Auto, ExactSpelling = false,
CallingConvention = CallingConvention.StdCall)]
public static extern int ParseIDCardInfo(byte[] pucCHMsg, int puiCHMsgLen, ref int iType, [MarshalAs(UnmanagedType.LPArray)] byte[] pSFZInfo);
//说明: 解析身份证照片信息
[DllImport("libtmz", EntryPoint = "ParseIDCardPic", SetLastError = true,
CharSet = CharSet.Auto, ExactSpelling = false,
CallingConvention = CallingConvention.StdCall)]
public static extern int ParseIDCardPic(byte[] pucPHMsg, int puiPHMsgLen, int iBmpFormat, byte[] pBmpFile);
[DllImport("libtmz", EntryPoint = "Hex2Asc", SetLastError = true,
CharSet = CharSet.Auto, ExactSpelling = false,
CallingConvention = CallingConvention.StdCall)]
//说明: 16进制 转ASCII字符串
public static extern int Hex2Asc(byte[] hex, [MarshalAs(UnmanagedType.LPArray)] byte[] asc, int hexlen);
[DllImport("libtmz", EntryPoint = "ReadIDCardBaseInfo", SetLastError = true,
CharSet = CharSet.Auto, ExactSpelling = false,
CallingConvention = CallingConvention.StdCall)]
public static extern int ReadIDCardBaseInfo(int icdev, int iFlag, int iBmpFormat, byte[] pBmpFile, ref int iType, [MarshalAs(UnmanagedType.LPArray)] byte[] pSFZInfo, [MarshalAs(UnmanagedType.LPArray)] byte[] pFingerInfo);
}