错误代码如下
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";
try {
//请求参数
Map<String, Object> sceneConf = new HashMap<String, Object>();
Map<String, Object> ocrConf = new HashMap<String, Object>();
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<String, Object> input = new HashMap<String, Object>();
List<Object> scenes = new ArrayList<Object>();
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";
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”:15669842523047777,“error_msg”:“download image error”,“error_code”:282804}
纠错:这是什么原因导致的
最终找到了解决办法
原来是我在set值的时候有问题,如果是:
本地图片应该为:input.put(“image”, imgStr);//与imgUrl二者选一
网络图片应该为:input.put(“imgUrl”, imageUrl);//与image二者选一
具体正确代码如下:
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<String, Object> sceneConf = new HashMap<String, Object>();
Map<String, Object> ocrConf = new HashMap<String, Object>();
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<String, Object> input = new HashMap<String, Object>();
List<Object> scenes = new ArrayList<Object>();
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", imageUrl);//与image二者选一
//input.put("image", 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();
}
}