FaceImportUtil.java 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package com.xjrsoft.module.hikvision.util;
  2. import com.google.gson.JsonObject;
  3. import com.xjrsoft.module.teacher.mapper.FaceImportMapper;
  4. import java.io.ByteArrayOutputStream;
  5. import java.io.InputStream;
  6. import java.net.URL;
  7. import java.util.Base64;
  8. import java.util.HashMap;
  9. import java.util.Map;
  10. public class FaceImportUtil {
  11. private static FaceImportMapper faceImportMapper;
  12. private static ApiUtil apiUtil;
  13. public static String ImportTeacherFace(Long id) {
  14. JsonObject paramJson = new JsonObject();
  15. paramJson.addProperty("personId", faceImportMapper.GetTeacherHikvisionIdById(id));
  16. paramJson.addProperty("faceData", ImageToBase64(faceImportMapper.GetTeacherHikvisionImgById(id)));
  17. Map<String, String> querys = new HashMap<>();
  18. querys.put("tagId", "frs");
  19. String apiPath = "/api/resource/v1/face/single/add";
  20. String response = apiUtil.doPost(apiPath, String.valueOf(paramJson), querys);
  21. return response;
  22. }
  23. public static String ImportStudentFace(Long id) {
  24. JsonObject paramJson = new JsonObject();
  25. paramJson.addProperty("personId", faceImportMapper.GetStudentHikvisionIdById(id));
  26. paramJson.addProperty("faceData", ImageToBase64(faceImportMapper.GetStudentHikvisionImgById(id)));
  27. Map<String, String> querys = new HashMap<>();
  28. querys.put("tagId", "frs");
  29. String apiPath = "/api/resource/v1/face/single/add";
  30. String response = apiUtil.doPost(apiPath, String.valueOf(paramJson), querys);
  31. return response;
  32. }
  33. public static String ImageToBase64(String imageUrl) {
  34. String base64String = "";
  35. try {
  36. URL url = new URL(imageUrl);
  37. InputStream inputStream = url.openStream();
  38. ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  39. byte[] buffer = new byte[4096];
  40. int bytesRead;
  41. while ((bytesRead = inputStream.read(buffer)) != -1) {
  42. outputStream.write(buffer, 0, bytesRead);
  43. }
  44. byte[] imageBytes = outputStream.toByteArray();
  45. base64String = Base64.getEncoder().encodeToString(imageBytes);
  46. inputStream.close();
  47. outputStream.close();
  48. } catch (Exception e) {
  49. e.printStackTrace();
  50. }
  51. return base64String;
  52. }
  53. // public String AddFace(Long id){
  54. // JsonObject paramJson = new JsonObject();
  55. // paramJson.addProperty("name", teacherFaceImportMapper.GetHikvisionIdById(id));
  56. // paramJson.addProperty("description", "none");
  57. //
  58. // Map<String, String> querys = new HashMap<>();
  59. //
  60. // String apiPath = "/api/frs/v1/face/group/single/addition";
  61. // String response = ApiUtil.doPost(apiPath, String.valueOf(paramJson), querys);
  62. //// ObjectMapper mapper = new ObjectMapper();
  63. //// try {
  64. //// JsonNode rootNode = mapper.readTree(response);
  65. //// JsonNode dataNode = rootNode.path("data");
  66. //// JsonNode indexCodeNode = dataNode.path("indexCode");
  67. ////
  68. //// if (indexCodeNode != null && !indexCodeNode.isNull()) {
  69. //// String indexCode = indexCodeNode.asText();
  70. //// return indexCode;
  71. //// } else {
  72. //// System.out.println("IndexCode is null or not present");
  73. //// return null;
  74. //// }
  75. //// } catch (JsonProcessingException e) {
  76. //// e.printStackTrace();
  77. //// return null;
  78. //// } catch (Exception e) {
  79. //// e.printStackTrace();
  80. //// return null;
  81. //// }
  82. // }
  83. }