Beispiel #1
0
        /// <summary>
        /// 打开端口
        /// </summary>
        /// <returns></returns>
        //public static int Open()
        //{
        //    if (!IsUse) return 144;
        //    try
        //    {
        //        int ret = clsICCard.SDT_OpenPort(int.Parse(Port.Substring(3)));
        //        if (ret == 0)
        //        {
        //            AppLog.Write("[Identifier][Open]" + 0.ToString(), AppLog.LogMessageType.Warn);
        //            clsICCard.SDT_ClosePort(int.Parse(Port.Substring(3)));
        //        }
        //        else if (ret != 144)
        //        {
        //            AppLog.Write("[Identifier][Open]" + ret.ToString(), AppLog.LogMessageType.Warn);
        //        }
        //        return ret;
        //    }
        //    catch (System.Exception e)
        //    {
        //        AppLog.Write("[Identifier][InitPort]Error!\n", AppLog.LogMessageType.Error, e);
        //        return -1;
        //    }
        //}

        //public static void Close()
        //{
        //    if (!IsUse) return;
        //    try
        //    {
        //        clsICCard.SDT_ClosePort(int.Parse(Port.Substring(3)));
        //    }
        //    catch (System.Exception ex)
        //    {
        //        AppLog.Write("[Identifier][Close]Error!\n", AppLog.LogMessageType.Error, ex);
        //        return;
        //    }

        //}

        /// <summary>
        /// 读身份证
        /// </summary>
        /// <param name="objEDZ"></param>
        /// <param name="existPic"></param>
        /// <param name="picName"></param>
        /// <returns></returns>
        public static bool ReadIdCard(ref clsEDZ objEDZ, ref bool existPic, ref string picName)
        {
            if (!IsUse)
            {
                objEDZ.ADDRESS      = "北京";
                objEDZ.BIRTH        = Utility.String2Datetime("20000101000000");
                objEDZ.IDC          = "350198099889101098";
                objEDZ.Name         = "刘备";
                objEDZ.Sex_CName    = "男";
                objEDZ.NATION_CName = "中国";
                existPic            = false;
                return(true);
            }
            try
            {
                clsICCard card = new clsICCard();
                string    name = "";
                if (card.ReadICCard(ref name))
                {
                    objEDZ = card.objEDZ;
                    if (File.Exists(name))
                    {
                        existPic = true;
                        picName  = name;
                    }
                    else
                    {
                        existPic = false;
                    }
                    return(true);
                }
                else
                {
                    AppLog.Write("[Identifier][ReadIdCard]Failed!", AppLog.LogMessageType.Warn);
                    return(false);
                }
            }
            catch (System.Exception e)
            {
                AppLog.Write("[Identifier][ReadIdCard]Error!\n", AppLog.LogMessageType.Error, e);
                return(false);
            }
        }
