使用RestTemplate改造face++人脸比对对接接口

    public FaceVO faceCompare(String imageReflUrl, String imageUrl, String userId) {
        MultiValueMap paramMap = new LinkedMultiValueMap<>();
        paramMap.add("api_key", faceConfig.getApiKey());
        paramMap.add("api_secret", faceConfig.getApiSecret());
        paramMap.add("comparison_type", 0);
        paramMap.add("face_image_type", "raw_image");
        paramMap.add("uuid", userId);
        paramMap.add("fail_when_multiple_faces", 0);
        paramMap.add("face_quality_threshold", 0);
        paramMap.add("image_ref1", new FileSystemResource(imageReflUrl));
        paramMap.add("image", new FileSystemResource(imageUrl));
        String result;
        try {
            result = restTemplate.postForEntity(faceConfig.getApiUrl(), new HttpEntity<>(paramMap, buildHttpHeaders()), String.class).getBody();
        } catch (HttpClientErrorException e) {
            result = e.getResponseBodyAsString();
        }
        return JSONObject.parseObject(result, FaceVO.class);
    }

    /**
     * 设置头信息
     *
     * @return 请求头
     */
    private HttpHeaders buildHttpHeaders() {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.parseMediaType("multipart/form-data"));
        return headers;
    }

 

你可能感兴趣的:(人脸识别,RestTemplate)