public static void main(String[] args) {
// 图像组合APIurl
String imgCensorUrl = "https://aip.baidubce.com/api/v1/solution/direct/img_censor";
// String filePath = "C:\\Users\\32249\\Desktop\\a.jpeg";
String filePath = "D:\\s.png";
try {
//请求参数
Map sceneConf = new HashMap();
Map ocrConf = new HashMap();
ocrConf.put("recognize_granularity", "big");
ocrConf.put("language_type", "CHN_ENG");
ocrConf.put("detect_direction", true);
ocrConf.put("detect_language", true);
sceneConf.put("ocr", ocrConf);
Map input = new HashMap();
List scenes = new ArrayList();
scenes.add("ocr");
scenes.add("face");
scenes.add("public");
scenes.add("politician");
scenes.add("antiporn");
scenes.add("terror");
scenes.add("webimage");
scenes.add("disgust");
scenes.add("watermark");
byte[] imgData = FileUtil.readFileByBytes(filePath);
String imgStr = Base64Util.encode(imgData);
String imageUrl = "https://image.so.com/view?src=360pic_normal&z=1&i=0&cmg=3e114faa2b60621a61e3e688b06f83d0&q=images&correct=images&ancestor=list&cmsid=1d5abda4c2c9e483a28c61858abed218&cmran=0&cmras=0&cn=0&gn=0&kn=12&fsn=87&adstar=0&clw=249#id=ba3fcdd4fc4e0874a02e7fc3fc59c0a1&currsn=0&ps=75&pc=75";
// String urlEncodeImageUrl = StringUtil.URLEncode(imageUrl);
input.put("imgUrl", imgStr);//与image二者选一
input.put("scenes", scenes);
input.put("sceneConf", sceneConf);
String params = GsonUtils.toJson(input);
System.out.println("params:"+params);
/**
* 线上环境access_token有过期时间, 客户端可自行缓存,过期后重新获取。
*/
ImgCensor m = new ImgCensor();
//String accessToken = "#####调用鉴权接口获取的token#####";
String accessToken=m.gettoken();
String result = HttpUtil.post(imgCensorUrl, accessToken,"application/json", params);
System.out.println("result:"+result);
} catch (Exception e) {
e.printStackTrace();
}
}
此为我的代码:
本地图片审核报:result:{"log_id":15669886577432500,"error_msg":"download image error","error_code":282804}
网上的图片报:result:{"log_id":15669887481086421,"error_msg":"image format error","error_code":216201}
错误代码写错了,网上图片应该报错:download image error,这是因为传的并不是图片链接,而是一个网址。
本地图片报错,代码、原图没有贴出来,所以不太清楚什么情况;应该是图片的问题,应该是不支持的图片格式。
下面是目前支持的图片要求:
请求格式支持:PNG、JPG、JPEG、BMP。
图像大小支持:图像要求base64后大于等于5kb,小于等于4M,最短边大于等于128像素,小于等于4096像素