| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- package com.xjrsoft.module.hikvision.util;
- import com.google.gson.JsonObject;
- import com.xjrsoft.module.teacher.mapper.FaceImportMapper;
- import java.io.ByteArrayOutputStream;
- import java.io.InputStream;
- import java.net.URL;
- import java.util.Base64;
- import java.util.HashMap;
- import java.util.Map;
- public class FaceImportUtil {
- private static FaceImportMapper faceImportMapper;
- private static ApiUtil apiUtil;
- public static String ImportTeacherFace(Long id) {
- JsonObject paramJson = new JsonObject();
- paramJson.addProperty("personId", faceImportMapper.GetTeacherHikvisionIdById(id));
- paramJson.addProperty("faceData", ImageToBase64(faceImportMapper.GetTeacherHikvisionImgById(id)));
- Map<String, String> querys = new HashMap<>();
- querys.put("tagId", "frs");
- String apiPath = "/api/resource/v1/face/single/add";
- String response = apiUtil.doPost(apiPath, String.valueOf(paramJson), querys);
- return response;
- }
- public static String ImportStudentFace(Long id) {
- JsonObject paramJson = new JsonObject();
- paramJson.addProperty("personId", faceImportMapper.GetStudentHikvisionIdById(id));
- paramJson.addProperty("faceData", ImageToBase64(faceImportMapper.GetStudentHikvisionImgById(id)));
- Map<String, String> querys = new HashMap<>();
- querys.put("tagId", "frs");
- String apiPath = "/api/resource/v1/face/single/add";
- String response = apiUtil.doPost(apiPath, String.valueOf(paramJson), querys);
- return response;
- }
- public static String ImageToBase64(String imageUrl) {
- String base64String = "";
- try {
- URL url = new URL(imageUrl);
- InputStream inputStream = url.openStream();
- ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
- byte[] buffer = new byte[4096];
- int bytesRead;
- while ((bytesRead = inputStream.read(buffer)) != -1) {
- outputStream.write(buffer, 0, bytesRead);
- }
- byte[] imageBytes = outputStream.toByteArray();
- base64String = Base64.getEncoder().encodeToString(imageBytes);
- inputStream.close();
- outputStream.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- return base64String;
- }
- // public String AddFace(Long id){
- // JsonObject paramJson = new JsonObject();
- // paramJson.addProperty("name", teacherFaceImportMapper.GetHikvisionIdById(id));
- // paramJson.addProperty("description", "none");
- //
- // Map<String, String> querys = new HashMap<>();
- //
- // String apiPath = "/api/frs/v1/face/group/single/addition";
- // String response = ApiUtil.doPost(apiPath, String.valueOf(paramJson), querys);
- //// ObjectMapper mapper = new ObjectMapper();
- //// try {
- //// JsonNode rootNode = mapper.readTree(response);
- //// JsonNode dataNode = rootNode.path("data");
- //// JsonNode indexCodeNode = dataNode.path("indexCode");
- ////
- //// if (indexCodeNode != null && !indexCodeNode.isNull()) {
- //// String indexCode = indexCodeNode.asText();
- //// return indexCode;
- //// } else {
- //// System.out.println("IndexCode is null or not present");
- //// return null;
- //// }
- //// } catch (JsonProcessingException e) {
- //// e.printStackTrace();
- //// return null;
- //// } catch (Exception e) {
- //// e.printStackTrace();
- //// return null;
- //// }
- // }
- }
|