Beispiel #2
0
        public bool ReadICCard(ref string bmpfileName)
        {
            bool bUsbPort = false;
            //int intOpenPortRtn = 0;
            int rtnTemp     = 0;
            int pucIIN      = 0;
            int pucSN       = 0;
            int puiCHMsgLen = 0;
            int puiPHMsgLen = 0;


            objEDZ     = new clsEDZ();
            EdziPortID = int.Parse(Identifier.Port.Substring(3));
            ////检测usb口的机具连接,必须先检测usb
            //for (int iPort = 1001; iPort <= 1016; iPort++)
            //{
            //    intOpenPortRtn = SDT_OpenPort(iPort);
            //    if (intOpenPortRtn == 144)
            //    {
            //        EdziPortID = iPort;
            //        bUsbPort = true;
            //        break;
            //    }
            //}
            ////检测串口的机具连接
            //if (!bUsbPort)
            //{
            //    for (int iPort = 1; iPort <= 8; iPort++)
            //    {
            //        intOpenPortRtn = SDT_OpenPort(iPort);
            //        if (intOpenPortRtn == 144)
            //        {
            //            EdziPortID = iPort;
            //            bUsbPort = false;
            //            break;
            //        }
            //    }
            //}
            //if (intOpenPortRtn != 144)
            //{
            //    throw new Exception("端口打开失败,请检测相应的端口或者重新连接读卡器!");
            //    return false;
            //}
            //在这里,如果您想下一次不用再耗费检查端口的检查的过程,您可以把 EdziPortID 保存下来,可以保存在注册表中,也可以保存在配置文件中,我就不多写了,但是,
            //您要考虑机具连接端口被用户改变的情况哦

            //下面找卡
            rtnTemp = SDT_StartFindIDCard(EdziPortID, ref pucIIN, EdziIfOpen);
            if (rtnTemp != 159)
            {
                rtnTemp = SDT_StartFindIDCard(EdziPortID, ref pucIIN, EdziIfOpen);  //再找卡
                if (rtnTemp != 159)
                {
                    //AppLog.Write("未放卡或者卡未放好,请重新放卡!", AppLog.LogMessageType.Error);
                    //rtnTemp = SDT_ClosePort(EdziPortID);
                    //throw new Exception("未放卡或者卡未放好,请重新放卡!");
                    return(false);
                }
            }
            //选卡
            rtnTemp = SDT_SelectIDCard(EdziPortID, ref pucSN, EdziIfOpen);
            if (rtnTemp != 144)
            {
                rtnTemp = SDT_SelectIDCard(EdziPortID, ref pucSN, EdziIfOpen);  //再选卡
                if (rtnTemp != 144)
                {
                    AppLog.Write("读卡失败!", AppLog.LogMessageType.Error);
                    //rtnTemp = SDT_ClosePort(EdziPortID);
                    //throw new Exception("读卡失败!");
                    return(false);
                }
            }
            //注意,在这里,用户必须有应用程序当前目录的读写权限
            if (!Directory.Exists("IDCardInfo"))
            {
                Directory.CreateDirectory("IDCardInfo");
            }
            string FilePreName = DateTime.Now.ToString("MMddHHmmss");
            string txtName     = Path.Combine("IDCardInfo", FilePreName + ".txt");
            string wltName     = Path.Combine("IDCardInfo", FilePreName + ".wlt");
            string bmpName     = Path.Combine("IDCardInfo", FilePreName + ".bmp");

            bmpfileName = "IDCardInfo/" + FilePreName + ".bmp";
            //FileInfo objFile = new FileInfo("wz.txt");
            //if (objFile.Exists)
            //{
            //    objFile.Attributes = FileAttributes.Normal;
            //    objFile.Delete();
            //}
            //if (objFile.Exists)
            //{
            //    AppLog.Write("Delete \"wz.txt\" Failed!", AppLog.LogMessageType.Warn);
            //    return false;
            //}
            //objFile = new FileInfo("zp.bmp");
            //if (objFile.Exists)
            //{
            //    objFile.Attributes = FileAttributes.Normal;
            //    objFile.Delete();
            //}
            //if (objFile.Exists)
            //{
            //    AppLog.Write("Delete \"zp.bmp\" Failed!", AppLog.LogMessageType.Warn);
            //    return false;
            //}
            //objFile = new FileInfo("zp.wlt");
            //if (objFile.Exists)
            //{
            //    objFile.Attributes = FileAttributes.Normal;
            //    objFile.Delete();
            //}
            //if (objFile.Exists)
            //{
            //    AppLog.Write("Delete \"zp.wlt\" Failed!", AppLog.LogMessageType.Warn);
            //    return false;
            //}
            rtnTemp = SDT_ReadBaseMsgToFile(EdziPortID, txtName, ref puiCHMsgLen, wltName, ref puiPHMsgLen, EdziIfOpen);
            //rtnTemp = SDT_ReadBaseMsgToFile(EdziPortID, "wz.txt", ref puiCHMsgLen, "zp.wlt", ref puiPHMsgLen, EdziIfOpen);
            //rtnTemp = SDT_ReadBaseMsg(EdziPortID, "wz.txt", ref puiCHMsgLen, "zp.wlt", ref puiPHMsgLen, EdziIfOpen);
            if (rtnTemp != 144)
            {
                AppLog.Write("读卡失败!", AppLog.LogMessageType.Error);
                //rtnTemp = SDT_ClosePort(EdziPortID);
                //throw new Exception("读卡失败!");
                return(false);
            }
            //下面解析照片,注意,如果在C盘根目录下没有机具厂商的授权文件Termb.Lic,照片解析将会失败
            if (bUsbPort)
            {
                rtnTemp = GetBmp(wltName, 2);
            }
            //rtnTemp = GetBmp("zp.wlt", 2);
            else
            {
                rtnTemp = GetBmp(wltName, 1);
            }
            //rtnTemp = GetBmp("zp.wlt", 1);
            switch (rtnTemp)
            {
            case 0:
                //MessageBox.Show("调用sdtapi.dll错误!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                AppLog.Write("调用sdtapi.dll错误!", AppLog.LogMessageType.Error);
                return(false);

            //throw new Exception("调用sdtapi.dll错误!");
            //break;
            case 1:       //正常
                break;

            case -1:
                //MessageBox.Show("相片解码错误!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                AppLog.Write("相片解码错误!", AppLog.LogMessageType.Error);
                return(false);

            //throw new Exception("相片解码错误!");
            //break;
            case -2:
                //MessageBox.Show("wlt文件后缀错误!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                AppLog.Write("wlt文件后缀错误!", AppLog.LogMessageType.Error);
                return(false);

            //throw new Exception("wlt文件后缀错误!");
            //break;
            case -3:
                //MessageBox.Show("wlt文件打开错误!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                AppLog.Write("wlt文件打开错误!", AppLog.LogMessageType.Error);
                return(false);

            //throw new Exception("wlt文件打开错误!");
            //break;
            case -4:
                //MessageBox.Show("wlt文件格式错误!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                AppLog.Write("wlt文件格式错误!", AppLog.LogMessageType.Error);
                return(false);

            //throw new Exception("wlt文件格式错误!");
            //break;
            case -5:
                //MessageBox.Show("软件未授权!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                AppLog.Write("软件未授权!", AppLog.LogMessageType.Error);
                return(false);

            //throw new Exception("软件未授权!");
            //break;
            case -6:
                //MessageBox.Show("设备连接错误!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                AppLog.Write("设备连接错误!", AppLog.LogMessageType.Error);
                return(false);
                //throw new Exception("设备连接错误!");
                //break;
            }
            //rtnTemp = SDT_ClosePort(EdziPortID);
            FileInfo   f  = new FileInfo(txtName);
            FileStream fs = f.OpenRead();

            byte[] bt = new byte[fs.Length];
            fs.Read(bt, 0, (int)fs.Length);
            fs.Close();

            string str = System.Text.UnicodeEncoding.Unicode.GetString(bt);

            objEDZ.Name        = System.Text.UnicodeEncoding.Unicode.GetString(bt, 0, 30).Trim();
            objEDZ.Sex_Code    = System.Text.UnicodeEncoding.Unicode.GetString(bt, 30, 2).Trim();
            objEDZ.NATION_Code = System.Text.UnicodeEncoding.Unicode.GetString(bt, 32, 4).Trim();
            string strBird = System.Text.UnicodeEncoding.Unicode.GetString(bt, 36, 16).Trim();

            objEDZ.BIRTH   = Convert.ToDateTime(strBird.Substring(0, 4) + "年" + strBird.Substring(4, 2) + "月" + strBird.Substring(6) + "日");
            objEDZ.ADDRESS = System.Text.UnicodeEncoding.Unicode.GetString(bt, 52, 70).Trim();
            objEDZ.IDC     = System.Text.UnicodeEncoding.Unicode.GetString(bt, 122, 36).Trim();
            objEDZ.REGORG  = System.Text.UnicodeEncoding.Unicode.GetString(bt, 158, 30).Trim();
            string strTem = System.Text.UnicodeEncoding.Unicode.GetString(bt, 188, bt.GetLength(0) - 188).Trim();

            objEDZ.STARTDATE = Convert.ToDateTime(strTem.Substring(0, 4) + "年" + strTem.Substring(4, 2) + "月" + strTem.Substring(6, 2) + "日");
            strTem           = strTem.Substring(8);
            if (strTem.Trim() != "长期")
            {
                objEDZ.ENDDATE = Convert.ToDateTime(strTem.Substring(0, 4) + "年" + strTem.Substring(4, 2) + "月" + strTem.Substring(6, 2) + "日");
            }
            else
            {
                objEDZ.ENDDATE = DateTime.MaxValue;
            }
            File.Delete(txtName);
            if (File.Exists(txtName))
            {
                AppLog.Write("删除" + txtName + "文件失败!", AppLog.LogMessageType.Error);
            }

            //FileInfo objFile = new FileInfo(bmpName);
            //if (!objFile.Exists)
            //{
            //    AppLog.Write("照片解析失败,找不到照片文件!", AppLog.LogMessageType.Error);
            //    return false;
            //}
            //if (objFile.Exists)
            //{

            //    FileStream fss = new FileStream("zp.bmp", FileMode.Open);
            //    byte[] imgbyte = new byte[(int)objFile.Length];
            //    fss.Read(imgbyte, 0, (int)objFile.Length);
            //    fss.Close();
            //    MemoryStream ms = new MemoryStream(imgbyte);
            //    Image img = Image.FromStream(ms);
            //    objEDZ.PIC_Image = (Image)img.Clone();
            //    objEDZ.PIC_Byte = imgbyte;
            //    img.Dispose();
            //    ms.Dispose();
            //}

            //ReadICCardComplete(objEDZ);
            return(true);
        }