using CollectInformation.HCUSBSDK; using CollectInformation.Tools; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Diagnostics; using System.Drawing; using System.Runtime.InteropServices; using System.Windows.Forms; using System.Xml; namespace CollectInformation { public partial class MainForm : Form { Dictionary loginUser = null; //Dictionary user = null; Dictionary securityInfo = null; HCUSBUtils hcUSBUtils = null; bool isInit = false; string sdkLevel = ""; string devName = ""; bool isLoginD = false; public MainForm() { InitializeComponent(); } public MainForm(Dictionary loginUser) { InitializeComponent(); this.loginUser = loginUser; if (loginUser != null) { this.Text = loginUser["name"] + "欢迎您使用" + this.Text; } isInit = false; this.btnLinkEquipment.Enabled = true; this.btnReadID.Enabled = false; initD(); } private void initD() { this.hcUSBUtils = new HCUSBUtils(); this.sdkLevel = "SDK lv " + hcUSBUtils.initSDK(); this.isInit = this.hcUSBUtils.TraverseDevice(); if (this.isInit) { this.devName = System.Text.Encoding.UTF8.GetString(this.hcUSBUtils.m_aHidDevInfo[0].szDeviceName).TrimEnd('\0'); this.labLinkStatus.Text = "" + this.devName + " | " + this.sdkLevel; } else { this.isInit = false; MessageBox.Show("当前设备链接失败,请尝试重新链接,或联系管理员。", "异常提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } private void MainForm_Load(object sender, EventArgs e) { if (securityInfo == null) { clear(); } } private void btnReplaceUser_Click(object sender, EventArgs e) { if (MessageBox.Show("您将要使用当前身份证信息覆盖报名人员信息,此操作无法撤销,请确定是否进行操作!", "系统警告", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes) { this.labName2.Text = this.labName.Text; this.labSex2.Text = this.labSex.Text; this.labNation2.Text = this.labNation.Text; this.labBirthday2.Text = this.labBirthday.Text.Replace("年", "-").Replace("月", "-").Replace("日", ""); this.securityInfo["name"] = this.labName.Text; this.securityInfo["gender"] = this.labSex.Text; this.securityInfo["nation"] = this.labNation.Text; this.securityInfo["birth"] = this.labBirthday.Text.Replace("年", "-").Replace("月", "-").Replace("日", ""); } } private void MainForm_FormClosing(object sender, FormClosingEventArgs e) { if (e.CloseReason == CloseReason.UserClosing) { if (MessageBox.Show("您确定要退出采集终端么?", "系统提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.Cancel) { e.Cancel = true; } else { Application.Exit(); } } } private void btnLinkEquipment_Click(object sender, EventArgs e) { if (!this.isInit) { initD(); } if (this.isInit) { this.isLoginD = hcUSBUtils.LoginDevice(); if (this.isLoginD) { MessageBox.Show("设备链接成功。", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information); this.btnReadID.Enabled = true; this.labLinkStatus.Text = "已连接 " + this.devName + " | " + this.sdkLevel; } else { MessageBox.Show("当前设备链接失败,请尝试重新链接,或联系管理员。", "异常提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } } private void btnReadID_Click(object sender, EventArgs e) { // 获取身份证信息 string inputStr = "{\"IdentityInfoCond\":{}}"; string outputStr = this.hcUSBUtils.CollectionNoImage(1, inputStr).TrimEnd('\0'); try { Dictionary d1 = JsonConvert.DeserializeObject>(outputStr); if (d1.ContainsKey("statusString")) { MessageBox.Show("设备未返回正确信息请重新尝试!错误信息:" + d1["statusString"], "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } } catch (Exception) { XmlDocument xdoc = new XmlDocument(); xdoc.LoadXml(outputStr); XmlNamespaceManager nsMgr = new XmlNamespaceManager(xdoc.NameTable); nsMgr.AddNamespace("ns", "http://www.isapi.org/ver20/XMLSchema"); XmlNode statusNode = xdoc.SelectSingleNode("/ns:ResponseStatus/ns:statusString", nsMgr); if (statusNode != null) { MessageBox.Show("设备未返回正确信息请重新尝试!错误信息:" + statusNode.InnerText, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } else { outputStr = JsonConvert.SerializeXmlNode(xdoc.FirstChild); } } Dictionary os = JsonConvert.DeserializeObject>(outputStr); Dictionary IdentityInfoD = JsonConvert.DeserializeObject>(os["IdentityInfo"].ToString()); this.labName.Text = IdentityInfoD["chnName"].ToString(); this.labSex.Text = IdentityInfoD["sex"].ToString() == "male" ? "男" : IdentityInfoD["sex"].ToString() == "female" ? "女" : "未知"; string nationIndex = IdentityInfoD["nation"].ToString().PadLeft(2, '0'); this.labNation.Text = Nationality.GetNationality(ref nationIndex); this.labBirthday.Text = this.getDate(IdentityInfoD["birth"].ToString()); this.labAddress.Text = IdentityInfoD["addr"].ToString(); this.labID.Text = IdentityInfoD["IDCardNo"].ToString(); // "220182199603257024"; this.labSigning.Text = IdentityInfoD["issuingAuthority"].ToString(); this.labValidity.Text = this.getDate(IdentityInfoD["startDate"].ToString()) + " 至 " + this.getDate(IdentityInfoD["endDate"].ToString()); // 使用身份证号获取待采集人员 string url = constants.host + string.Format(constants.fetch, "security_guard_base") + "?status=1&card=" + labID.Text; Dictionary res = HttpUitls.Get(url, constants.userToken); if (res["code"] == "0") { MessageBox.Show("系统出现异常,请联系管理员进行处理!\r\n错误信息:" + res["message"], "系统异常", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { Dictionary retData = JsonConvert.DeserializeObject>(res["data"]); if (retData["errcode"].ToString() == "0") { Dictionary ret = JsonConvert.DeserializeObject>(res["data"]); if (ret["data"] != null) { string securityStr = JsonConvert.SerializeObject(ret["data"]); this.securityInfo = JsonConvert.DeserializeObject>(securityStr); this.labName2.Text = securityInfo["name"]; this.labBeforeName.Text = securityInfo["beforeName"]; this.labSex2.Text = securityInfo["gender"]; this.labNation2.Text = securityInfo["nation"]; this.labBirthday2.Text = securityInfo["birth"]; this.labLevel.Text = securityInfo["grade"]; this.labCompany.Text = securityInfo["sign_company"]; this.labID2.Text = securityInfo["card"]; this.btnOK.Enabled = true; if (securityInfo["collect_photo"].ToString() == "已采集") { this.btnGetImage.Enabled = false; url = constants.host + string.Format(constants.fetch, "security_guard_collect") + "?type=人像&security_guard_id=" + securityInfo["id"].ToString(); Dictionary resCollect = HttpUitls.Get(url, constants.userToken); if (res["code"] == "0") { this.btnGetImage.Enabled = true; } else { Dictionary collect = JsonConvert.DeserializeObject>(resCollect["data"]); string collectStr = JsonConvert.SerializeObject(collect["data"]); Dictionary collect1 = JsonConvert.DeserializeObject>(collectStr); this.picUser.ImageLocation = constants.urlbase + collect1["data"].ToString(); } } else { this.btnGetImage.Enabled = true; } this.btnGetFingerprint.Enabled = true; this.btnReplaceUser.Enabled = true; } else { MessageBox.Show("没有对应的待采集人员信息!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } else { MessageBox.Show("服务端返回异常信息,请联系管理员!\r\n异常信息:" + retData["errmsg"], "系统异常", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } private void btnClear_Click(object sender, EventArgs e) { clear(); } private void clear() { // 清空身份证读取信息 this.labName.Text = ""; this.labSex.Text = ""; this.labNation.Text = ""; this.labBirthday.Text = ""; this.labAddress.Text = ""; this.labID.Text = ""; this.labSigning.Text = ""; this.labValidity.Text = ""; // 清空采集人员信息 this.securityInfo = null; this.labName2.Text = ""; this.labBeforeName.Text = ""; this.labSex2.Text = ""; this.labNation2.Text = ""; this.labBirthday2.Text = ""; this.labLevel.Text = ""; this.labUserFingerprint.Text = ""; this.labCompany.Text = ""; this.labID2.Text = ""; this.picUser.Image = null; // 控制管理按钮 this.btnReplaceUser.Enabled = false; this.btnOK.Enabled = false; this.btnGetFingerprint.Enabled = false; this.btnGetImage.Enabled = false; } private void btnOK_Click(object sender, EventArgs e) { if (this.securityInfo != null) { // 上传指纹采集信息,并修改保安员状态 if (this.labUserFingerprint.Text == "" || this.labUserFingerprint.Text == "未获取") { MessageBox.Show("请采集指纹信息!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } else if(this.securityInfo["collect_photo"] != "已采集" && this.picUser.Image == null) { MessageBox.Show("请采集人像信息!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { string url = constants.host + string.Format(constants.fetch, "security_guard_others") + "?security_guard_id=" + this.securityInfo["id"]; Dictionary res = HttpUitls.Get(url, constants.userToken); if (res["code"] == "0") { MessageBox.Show("系统出现异常,请联系管理员进行处理!\r\n错误信息:" + res["message"], "系统异常", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { Dictionary retData = JsonConvert.DeserializeObject>(res["data"]); if (retData["errcode"].ToString() == "0") { Dictionary ret = JsonConvert.DeserializeObject>(res["data"]); if (ret["data"] != null) { string otherStr = JsonConvert.SerializeObject(ret["data"]); Dictionary securityOther = JsonConvert.DeserializeObject>(otherStr); //securityOther["fingerprint_num"] = this.labUserFingerprint.Text; securityOther["fingerprint_date"] = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); securityOther["fingerprint_personal"] = this.loginUser["name"]; securityOther["fingerprint_office"] = this.loginUser["belong_company"]; this.editSecurityOtherInfo(securityOther); } } else { MessageBox.Show("服务端返回异常信息,请联系管理员!\r\n异常信息:" + retData["errmsg"], "系统异常", MessageBoxButtons.OK, MessageBoxIcon.Error); } } Dictionary collect = new Dictionary(); collect["name"] = this.securityInfo["name"]; collect["security_guard_id"] = this.securityInfo["id"]; collect["type"] = "指纹"; collect["data"] = this.labUserFingerprint.Text; collect["collect_personal"] = this.loginUser["name"]; collect["collect_police"] = this.loginUser["belong_company"]; if (this.createSecurityCollect(collect)) { if (this.securityInfo["collect_photo"] == "已采集") { this.securityInfo["status"] = "2"; this.securityInfo["collect_fingerprint"] = "已采集"; if (this.editSecurityBassInfo(this.securityInfo)) { MessageBox.Show("信息上传成功!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information); clear(); } } else { this.securityInfo["collect_fingerprint"] = "已采集"; this.editSecurityBassInfo(this.securityInfo); // 上传头像信息,并修改保安员状态 string upLoadUrl = string.Format(constants.filehost, "baoan", "collect", DateTime.Now.ToString("yyyyMMdd"), this.securityInfo["card"]); Dictionary upLoadRes = HttpUitls.UploadImage(upLoadUrl, this.picUser.ImageLocation, constants.userToken); if (upLoadRes["code"] != "0") { Dictionary tempData = JsonConvert.DeserializeObject>(upLoadRes["data"]); if (tempData["errcode"].ToString() == "0") { //Dictionary upLoadResData = JsonConvert.DeserializeObject>(tempData["data"].ToString()); collect = new Dictionary(); collect["name"] = this.securityInfo["name"]; collect["security_guard_id"] = this.securityInfo["id"]; collect["type"] = "人像"; collect["data"] = tempData["uri"].ToString(); collect["collect_personal"] = this.loginUser["name"]; collect["collect_police"] = this.loginUser["belong_company"]; if (this.createSecurityCollect(collect)) { // 全部处理成功后,修改保安员状态 this.securityInfo["collect_photo"] = "已采集"; this.securityInfo["status"] = "2"; if (this.editSecurityBassInfo(this.securityInfo)) { MessageBox.Show("信息上传成功!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information); clear(); } } } } } } else { MessageBox.Show("指纹信息保存失败!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } else { MessageBox.Show("没有需要提交写信息,请重新操作!", "系统异常", MessageBoxButtons.OK, MessageBoxIcon.Error); this.btnOK.Enabled = false; } } bool editSecurityBassInfo(Dictionary security) { bool rest = false; // 创建修改链接 string url = constants.host + string.Format(constants.update, "security_guard_base") + "?id=" + security["id"]; string data = JsonConvert.SerializeObject(security); Dictionary res = HttpUitls.Post(url, data, "", constants.userToken); if (res["code"] == "0") { MessageBox.Show("系统出现异常,请联系管理员进行处理!\r\n错误信息:" + res["message"], "系统异常", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { Dictionary retData = JsonConvert.DeserializeObject>(res["data"]); if (retData["errcode"].ToString() == "0") { rest = true; } else { MessageBox.Show("服务端返回异常信息,请联系管理员!\r\n异常信息:" + retData["errmsg"], "系统异常", MessageBoxButtons.OK, MessageBoxIcon.Error); } } return rest; } bool editSecurityOtherInfo(Dictionary securityOther) { bool rest = false; // 创建修改链接 string url = constants.host + string.Format(constants.update, "security_guard_others") + "?id=" + securityOther["id"]; string data = JsonConvert.SerializeObject(securityOther); Dictionary res = HttpUitls.Post(url, data, "", constants.userToken); if (res["code"] == "0") { MessageBox.Show("系统出现异常,请联系管理员进行处理!\r\n错误信息:" + res["message"], "系统异常", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { Dictionary retData = JsonConvert.DeserializeObject>(res["data"]); if (retData["errcode"].ToString() == "0") { rest = true; } else { MessageBox.Show("服务端返回异常信息,请联系管理员!\r\n异常信息:" + retData["errmsg"], "系统异常", MessageBoxButtons.OK, MessageBoxIcon.Error); } } return rest; } bool createSecurityCollect(Dictionary collect) { bool rest = false; // 创建修改链接 string url = constants.host + string.Format(constants.install, "security_guard_collect"); string data = JsonConvert.SerializeObject(collect); Dictionary res = HttpUitls.Post(url, data, "", constants.userToken); if (res["code"] == "0") { MessageBox.Show("系统出现异常,请联系管理员进行处理!\r\n错误信息:" + res["message"], "系统异常", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { Dictionary retData = JsonConvert.DeserializeObject>(res["data"]); if (retData["errcode"].ToString() == "0") { rest = true; } else { MessageBox.Show("服务端返回异常信息,请联系管理员!\r\n异常信息:" + retData["errmsg"], "系统异常", MessageBoxButtons.OK, MessageBoxIcon.Error); } } return rest; } private void btnGetImage_Click(object sender, EventArgs e) { string filePath = Application.StartupPath + "\\" + constants.tempFiles; string fileName = "tempImage.png"; string outputStr = this.hcUSBUtils.CaptureFace(filePath, fileName); if (outputStr != null) { try { Dictionary d1 = JsonConvert.DeserializeObject>(outputStr); MessageBox.Show("设备未返回正确信息请重新尝试!错误信息:" + d1["statusString"], "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } catch (Exception) { XmlDocument xdoc = new XmlDocument(); xdoc.LoadXml(outputStr); XmlNamespaceManager nsMgr = new XmlNamespaceManager(xdoc.NameTable); nsMgr.AddNamespace("ns", "http://www.isapi.org/ver20/XMLSchema"); XmlNode statusNode = xdoc.SelectSingleNode("/ns:ResponseStatus/ns:statusString", nsMgr); if (statusNode != null) { MessageBox.Show("设备未返回正确信息请重新尝试!错误信息:" + statusNode.InnerText, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); } XmlNode captureProgressNode = xdoc.SelectSingleNode("/ns:CaptureFaceData/ns:captureProgress", nsMgr); if (captureProgressNode != null && captureProgressNode.InnerText == "100") { // 使用第一张图片 string fileFullPath = filePath + "\\0_" + fileName; this.picUser.ImageLocation = fileFullPath; } else { MessageBox.Show("人像采集失败,请重新尝试。", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } } } private void btnGetFingerprint_Click(object sender, EventArgs e) { string inputStr = "" + "1" + ""; string outputStr = this.hcUSBUtils.CollectionNoImage(2, inputStr).TrimEnd('\0'); if (outputStr != null) { try { Dictionary d1 = JsonConvert.DeserializeObject>(outputStr); MessageBox.Show("设备未返回正确信息请重新尝试!错误信息:" + d1["statusString"], "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } catch (Exception) { XmlDocument xdoc = new XmlDocument(); xdoc.LoadXml(outputStr); XmlNamespaceManager nsMgr = new XmlNamespaceManager(xdoc.NameTable); nsMgr.AddNamespace("ns", "http://www.isapi.org/ver20/XMLSchema"); //outputStr = JsonConvert.SerializeXmlNode(xdoc.FirstChild); XmlNode statusNode = xdoc.SelectSingleNode("/ns:ResponseStatus/ns:statusString", nsMgr); if (statusNode != null) { MessageBox.Show("设备未返回正确信息请重新尝试!错误信息:" + statusNode.InnerText, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); } XmlNode fingerDataNode = xdoc.SelectSingleNode("/ns:CaptureFingerPrint/ns:fingerData", nsMgr); if (fingerDataNode != null) { string finger = fingerDataNode.InnerText; this.labUserFingerprint.Text = finger; } else { MessageBox.Show("指纹采集失败,请重新尝试。", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } } } string getDate(string str) { string yyyy = str.Substring(0,4); string mm = str.Substring(4, 2); string dd = str.Substring(6, 2); return yyyy + "年" + mm + "月" + dd + "日"; } } }