用java封装一个图片审核的代码工具类,图片审核调用百度api内容审核功能,代码的复用性要高
时间: 2024-04-01 19:34:22 浏览: 15
好的,这里给出一个 Java 版本的图片审核工具类,同样使用了百度的内容审核 API。
注意,这里需要使用到百度的 API Key 和 Secret Key,请根据自己的实际情况进行替换。
```java
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.*;
public class ImageModeration {
private String apiKey;
private String secretKey;
private String accessToken;
public ImageModeration(String apiKey, String secretKey) {
this.apiKey = apiKey;
this.secretKey = secretKey;
this.accessToken = getAccessToken();
}
private String getAccessToken() {
String accessTokenUrl = "https://aip.baidubce.com/oauth/2.0/token";
Map<String, String> params = new HashMap<>();
params.put("grant_type", "client_credentials");
params.put("client_id", apiKey);
params.put("client_secret", secretKey);
try {
String result = HttpUtil.post(accessTokenUrl, params);
return JsonUtil.getValue(result, "access_token");
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public JSONObject imageModeration(String imagePath, String imageType, String[] scenes, double threshold) {
String image = imageToBase64(imagePath);
String url = "https://aip.baidubce.com/rest/2.0/solution/v1/img_censor/v2/user_defined";
Map<String, String> params = new HashMap<>();
params.put("image", image);
params.put("image_type", imageType);
params.put("scenes", String.join(",", scenes));
params.put("threshold", String.valueOf(threshold));
try {
String result = HttpUtil.post(url, params, accessToken);
return new JSONObject(result);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
private String imageToBase64(String imagePath) {
File file = new File(imagePath);
try (InputStream in = new FileInputStream(file)) {
byte[] data = new byte[in.available()];
in.read(data);
return Base64.getEncoder().encodeToString(data);
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
}
class HttpUtil {
public static String post(String url, Map<String, String> params) throws Exception {
return post(url, params, null);
}
public static String post(String url, Map<String, String> params, String accessToken) throws Exception {
HttpURLConnection conn = null;
BufferedReader reader = null;
try {
URL realUrl = new URL(url);
conn = (HttpURLConnection) realUrl.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
if (accessToken != null) {
conn.setRequestProperty("Authorization", "Bearer " + accessToken);
}
conn.setDoOutput(true);
conn.setDoInput(true);
OutputStream out = conn.getOutputStream();
out.write(getParamString(params).getBytes());
out.flush();
out.close();
reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder result = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
result.append(line);
}
return result.toString();
} finally {
if (reader != null) {
reader.close();
}
if (conn != null) {
conn.disconnect();
}
}
}
private static String getParamString(Map<String, String> params) throws UnsupportedEncodingException {
StringBuilder result = new StringBuilder();
for (Map.Entry<String, String> entry : params.entrySet()) {
result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
result.append("&");
}
return result.substring(0, result.length() - 1);
}
}
class JsonUtil {
public static String getValue(String json, String key) throws JSONException {
JSONObject object = new JSONObject(json);
return object.getString(key);
}
}
```
该工具类的使用方法如下:
```java
String apiKey = "your_api_key";
String secretKey = "your_secret_key";
ImageModeration moderation = new ImageModeration(apiKey, secretKey);
JSONObject result = moderation.imageModeration("your_image_path", "BASE64", new String[]{"antiporn"}, 0.95);
System.out.println(result);
```
其中,`apiKey` 和 `secretKey` 分别为你的百度 API Key 和 Secret Key,`your_image_path` 为待审核的图片路径。`imageModeration` 方法的返回值是一个 `JSONObject` 对象,包含了审核结果的详细信息。你可以根据需要对这些信息进行解析和处理。