小白问下这边是传图片路径还是图片base64编码
随风丶浮云丶 发布于2018-10 浏览:2283 回复:2
0
收藏

package com.baidu.ai.aip.auth;


import com.baidu.ai.aip.auth.util.Base64Util;
import com.baidu.ai.aip.auth.util.FileUtil;
import com.baidu.ai.aip.auth.util.GsonUtils;
import com.baidu.ai.aip.auth.util.HttpUtil;

import java.util.*;

/**
* 人脸对比
*/
public class FaceMatch {


//人脸图片比对
public static String match() {
// 请求url
String url = "https://aip.baidubce.com/rest/2.0/face/v3/match";
try {

byte[] bytes1 = FileUtil.readFileByBytes("E:/images/liudehua.png"); //直接传图片base64编码也不行
byte[] bytes2 = FileUtil.readFileByBytes("E:/images/liudehua2.png");
String image1 = Base64Util.encode(bytes1);
String image2 = Base64Util.encode(bytes2);

List> images = new ArrayList<>();

Map map1 = new HashMap<>();
map1.put("image", image1);
map1.put("image_type", "BASE64");
map1.put("face_type", "LIVE");
map1.put("quality_control", "LOW");
map1.put("liveness_control", "NORMAL");

Map map2 = new HashMap<>();
map2.put("image", image2);
map2.put("image_type", "BASE64");
map2.put("face_type", "LIVE");
map2.put("quality_control", "LOW");
map2.put("liveness_control", "NORMAL");

images.add(map1);
images.add(map2);

String param = GsonUtils.toJson(images);

// 注意这里仅为了简化编码每一次请求都去获取access_token,线上环境access_token有过期时间, 客户端可自行缓存,过期后重新获取。
String accessToken = "24.2884842328233be84ba8b23280b26756.2592000.1542369322.282335-\n" +
"14219405";

String result = HttpUtil.post(url, accessToken, param);
System.out.println(result);
return result;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

public static void main(String[] args) {
FaceMatch.match();
}

}

错误信息

com.baidu.ai.aip.auth.FaceMatch
java.net.MalformedURLException: Illegal character in URL
at sun.net.www.protocol.https.HttpsURLConnectionImpl.checkURL(HttpsURLConnectionImpl.java:86)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.(HttpsURLConnectionImpl.java:94)
at sun.net.www.protocol.https.Handler.openConnection(Handler.java:62)
at sun.net.www.protocol.https.Handler.openConnection(Handler.java:57)
at java.net.URL.openConnection(URL.java:979)
at com.baidu.ai.aip.auth.util.HttpUtil.postGeneralUrl(HttpUtil.java:41)
at com.baidu.ai.aip.auth.util.HttpUtil.post(HttpUtil.java:34)
at com.baidu.ai.aip.auth.util.HttpUtil.post(HttpUtil.java:28)
at com.baidu.ai.aip.auth.util.HttpUtil.post(HttpUtil.java:19)
at com.baidu.ai.aip.auth.FaceMatch.match(FaceMatch.java:53)
at com.baidu.ai.aip.auth.FaceMatch.main(FaceMatch.java:63)

 

收藏
点赞
0
个赞
共2条回复 最后由用户已被禁言回复于2018-10
#3用户已被禁言回复于2018-10

为什么不用百度现成的Java SDK呢?

可以参考我写的示例代码  https://gitee.com/xshuai/ai/blob/master/AIDemo/src/main/java/com/xs/face/FaceMatchV3Sample.java

推荐使用fastjson。

2
#2胖虎胖虎真可爱回复于2018-10

base64编码

0
TOP
切换版块