Step By Step
1.开通人脸比对1:1服务。
人脸比对1:1
2.获取您的真实AK信息
查看AK
3.添加java依赖
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-facebody</artifactId>
<version>1.2.27</version>
</dependency>
注意:仓库同步可能会有延迟,如果遇到版本不存在的情况,请稍后再试或使用上一个版本
4.Java Common SDK Code
替换aksk以及图片路径即可
package org.example;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import sun.misc.BASE64Encoder;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
public class CompareFaceCommon {
public static DefaultProfile profile = DefaultProfile.getProfile("cn-shanghai", "ak", "sk");
public static DefaultAcsClient client = new DefaultAcsClient(profile);
public static void main(String[] args) {
CommonRequest request = new CommonRequest();
request.setSysDomain("facebody.cn-shanghai.aliyuncs.com");
request.setSysMethod(MethodType.POST);
request.setSysVersion("2019-12-30");
request.setSysAction("CompareFace");
// Request body
String pic_path = "C:\\Users\\cnc\\Desktop\\cnc\\identity.jpg";//本地图片的路径\
String pic_path2 = "C:\\Users\\cnc\\Desktop\\cnc\\test.jpg";//本地图片的路径
File picBase64 = new File(pic_path);
File picBase65 = new File(pic_path2);
try {
//提出base64编码的换行符问题
String ImageDataA = CompareFaceCommon.encodeImageToBase64(picBase64);
String ImageDataB = CompareFaceCommon.encodeImageToBase64(picBase65);
System.out.println();
String A = ImageDataA.replaceAll("[\\s*\t\n\r]", "");
String B = ImageDataB.replaceAll("[\\s*\t\n\r]", "");
//System.out.println(A);
//System.out.println(B);
request.putBodyParameter("ImageDataA", A);
request.putBodyParameter("ImageDataB", B);
CommonResponse response = client.getCommonResponse(request);
System.out.println(response.getData());
}catch (Exception e) {
e.printStackTrace();
}
}
public static String encodeImageToBase64(File file) throws Exception {
//将图片文件转化为字节数组字符串,并对其进行Base64编码处理
// loggerger.info("图片的路径为:" + file.getAbsolutePath());
InputStream in = null;
byte[] data = null;
//读取图片字节数组
try {
in = new FileInputStream(file);
data = new byte[in.available()];
in.read(data);
in.close();
} catch (IOException e) {
e.printStackTrace();
throw new Exception("图片上传失败,请联系客服!");
}
//对字节数组Base64编码
BASE64Encoder encoder = new BASE64Encoder();
String base64 = encoder.encode(data);
return base64;//返回Base64编码过的字节数组字符串
}
}
5.运行结果
参考链接
人脸比对1:1