c# 新中新二代身份证阅读,包含头像

2021-04-15 15:30

阅读:635

标签:last   一个   sed   lob   save   byte   ror   ppm   thread   

需要用到的函数:

技术分享图片技术分享图片
        /************************端口类API *************************/
        [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_SetMaxRFByte", CharSet = CharSet.Ansi, SetLastError = true)]
        public static extern int Syn_SetMaxRFByte(int iPort, byte ucByte, int iIfOpen);

        [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_GetCOMBaud", CharSet = CharSet.Ansi, SetLastError = true)]
        public static extern int Syn_GetCOMBaud(int iPort, ref uint puiBaudRate);

        [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_SetCOMBaud", CharSet = CharSet.Ansi, SetLastError = true)]
        public static extern int Syn_SetCOMBaud(int iPort, uint uiCurrBaud, uint uiSetBaud);

        [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_OpenPort", CharSet = CharSet.Ansi, SetLastError = true)]
        public static extern int Syn_OpenPort(int iPort);

        [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_ClosePort", CharSet = CharSet.Ansi, SetLastError = true)]
        public static extern int Syn_ClosePort(int iPort);

        /**************************SAM类函数 **************************/
        [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_ResetSAM", CharSet = CharSet.Ansi, SetLastError = true)]
        public static extern int Syn_ResetSAM(int iPort, int iIfOpen);

        [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_GetSAMStatus", CharSet = CharSet.Ansi, SetLastError = true)]
        public static extern int Syn_GetSAMStatus(int iPort, int iIfOpen);

        [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_GetSAMID", CharSet = CharSet.Ansi, SetLastError = true)]
        public static extern int Syn_GetSAMID(int iPort, ref byte pucSAMID, int iIfOpen);

        [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_GetSAMIDToStr", CharSet = CharSet.Ansi, SetLastError = true)]
        public static extern int Syn_GetSAMIDToStr(int iPort, ref byte pcSAMID, int iIfOpen);

        /*************************身份证卡类函数 ***************************/
        [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_StartFindIDCard", CharSet = CharSet.Ansi, SetLastError = true)]
        public static extern int Syn_StartFindIDCard(int iPort, ref byte pucIIN, int iIfOpen);

        [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_SelectIDCard", CharSet = CharSet.Ansi, SetLastError = true)]
        public static extern int Syn_SelectIDCard(int iPort, ref byte pucSN, int iIfOpen);

        [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_ReadBaseMsg", CharSet = CharSet.Ansi, SetLastError = true)]
        public static extern int Syn_ReadBaseMsg(int iPort, IntPtr pucCHMsg, ref uint puiCHMsgLen, IntPtr pucPHMsg,
            ref uint puiPHMsgLen, int iIfOpen);

        [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_ReadIINSNDN", CharSet = CharSet.Ansi, SetLastError = true)]
        public static extern int Syn_ReadIINSNDN(int iPort, ref byte pucIINSNDN, int iIfOpen);

        [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_ReadBaseMsgToFile", CharSet = CharSet.Ansi,
            SetLastError = true)]
        public static extern int Syn_ReadBaseMsgToFile(int iPort, ref byte pcCHMsgFileName, ref uint puiCHMsgFileLen,
            ref byte pcPHMsgFileName, ref uint puiPHMsgFileLen, int iIfOpen);

        [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_ReadIINSNDNToASCII", CharSet = CharSet.Ansi,
            SetLastError = true)]
        public static extern int Syn_ReadIINSNDNToASCII(int iPort, ref byte pucIINSNDN, int iIfOpen);

        [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_ReadNewAppMsg", CharSet = CharSet.Ansi, SetLastError = true)]
        public static extern int Syn_ReadNewAppMsg(int iPort, ref byte pucAppMsg, ref uint puiAppMsgLen, int iIfOpen);

        [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_GetBmp", CharSet = CharSet.Ansi, SetLastError = true)]
        public static extern int Syn_GetBmp(string Wlt_File, int iPort);

        [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_ReadMsg", CharSet = CharSet.Ansi, SetLastError = true)]
        public static extern int Syn_ReadMsg(int iPortID, int iIfOpen, ref IDCardData pIDCardData);

        [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_FindReader", CharSet = CharSet.Ansi, SetLastError = true)]
        public static extern int Syn_FindReader();

        /***********************设置附加功能函数 ************************/
        [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_SetPhotoPath", CharSet = CharSet.Ansi, SetLastError = true)]
        public static extern int Syn_SetPhotoPath(int iOption, ref byte cPhotoPath);

        [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_SetPhotoType", CharSet = CharSet.Ansi, SetLastError = true)]
        public static extern int Syn_SetPhotoType(int iType);

        [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_SetPhotoName", CharSet = CharSet.Ansi, SetLastError = true)]
        public static extern int Syn_SetPhotoName(int iType);

        [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_SetSexType", CharSet = CharSet.Ansi, SetLastError = true)]
        public static extern int Syn_SetSexType(int iType);

        [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_SetNationType", CharSet = CharSet.Ansi, SetLastError = true)]
        public static extern int Syn_SetNationType(int iType);

        [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_SetBornType", CharSet = CharSet.Ansi, SetLastError = true)]
        public static extern int Syn_SetBornType(int iType);

        [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_SetUserLifeBType", CharSet = CharSet.Ansi,
            SetLastError = true)]
        public static extern int Syn_SetUserLifeBType(int iType);

        [DllImport("SynIDCardAPI.dll", EntryPoint = "Syn_SetUserLifeEType", CharSet = CharSet.Ansi,
            SetLastError = true)]
        public static extern int Syn_SetUserLifeEType(int iType, int iOption);

        [DllImport("WltRS.dll", CallingConvention = CallingConvention.StdCall, SetLastError = true)]
        static extern int GetBmp(string pucPHMsg, int intf);
函数

存储身份证数据的类:

技术分享图片技术分享图片
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
    public struct IDCardData
    {
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
        [JsonProperty("name")]
        public string Name; //姓名   
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 6)] 
        [JsonProperty("sex")] 
        public string Sex; //性别
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 20)]
        [JsonProperty("nation")] 
        public string Nation; //名族
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 18)]
        [JsonProperty("born")] 
        public string Born; //出生日期
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 72)]
        [JsonProperty("address")] 
        public string Address; //住址
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 38)] 
        [JsonProperty("idCardNo")] 
        public string IDCardNo; //身份证号
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)] 
        [JsonProperty("grantDept")] 
        public string GrantDept; //发证机关

        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 18)] 
        [JsonProperty("userLifeBegin")]
        public string UserLifeBegin; // 有效开始日期

        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 18)]
        [JsonProperty("userLifeEnd")]
        public string UserLifeEnd; // 有效截止日期

        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 38)]
        [JsonIgnore] 
        public string reserved; // 保留
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 255)] 
        [JsonProperty("photeBase64Str")]
        public string PhotoFileName; // 照片路径
    }
