/// <summary> /// 查找或者更新数据 /// </summary> /// <param name="ip">所属ip</param> /// <param name="acc">账号</param> /// <param name="newpass">新密码</param> /// <returns></returns> public static bool SearchOrUpdate(LocalUserMod user, String gender) { //先判断是否存在文件 Create(@xmlPath); //装载文件 XmlDocument doc = new XmlDocument(); doc.Load(@xmlPath); XmlElement root = doc.DocumentElement; var nodes = root.GetElementsByTagName(gender); foreach (XmlNode node in nodes) //male { foreach (XmlNode inode in node.ChildNodes) //ID { if (inode.Attributes[0].Value.Equals(user.Num)) //如果该学号已经存在,更新数据 { inode.FirstChild.InnerText = user.Name; inode.LastChild.InnerText = user.Freature; //保存结果 doc.Save(@xmlPath); return(true); //存在,且更新了 } } } //查找的IP不存在 return(false); }
/// <summary> /// 添加一条记录 /// </summary> /// <param name="ip">所属ip</param> /// <param name="nacc">账号</param> /// <param name="npass">密码</param> public static void AddOneData(LocalUserMod user, String gender) { //先判断是否存在文件 Create(@xmlPath); //装载文件 XmlDocument doc = new XmlDocument(); doc.Load(@xmlPath); XmlElement root = doc.DocumentElement; var nodes = root.GetElementsByTagName(gender); //male foreach (XmlNode node in nodes) { if (SearchOrUpdate(user, gender)) { return; } } //新增连接、账号信息 XmlElement newCon = doc.CreateElement("ID"); newCon.SetAttribute("id", user.Num); XmlElement temp1 = doc.CreateElement("Name"); temp1.InnerText = user.Name; XmlElement temp2 = doc.CreateElement("Feature"); temp2.InnerText = user.Freature; newCon.AppendChild(temp1); newCon.AppendChild(temp2); nodes.Item(0).AppendChild(newCon); //root.AppendChild(newCon); doc.Save(@xmlPath); }
/// <summary> /// 注册事件触发函数 /// </summary> /// <param name="sender"></param> /// <param name="args">变量参数</param> public static void NewUserRegist(object sender, myEventArgs.RegistArgs args) { /**************************** * 保存特征值时才区分性别 * **************************/ //下载图片到本地 String url = String.Format("{0}/{1}_{2}.{3}", Url_DldImg, args.Regist.Id, args.Regist.Name, args.Regist.Extension); String pathImg = String.Format("{0}\\{1}_{2}.{3}", ImgRootPath_Regist, args.Regist.Id, args.Regist.Name, args.Regist.Extension); HttpUtil.downFile(url, pathImg); //判断性别(假设性别判断 100% 准确) try { ResultCode result = EngineActivate.ActivateEngine(APPID, APPKEY); if (result != ResultCode.SDK已激活) { Console.WriteLine(result.ToString()); return; } IntPtr hengine = EngineFactory.GetEngineInstance(EngineFactory.Image, DetectionOrientPriority.ASF_OP_0_ONLY, 16); //检测角度指人脸在照片中的角度 Bitmap img1 = new Bitmap(pathImg); var face = new FaceDetection(hengine, img1); var r = face.GetGender(); LocalUserMod temp1 = new LocalUserMod(); temp1.Name = args.Regist.Name; temp1.Num = args.Regist.Id; //用Base64转码 temp1.Freature = Convert.ToBase64String(face.getFaceFeature(img1)); //销毁图片 img1.Dispose(); //提取特征值保存 switch (r) { case "男": XmlUtil.AddOneData(temp1, "Male"); break; case "女": XmlUtil.AddOneData(temp1, "Female"); break; default: Console.WriteLine("获取性别失败!用户:{0} , 学号:{1},注册时间:{2}", args.Regist.Name, args.Regist.Id, DateTime.Now.ToString()); return; } //提示用户注册 Console.WriteLine("User registed: {0} at {1}", args.Regist.Name, DateTime.Now.ToString()); //更新用户信息 //令写一个函数 getAllUserInfo(); } catch (Exception ex) { Console.WriteLine("Regist Error: {0}", ex.Message); } finally { EngineFactory.DisposeEngine(); } }