圖像審核API參考
文檔地址
https://cloud.baidu.com/doc/ANTIPORN/s/jk42xep4e
注意:
Content-Type為application/x-www-form-urlencoded
,然后通過urlencode
格式化請求體。
代碼
1.圖片轉base64
注意使用java.util.Base64的包,生成的base64才不會帶換行符
import java.util.Base64;
public static String convertFileToBase64(String imgPath) {
byte[] data = null;
// 讀取圖片字節數組
try {
InputStream in = new FileInputStream(imgPath);
System.out.println("文件大小(字節Byte)=" + in.available());
data = new byte[in.available()];
in.read(data);
in.close();
} catch (IOException e) {
e.printStackTrace();
}
// 對字節數組進行Base64編碼,得到Base64編碼的字符串
return new String(Base64.getEncoder().encodeToString(data));
}
2.發送POST請求
/**
* 向指定URL發送POST方法的請求 Content-Type=application/x-www-form-urlencoded
*
* @param targetUrl 發送請求的URL
* @param params 請求參數,請求參數應該是name1=value1&name2=value2的形式。
* @return JSONObject 返回的JSON數據
*/
public static JSONObject postFormUrlEncoded(String targetUrl, String params) {
HttpURLConnection urlConnection = null;
try {
URL url = new URL(targetUrl.trim());
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("POST");
// 百度要求此類型上傳圖片
urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
urlConnection.setUseCaches(false);
urlConnection.connect();
PrintWriter out = new PrintWriter(new OutputStreamWriter(urlConnection.getOutputStream(), StandardCharsets.UTF_8));
// 寫入參數
out.print(params);
out.flush();
int resultCode = urlConnection.getResponseCode();
if (HttpURLConnection.HTTP_OK == resultCode) {
StringBuffer stringBuffer = new StringBuffer();
String readLine;
BufferedReader responseReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), StandardCharsets.UTF_8));
while ((readLine = responseReader.readLine()) != null) {
stringBuffer.append(readLine);
}
responseReader.close();
return JSONObject.parseObject(stringBuffer.toString());
}
out.close();
} catch (Exception e) {
return null;
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
}
return null;
}
3.圖片審核
/**
* 獲得校驗結果
*
* @param imgBase64 Base64格式
* @param imgType 0 靜態圖片 1 動態圖片
* @return
* @throws UnsupportedEncodingException
*/
public static Boolean getCensorResult(String imgBase64, String imgType) {
if (ValidatorUtils.isEmpty(accessToken)) {
getAccessToken();
}
try {
String params = "image=" + URLEncoder.encode(imgBase64,"UTF-8") + "&imgType=" + imgType + "&access_token=" + accessToken;
JSONObject jsonObject;
//post 請求方式
jsonObject = HttpUtils.postFormUrlEncoded(host, params);
if (jsonObject.get("error_code") != null) {
// token失效
if ("110".equals(jsonObject.get("error_code"))) {
getAccessToken();
}
System.out.println(" 校驗失敗,原因:" + jsonObject.get("error_msg"));
return false;
}
if ("1".equals(jsonObject.get("conclusionType").toString())) {
return true;
} else {
System.out.println(" 校驗失敗,原因:" + jsonObject.get("data"));
return false;
}
}catch (UnsupportedEncodingException e){
e.printStackTrace();
return false;
}
}
踩坑
通過第一段代碼的返回的base64圖片格式需要使用URLEncoder格式化一遍,否則會報錯
String params = "image=" + URLEncoder.encode(imgBase64,"UTF-8") + "&imgType=" + imgType + "&access_token=" + accessToken;
錯誤的寫法
String params = "image=" + imgBase64 + "&imgType=" + imgType + "&access_token=" + accessToken;