data code

具体方法:

技术分享图片技术分享图片
        private static string _imageDirPath = Path.Combine(CommonConst.DirPath, "CardImg");

        public static bool Read(ref IDCardData idCardData)
        {
            if (!Directory.Exists(_imageDirPath))
            {
                Directory.CreateDirectory(_imageDirPath);
            }
            bool isTrue = false;
            try
            {
                //先查找读卡器
                int i, nRet;
                i = Syn_FindReader();
                //只支持usb
                if (i > 1000)
                {
                    var nPort = i;
                    if (Syn_OpenPort(nPort) == 0)
                    {
                        Thread.Sleep(5);
                        if (Syn_SetMaxRFByte(nPort, 80, 0) == 0)
                        {
                            Thread.Sleep(5);
                            byte[] pucIIN = new byte[4];
                            byte[] pucSN = new byte[8];
                            IDCardData cardMsg = new IDCardData();
                            nRet = Syn_StartFindIDCard(nPort, ref pucIIN[0], 0); //找卡
                            nRet = Syn_SelectIDCard(nPort, ref pucSN[0], 0); //选卡
                            string cardStr = new string( , 256); //身份证基本信息返回长度为256
                            string imgMsg = new string( , 1024); //身份证图片信息返回长度为1024
                            IntPtr msg = Marshal.StringToHGlobalAnsi(cardStr); //身份证基本信息
                            IntPtr img = Marshal.StringToHGlobalAnsi(imgMsg); //身份证图片
                            uint mLen = 0;
                            uint iLen = 0;
                            nRet = Syn_ReadBaseMsg(nPort, msg, ref mLen, img, ref iLen, 0);
                            if (nRet == 0)
                            {
                                Syn_ClosePort(nPort);
                                string card = Marshal.PtrToStringUni(msg);
                                char[] cartb = card.ToCharArray();
                                cardMsg.Name = (new string(cartb, 0, 15)).Trim();
                                cardMsg.Sex = new string(cartb, 15, 1);
                                cardMsg.Nation = new string(cartb, 16, 2);
                                cardMsg.Born = new string(cartb, 18, 8);
                                cardMsg.Address = (new string(cartb, 26, 35)).Trim();
                                cardMsg.IDCardNo = new string(cartb, 61, 18);
                                cardMsg.GrantDept = (new string(cartb, 79, 15)).Trim();
                                cardMsg.UserLifeBegin = new string(cartb, 94, 8);
                                cardMsg.UserLifeEnd = new string(cartb, 102, 8);
                                byte[] data=new byte[iLen];
                                Marshal.Copy(img,data,0,data.Length);
                                cardMsg.PhotoFileName = GetImageBase64Str(data, cardMsg.IDCardNo);
                                idCardData = cardMsg;
                                isTrue = true;
                            }
                        }
                        Syn_ClosePort(nPort);
                    }
                }
            }
            catch (Exception e)
            {
                LogHelper.WriteLog("读取身份证失败", e);
                isTrue = false;
            }
            return isTrue;
        }

        private static string GetImageBase64Str(byte[] data, string idCardNo)
        {
            var wltFileName = Path.Combine(_imageDirPath,
                string.Format("{0}.wlt", idCardNo));
            if (File.Exists(wltFileName))
            {
                File.Delete(wltFileName);
            }
            using (FileStream fs = new FileStream(wltFileName, FileMode.Create))
            {
                fs.Write(data, 0, data.Length);
                fs.Close();
            }
           // Thread.Sleep(200);
            var nRet = GetBmp(wltFileName, 2);
            if (nRet == 1)
            {
              //  Thread.Sleep(200);
                string bmpFileName = Path.Combine(_imageDirPath,
                    string.Format("{0}.bmp", idCardNo));
                if (File.Exists(bmpFileName))
                {
                    var str = GetFileBase64Str(bmpFileName);
                    File.Delete(bmpFileName);
                    return str;
                }
            }
            return string.Empty;
        }

        private static string GetFileBase64Str(string fileName)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                using (Bitmap bmp = new Bitmap(fileName))
                {
                    bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
                }
                byte[] arr1 = new byte[ms.Length];
                ms.Position = 0;
                ms.Read(arr1, 0, (int) ms.Length);
                return Convert.ToBase64String(arr1);
            }
            using (FileStream fs=new FileStream(fileName,FileMode.Open))
            {
                byte[] bt = new byte[fs.Length];

                //调用read读取方法  
                fs.Read(bt, 0, bt.Length);
               return Convert.ToBase64String(bt);  
            }
        }
View Code

最终结果是将信息存储IDCardData结构体中,图片信息转换为base64str。

注意:WltRS.dll一定要很新,我以前用的是2007年的,在转换头像的时候总是报错,找了一个新的,是2012年的,就好了。

c# 新中新二代身份证阅读,包含头像

标签:last   一个   sed   lob   save   byte   ror   ppm   thread   

原文地址:https://www.cnblogs.com/sczmzx/p/8868391.html


评论


亲,登录后才可以留言!