对接阿里云人脸对比接口

一:对接阿里云人脸识别接口的工具类
注意:如果你的图片已经转换为base64的编码以后参数是content_1,后面要加type请求参数
public class FaceUtil {

private static final String ak_id = "你自己的ak_id";

private static final String ak_secret = "你自己的ak_secret";

//阿里云给的接口地址https://dtplus-cn-shanghai.data.aliyuncs.com/face/verify

private static final String url = "";

/*
 * 计算MD5+BASE64
 */
public static String MD5Base64(String s) {
	if (s == null)
		return null;
	String encodeStr = "";
	byte[] utfBytes = s.getBytes();
	MessageDigest mdTemp;
	try {
		mdTemp = MessageDigest.getInstance("MD5");
		mdTemp.update(utfBytes);
		byte[] md5Bytes = mdTemp.digest();
		Base64Helper b64Encoder = new Base64Helper();
		encodeStr = b64Encoder.encode(md5Bytes);
	} catch (Exception e) {
		throw new Error("Failed to generate MD5 : " + e.getMessage());
	}
	return encodeStr;
}

/*
 * 计算 HMAC-SHA1
 */
public static String HMACSha1(String data, String key) {
	String result;
	try {
		SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(), "HmacSHA1");
		Mac mac = Mac.getInstance("HmacSHA1");
		mac.init(signingKey);
		byte[] rawHmac = mac.doFinal(data.getBytes());
		result = (new Base64Helper()).encode(rawHmac);
	} catch (Exception e) {
		throw new Error("Failed to generate HMAC : " + e.getMessage());
	}
	return result;
}

/*
 * 等同于javaScript中的 new Date().toUTCString();
 */
public static String toGMTString(Date date) {
	SimpleDateFormat df = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss z", Locale.UK);
	df.setTimeZone(new java.util.SimpleTimeZone(0, "GMT"));
	return df.format(date);
}

/*
 * 发送POST请求
 */
 //如果发送的是转换为base64编码后后面加请求参数type为1,如果请求的是图片的url则不用加type参数。
public static String sendPost(String content_1, String content_2) throws Exception {
	String body = "{\"content_1\": \"" + content_1 + "\", \"content_2\":\"" + content_2 + "\", \"type\":\"" + "1" + "\"}";
	PrintWriter out = null;
	BufferedReader in = null;
	String result = "";
	int statusCode = 200;
	try {
		URL realUrl = new URL(url);
		/*
		 * http header 参数
		 */
		String method = "POST";
		// 返回值类型
		String accept = "application/json";
		// 请求内容类型
		String content_type = "application/json";
		String path = realUrl.getFile();
		// GMT时间
		String date = toGMTString(new Date());
		// 1.对body做MD5+BASE64加密
		String bodyMd5 = MD5Base64(body);
		String stringToSign = method + "\n" + accept + "\n" + bodyMd5 + "\n" + content_type + "\n" + date + "\n"
				+ path;
		// 2.计算 HMAC-SHA1
		String signature = HMACSha1(stringToSign, ak_secret);
		// 3.得到 authorization header
		String authHeader = "Dataplus " + ak_id + ":" + signature;
		// 打开和URL之间的连接
		URLConnection conn = realUrl.openConnection();
		// 设置通用的请求属性
		conn.setRequestProperty("Accept", accept);
		conn.setRequestProperty("Content-type", content_type);
		conn.setRequestProperty("Date", date);
		// 认证信息
		conn.setRequestProperty("Authorization", authHeader);
		// 发送POST请求必须设置如下两行
		conn.setDoOutput(true);
		conn.setDoInput(true);
		// 获取URLConnection对象对应的输出流
		out = new PrintWriter(conn.getOutputStream());
		// 发送请求参数
		out.print(body);
		// flush输出流的缓冲
		out.flush();
		// 定义BufferedReader输入流来读取URL的响应
		statusCode = ((HttpURLConnection) conn).getResponseCode();
		if (statusCode != 200) {
			in = new BufferedReader(new InputStreamReader(((HttpURLConnection) conn).getErrorStream()));
		} else {
			in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
		}
		String line;
		while ((line = in.readLine()) != null) {
			result += line;
		}
	} catch (Exception e) {
		e.printStackTrace();
	} finally {
		try {
			if (out != null) {
				out.close();
			}
			if (in != null) {
				in.close();
			}
		} catch (IOException ex) {
			ex.printStackTrace();
		}
	}
	if (statusCode != 200) {
		throw new IOException("\nHttp StatusCode: " + statusCode + "\nErrorMessage: " + result);
	}
	return result;
}

public static void main(String[] args) throws Exception {
	String url1 = "https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=2465377548,3910830278&fm=26&gp=0.jpg";
	String url2 = "https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=2465377548,3910830278&fm=26&gp=0.jpg";
	String response = sendPost(url1, url2);

	System.out.println(response.toString());
}

}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值