登录
原创

人脸识别接口JAVA示例

发布于 2022-06-14 阅读 247
  • Java
  • HTTP
  • 聚合数据
原创

package com.jefferson.utils.interfaceDemo.verifyface;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**

  • @ClassName: HttpRequestUtil

  • @Description: Http请求工具类
    */
    public class HttpRequestUtil {

    private static final Logger logger = LoggerFactory.getLogger(HttpRequestUtil.class);

    public static final String UTF8 = “UTF-8”;

    private static final String EMPTY = “”;
    private static final String JuHeURl = “http://apis.juhe.cn/verifyface/verify”;

    private static MultiThreadedHttpConnectionManager connectionManager = null;

    private static int connectionTimeOut = 5000;

    private static int socketTimeOut = 5000;

    private static int maxConnectionPerHost = 500;

    private static int maxTotalConnections = 500;

    private static HttpClient client;

    static {
    connectionManager = new MultiThreadedHttpConnectionManager();
    connectionManager.getParams().setConnectionTimeout(connectionTimeOut);
    connectionManager.getParams().setSoTimeout(socketTimeOut);
    connectionManager.getParams().setDefaultMaxConnectionsPerHost(maxConnectionPerHost);
    connectionManager.getParams().setMaxTotalConnections(maxTotalConnections);
    client = new HttpClient(connectionManager);
    }

    /**

    • POST方式表单提交数据
    • @param url
    • @param busiParams
    •        业务参数
      
    • @param headParams
    •        请求头参数
      

    */
    public static String URLPost(String url, Map<String, Object> busiParams) {
    String response = EMPTY;
    PostMethod postMethod = null;
    try {
    postMethod = new PostMethod(url);
    // 将表单的值放入postMethod中
    if (busiParams != null) {
    Set<String> keySet = busiParams.keySet();
    for (String key : keySet) {
    Object value = busiParams.get(key);
    postMethod.addParameter(key, String.valueOf(value));
    }
    }
    postMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, “utf-8”);
    // 执行postMethod
    int statusCode = client.executeMethod(postMethod);
    if (statusCode == HttpStatus.SC_OK) {
    response = postMethod.getResponseBodyAsString();
    } else {
    logger.error(“响应状态码=” + postMethod.getStatusCode());
    }
    } catch (HttpException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    } finally {
    if (postMethod != null) {
    postMethod.releaseConnection();
    postMethod = null;
    }
    }
    return response;
    }

    public static void main(String[] args) throws Exception {
    String imgFile = “F:/ocr/aaa.jpg”;// 图片地址
    File file = new File(imgFile);
    String realName = “张三”;// 姓名
    String idcard = “**********************”;// 身份证号
    FileInputStream fin = null;
    try {
    fin = new FileInputStream(file);
    byte[] bytes = new byte[fin.available()];
    fin.read(bytes);
    String base64 = Base64.getEncoder().encodeToString(bytes);
    Map<String, Object> params = new HashMap<>();
    params.put(“idcard”, idcard);
    params.put(“realname”, realName);
    params.put(“image”, base64);
    params.put(“key”, “”);
    String s = HttpRequestUtil.URLPost(JuHeURl, params);
    System.out.println(s);
    } catch (Exception e) {
    e.printStackTrace();
    } finally {
    if (fin != null) {
    fin.close();
    }
    }

    }
    }

评论区

詹姆斯36岁了还能获得NBA总冠军,你呢?

0

0

0

举报