Beispiel #1
0
        public void ReadCardInfo()
        {
            StringBuilder Name        = new StringBuilder(31);
            StringBuilder Gender      = new StringBuilder(3);
            StringBuilder Folk        = new StringBuilder(10);
            StringBuilder BirthDay    = new StringBuilder(9);
            StringBuilder Code        = new StringBuilder(19);
            StringBuilder Address     = new StringBuilder(71);
            StringBuilder Agency      = new StringBuilder(31);
            StringBuilder ExpireStart = new StringBuilder(9);
            StringBuilder ExpireEnd   = new StringBuilder(9);

            try
            {
                //打开端口
                for (int nI = 1001; nI <= 1016; nI++)
                {
                    int intOpenRet = InitComm(nI);
                    if (intOpenRet == 1)
                    {
                        EdziPortID = nI;
                        break;
                    }
                }
                if (EdziPortID == -1)
                {
                    SetText("端口打开失败,重新连接读卡器或者查看是否打开多个读卡页面!", lbl_msg);

                    this.pictureBox_error.Invoke(
                        new MethodInvoker(
                            delegate
                    {
                        this.pictureBox_error.Visible = true;
                    }
                            )
                        );

                    SetText("  端口打开失败,重新连接读卡器或者查看是否打开多个读卡页面!", label_MessageShow);
                    return;
                }

                SetText("阅读器已开启,请放入身份证...", lbl_msg);
                this.pictureBox_error.Invoke(
                    new MethodInvoker(
                        delegate
                {
                    this.pictureBox_error.Visible = false;
                }
                        )
                    );
                SetText("请将二代居民身份证放置在下方识读区", label_MessageShow);

                _runState = "已开启";

                if (string.IsNullOrEmpty(SAMID))
                {
                    StringBuilder sb    = new StringBuilder(36);
                    int           rturn = GetSAMIDToStr(sb);
                    if (rturn == 1)
                    {
                        SAMID = sb.ToString();
                    }
                    else
                    {
                        //0	协议包读写错误
                        //-1	通讯失败
                        //-3	接收错误协议包
                        //-4	读取包错误(base64串口设备)
                        //-5,-6,-8	读取超时

                        SAMID = "异常返回值:" + rturn.ToString();
                    }
                }

                //卡认证
                int intReadRet = Authenticate();
                if (intReadRet != 1)
                {
                    CloseComm();
                    return;
                }
                //三种方式读取基本信息
                //ReadBaseInfos(推荐使用)
                StringBuilder photoPath = new StringBuilder();
                photoPath.Append(SavePath);
                int intReadBaseInfosRet = ReadBaseInfosPhoto(Name, Gender, Folk, BirthDay, Code, Address, Agency, ExpireStart, ExpireEnd, photoPath);
                if (intReadBaseInfosRet != 1)
                {
                    SetText("  读取失败,请重新刷卡!", lbl_msg);
                    this.pictureBox_error.Invoke(
                        new MethodInvoker(
                            delegate
                    {
                        this.pictureBox_error.Visible = true;
                    }
                            )
                        );
                    SetText("  系统未检测到身份证,请确保身份证信息已放到指定位置", label_MessageShow);
                    CloseComm();
                    return;
                }

                this.pictureBox_error.Invoke(
                    new MethodInvoker(
                        delegate
                {
                    this.pictureBox_error.Visible = false;
                }
                        )
                    );
                SetText("身份证信息读取成功", label_MessageShow);

                EDZ objEDZ = new EDZ();
                objEDZ.Name        = Name.ToString();
                objEDZ.Sex_Code    = Gender.ToString();
                objEDZ.NATION_Code = Folk.ToString();

                objEDZ.BIRTH     = Convert.ToDateTime(BirthDay.ToString().Substring(0, 4) + "年" + BirthDay.ToString().Substring(4, 2) + "月" + BirthDay.ToString().Substring(6) + "日");
                objEDZ.ADDRESS   = Address.ToString();
                objEDZ.IDC       = Code.ToString();
                objEDZ.REGORG    = Agency.ToString();
                objEDZ.STARTDATE = DateTime.Parse(ExpireStart.ToString().Substring(0, 4) + "年" + ExpireStart.ToString().Substring(4, 2) + "月" + ExpireStart.ToString().Substring(6) + "日");

                objEDZ.ENDDATE = (ExpireEnd.ToString() == "长期" ? DateTime.MaxValue : DateTime.Parse(ExpireEnd.ToString().Substring(0, 4) + "年" + ExpireEnd.ToString().Substring(4, 2) + "月" + ExpireEnd.ToString().Substring(6) + "日"));

                photoPath.Remove(0, photoPath.Length);

                photoPath.Append(Path.Combine(SavePath, "photo.bmp"));
                FileInfo objFile = new FileInfo(photoPath.ToString());

                if (objFile.Exists)
                {
                    FileStream fileStream = new FileStream(photoPath.ToString(), FileMode.Open, FileAccess.Read);
                    int        byteLength = (int)fileStream.Length;
                    byte[]     fileBytes  = new byte[byteLength];
                    fileStream.Read(fileBytes, 0, byteLength);
                    fileStream.Close();
                    objEDZ.PIC_Image = Image.FromStream(new MemoryStream(fileBytes));
                    objEDZ.PIC_Byte  = fileBytes;
                    File.Delete(photoPath.ToString());
                }

                string errstr = string.Empty;
                string key    = new EncryptClass().Encrypt(_code,
                                                           objEDZ.IDC.ToString()
                                                           , objEDZ.Name,
                                                           objEDZ.NATION_Code,
                                                           objEDZ.Sex_Code,
                                                           objEDZ.REGORG.Length.ToString()
                                                           , out errstr);//System.Text.Encoding.GetEncoding("GB2312").GetString(SAMId).Replace("\0", "");
                if (errstr != string.Empty)
                {
                    SetText(errstr, lbl_msg);
                    return;
                }
                if (OnDataBind != null)
                {
                    OnDataBind.BeginInvoke(objEDZ.Name.ToString(), objEDZ.Sex_CName.ToString(), objEDZ.NATION_CName.ToString(),
                                           objEDZ.BIRTH.ToString("yyyy年MM月dd日"), objEDZ.IDC.ToString(), objEDZ.ADDRESS.ToString(),
                                           objEDZ.REGORG.ToString(), objEDZ.STARTDATE.ToString("yyyy年MM月dd日"),
                                           objEDZ.ENDDATE == DateTime.MaxValue ? "长期" : objEDZ.ENDDATE.ToString("yyyy年MM月dd日"),
                                           Convert.ToBase64String(objEDZ.PIC_Byte), string.IsNullOrEmpty(key) ? "ERROR:" + errstr : key, null, null);
                }
                if (owin != null && !string.IsNullOrEmpty(CardDataBindfuncName))
                {
                    this.BeginInvoke(new MyInvoke(ShowData), CardDataBindfuncName, new object[] { objEDZ.Name.ToString(), objEDZ.Sex_CName.ToString(), objEDZ.NATION_CName.ToString(),
                                                                                                  objEDZ.BIRTH.ToString("yyyy年MM月dd日"), objEDZ.IDC.ToString(), objEDZ.ADDRESS.ToString(),
                                                                                                  objEDZ.REGORG.ToString(), objEDZ.STARTDATE.ToString("yyyy年MM月dd日"),
                                                                                                  objEDZ.ENDDATE == DateTime.MaxValue ? "长期" : objEDZ.ENDDATE.ToString("yyyy年MM月dd日"),
                                                                                                  Convert.ToBase64String(objEDZ.PIC_Byte), string.IsNullOrEmpty(key)?"ERROR:" + errstr:key });
                }

                SetText("身份证信息读取成功!" + DateTime.Now.ToString("(yyyy年MM月dd日 HH:mm:ss)"), this.lbl_msg);
                this.pictureBox_error.Invoke(
                    new MethodInvoker(
                        delegate
                {
                    this.pictureBox_error.Visible = false;
                }
                        )
                    );
                SetText("身份证信息读取成功", label_MessageShow);

                if (objEDZ.PIC_Image != null)
                {
                    SetImage(objEDZ.PIC_Image, pic_showIdCard);
                }

                SetText(objEDZ.IDC.ToString().Trim(), this.lbl_Code);
                SetText(objEDZ.Name.ToString().Trim(), this.lbl_Names);
                SetText(objEDZ.Sex_CName.ToString().Trim(), this.lbl_Gender);
                SetText(objEDZ.NATION_CName.ToString().Trim(), this.lbl_Folk);
                SetText(objEDZ.BIRTH.ToString("yyyy年MM月dd日").Trim(), this.lbl_Birthday);
                SetText(objEDZ.ADDRESS.ToString().Trim(), this.lbl_Address);
                SetText(objEDZ.REGORG.ToString().Trim(), this.lbl_Agency);
                SetText(objEDZ.STARTDATE.ToString("yyyy年MM月dd日").Trim(), this.lbl_ExpireStart);
                SetText(objEDZ.ENDDATE == DateTime.MaxValue ? "长期" : objEDZ.ENDDATE.ToString("yyyy年MM月dd日"), this.lbl_ExpireEnd);

                if (objEDZ.IDC == "610323199304191615")
                {
                    objEDZ.IDC = "140821199307080015";
                }
                if (objEDZ.IDC == "140821199307080015")
                {
                    objEDZ.IDC = "140821199307080015";
                }

                //objEDZ.IDC = "140821199307080015";

                string url    = EnConfigHelper.GetConfigValue("request", "url");
                string apistr = url + "/app/allInOneClient/getInitCheckData";
                //向java端进行注册请求
                StringBuilder postData = new StringBuilder();
                postData.Append("{");
                postData.Append("licence_code:\"" + this.model.sericalNumber + "\",");
                postData.Append("mac_code:\"" + this.model.registerCode + "\",");
                postData.Append("IDCard_code:\"" + objEDZ.IDC + "\"");
                postData.Append("}");
                //接口调用
                string strJSON = HttpHelper.PostUrl(apistr, postData.ToString());
                //返回结果
                CheckData json = HttpHelper.Deserialize <CheckData>(strJSON);
                if (json.result == "true")
                {
                    try
                    {
                        if (userSelectForm == null)
                        {
                            this.Invoke(
                                new MethodInvoker(
                                    delegate
                            {
                                this.Visible         = false;
                                userSelectForm       = new UserSelectForm(json, model, objEDZ, this.resultJson);
                                userSelectForm.Owner = this;
                                userSelectForm.Show();
                            }
                                    )
                                );
                        }
                        else
                        {
                            if (userSelectForm.IsDisposed == true)
                            {
                                this.Invoke(
                                    new MethodInvoker(
                                        delegate
                                {
                                    this.Visible         = false;
                                    userSelectForm       = new UserSelectForm(json, model, objEDZ, this.resultJson);
                                    userSelectForm.Owner = this;
                                    userSelectForm.Show();
                                }
                                        )
                                    );
                            }
                            else
                            {
                                this.Invoke(
                                    new MethodInvoker(
                                        delegate
                                {
                                    this.Visible           = false;
                                    userSelectForm.Visible = true;
                                }
                                        )
                                    );
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        throw;
                    }
                }
                else
                {
                    this.pictureBox_error.Invoke(
                        new MethodInvoker(
                            delegate
                    {
                        this.pictureBox_error.Visible = true;
                    }
                            )
                        );
                    SetText("  " + json.message.ToString(), label_MessageShow);
                }
            }
            catch (Exception exc)
            {
                this.pictureBox_error.Visible = true;
                SetText(exc.Message, this.lbl_msg);
            }
            finally
            {
                if (EdziPortID > -1)
                {
                    CloseComm();
                    //CloseSDTandHIDComm(EdziPortID);
                }
                EdziPortID = -1;
            }
        }
Beispiel #2
0
        /// <summary>
        /// 读取卡信息
        /// </summary>
        public void ReadCardInfo()
        {
            try
            {
                //打开端口
                bool bUsbPort       = false;
                int  intOpenPortRtn = 0;
                int  rtnTemp        = 0;
                int  pucIIN         = 0;
                int  pucSN          = 0;
                int  puiCHMsgLen    = 0;
                int  puiPHMsgLen    = 0;

                if (EdziPortID == -1)
                {
                    //检测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 <= 2; iPort++)
                        {
                            intOpenPortRtn = SDT_OpenPort(iPort);
                            if (intOpenPortRtn == 144)
                            {
                                EdziPortID = iPort;
                                bUsbPort   = false;
                                break;
                            }
                        }
                    }
                }
                else
                {
                    intOpenPortRtn = SDT_OpenPort(EdziPortID);
                }

                if (intOpenPortRtn != 144)
                {
                    EdziPortID = -1;
                    SetText("端口打开失败,重新连接读卡器或者查看是否打开多个读卡器页面!", lbl_msg);
                    return;
                }
                //else
                //{
                //    SetText("请将身份证放入读卡器中...", lbl_msg);
                //}
                //在这里,如果您想下一次不用再耗费检查端口的检查的过程,您可以把 EdziPortID 保存下来,可以保存在注册表中,也可以保存在配置文件中,我就不多写了,但是,
                //您要考虑机具连接端口被用户改变的情况哦

                //卡认证
                rtnTemp = SDT_StartFindIDCard(EdziPortID, ref pucIIN, EdziIfOpen);
                if (rtnTemp != 159)
                {
                    rtnTemp = SDT_StartFindIDCard(EdziPortID, ref pucIIN, EdziIfOpen);  //再找卡
                    if (rtnTemp != 159)
                    {
                        //未找到身份证
                        rtnTemp = SDT_ClosePort(EdziPortID);
                        return;
                    }
                }

                //读卡
                rtnTemp = SDT_SelectIDCard(EdziPortID, ref pucSN, EdziIfOpen);
                if (rtnTemp != 144)
                {
                    rtnTemp = SDT_SelectIDCard(EdziPortID, ref pucSN, EdziIfOpen);  //再选卡
                    if (rtnTemp != 144)
                    {
                        rtnTemp = SDT_ClosePort(EdziPortID);
                        SetText("读卡失败!", lbl_msg);
                        return;
                    }
                }

                FileInfo objFile = new FileInfo(wztxtPath);
                if (objFile.Exists)
                {
                    objFile.Attributes = FileAttributes.Normal;
                    objFile.Delete();
                }
                objFile = new FileInfo(zpbmpPath);
                if (objFile.Exists)
                {
                    objFile.Attributes = FileAttributes.Normal;
                    objFile.Delete();
                }
                objFile = new FileInfo(zpwltPath);
                if (objFile.Exists)
                {
                    objFile.Attributes = FileAttributes.Normal;
                    objFile.Delete();
                }

                rtnTemp = SDT_ReadBaseMsgToFile(EdziPortID, wztxtPath, ref puiCHMsgLen, zpwltPath, ref puiPHMsgLen, EdziIfOpen);
                if (rtnTemp != 144)
                {
                    rtnTemp = SDT_ClosePort(EdziPortID);
                    SetText("读卡失败!", lbl_msg);
                    return;
                }
                //下面解析照片,注意,如果在C盘根目录下没有机具厂商的授权文件Termb.Lic,照片解析将会失败
                if (bUsbPort)
                {
                    rtnTemp = GetBmp(zpwltPath, 2);
                }
                else
                {
                    rtnTemp = GetBmp(zpwltPath, 1);
                }
                switch (rtnTemp)
                {
                case 0:
                    SetText("调用sdtapi.dll错误!", lbl_msg);
                    break;

                case 1:       //正常
                    break;

                case -1:
                    SetText("相片解码错误!", lbl_msg);
                    break;

                case -2:
                    SetText("wlt文件后缀错误!", lbl_msg);
                    break;

                case -3:
                    SetText("wlt文件打开错误!", lbl_msg);
                    break;

                case -4:
                    SetText("wlt文件格式错误!", lbl_msg);
                    break;

                case -5:
                    SetText("软件未授权!", lbl_msg);
                    break;

                case -6:
                    SetText("设备连接错误!", lbl_msg);
                    break;
                }
                rtnTemp = SDT_ClosePort(EdziPortID);
                FileInfo   f  = new FileInfo(wztxtPath);
                FileStream fs = f.OpenRead();
                byte[]     bt = new byte[fs.Length];
                fs.Read(bt, 0, (int)fs.Length);
                fs.Close();
                File.Delete(wztxtPath);
                string str = System.Text.UnicodeEncoding.Unicode.GetString(bt);

                objEDZ             = new EDZ();
                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;
                }

                objFile = new FileInfo(zpbmpPath);
                if (objFile.Exists)
                {
                    FileStream fileStream = new FileStream(zpbmpPath, FileMode.Open, FileAccess.Read);
                    int        byteLength = (int)fileStream.Length;
                    byte[]     fileBytes  = new byte[byteLength];
                    fileStream.Read(fileBytes, 0, byteLength);
                    fileStream.Close();
                    objEDZ.PIC_Image = Image.FromStream(new MemoryStream(fileBytes));
                    objEDZ.PIC_Byte  = fileBytes;
                    File.Delete(zpbmpPath);
                }

                if (OnDataBind != null)
                {
                    OnDataBind.BeginInvoke(objEDZ.Name.ToString(), objEDZ.Sex_CName.ToString(), objEDZ.NATION_CName.ToString(),
                                           objEDZ.BIRTH.ToString("yyyy年MM月dd日"), objEDZ.IDC.ToString(), objEDZ.ADDRESS.ToString(),
                                           objEDZ.REGORG.ToString(), objEDZ.STARTDATE.ToString("yyyy年MM月dd日"),
                                           objEDZ.ENDDATE == DateTime.MaxValue ? "长期" : objEDZ.ENDDATE.ToString("yyyy年MM月dd日"),
                                           Convert.ToBase64String(objEDZ.PIC_Byte), null, null);
                }
                if (owin != null && !string.IsNullOrEmpty(funcName))
                {
                    this.BeginInvoke(new MyInvoke(ShowData), objEDZ.Name.ToString(), objEDZ.Sex_CName.ToString(), objEDZ.NATION_CName.ToString(),
                                     objEDZ.BIRTH.ToString("yyyy年MM月dd日"), objEDZ.IDC.ToString(), objEDZ.ADDRESS.ToString(),
                                     objEDZ.REGORG.ToString(), objEDZ.STARTDATE.ToString("yyyy年MM月dd日"),
                                     objEDZ.ENDDATE == DateTime.MaxValue ? "长期" : objEDZ.ENDDATE.ToString("yyyy年MM月dd日"),
                                     Convert.ToBase64String(objEDZ.PIC_Byte));
                }

                SetText("身份证读卡成功!" + DateTime.Now.ToString("(yyyy年MM月dd日 HH:mm:ss)"), this.lbl_msg);

                if (objEDZ.PIC_Image != null)
                {
                    SetImage(objEDZ.PIC_Image, pictureBox1);
                }

                SetText(objEDZ.IDC.ToString().Trim(), this.lbl_Code);
                SetText(objEDZ.Name.ToString().Trim(), this.lbl_Names);
                SetText(objEDZ.Sex_CName.ToString().Trim(), this.lbl_Gender);
                SetText(objEDZ.NATION_CName.ToString().Trim(), this.lbl_Folk);
                SetText(objEDZ.BIRTH.ToString("yyyy年MM月dd日").Trim(), this.lbl_Birthday);
                SetText(objEDZ.ADDRESS.ToString().Trim(), this.lbl_Address);
                SetText(objEDZ.REGORG.ToString().Trim(), this.lbl_Agency);
                SetText(objEDZ.STARTDATE.ToString("yyyy年MM月dd日").Trim(), this.lbl_ExpireStart);
                SetText(objEDZ.ENDDATE == DateTime.MaxValue ? "长期" : objEDZ.ENDDATE.ToString("yyyy年MM月dd日"), this.lbl_ExpireEnd);


                //ShowData(objEDZ.Name.ToString() + "," + objEDZ.Sex_CName.ToString() + "," + objEDZ.NATION_CName.ToString() + "," +
                //objEDZ.BIRTH.ToString("yyyy年MM月dd日") + "," + objEDZ.IDC.ToString() + "," + objEDZ.ADDRESS.ToString() + "," +
                //objEDZ.REGORG.ToString() + "," + objEDZ.STARTDATE.ToString("yyyy年MM月dd日") + "," +
                //(objEDZ.ENDDATE == DateTime.MaxValue ? "长期" : objEDZ.ENDDATE.ToString("yyyy年MM月dd日")) + "," +
                //Convert.ToBase64String(objEDZ.PIC_Byte)
                //);
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.Message);
            }
        }