百度智能云:https://cloud.baidu.com/campaign/Promotionai/index.html
Java SDK 内容接口说明:https://cloud.baidu.com/doc/ANTIPORN/s/tk3h6xdji
依赖:
1 2 3 4 5
| <dependency> <groupId>com.baidu.aip</groupId> <artifactId>java-sdk</artifactId> <version>4.12.0</version> </dependency>
|
demo:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
| import com.baidu.aip.contentcensor.AipContentCensor; import com.baidu.aip.contentcensor.EImgType; import lombok.extern.slf4j.Slf4j; import org.json.JSONObject;
import java.io.*;
@Slf4j public class CensorUtil { public static final String APP_ID = "APP_ID"; public static final String API_KEY = "API_KEY"; public static final String SECRET_KEY = "SECRET_KEY";
private static AipContentCensor contentCensor;
static { contentCensor = new AipContentCensor(APP_ID, API_KEY, SECRET_KEY); }
public static boolean checkText(String msg) { if (msg != null && msg.length() > 0) { JSONObject response = contentCensor.textCensorUserDefined(msg); log.info("结果:" + response.getString("conclusion")); return response.getInt("conclusionType") == 1; } return true; }
public static boolean checkImg(byte[] data) { if (data != null && data.length > 0) { JSONObject object = contentCensor.imageCensorUserDefined(data, null); log.info("结果:" + object.getString("conclusion")); return object.getInt("conclusionType") == 1; } else { return true; } }
public static boolean checkImg(String imgUrl) { if (imgUrl != null && imgUrl.length() > 0) { JSONObject object = contentCensor.imageCensorUserDefined(imgUrl, EImgType.URL, null); log.info("结果:" + object.getString("conclusion")); return object.getInt("conclusionType") == 1; } else { return true; } }
public static void main(String[] args) throws IOException { String str = "词语"; log.info(str + " ---> " + checkText(str));
String imgName = "图片路径\\aaa.jpg"; FileInputStream fis = new FileInputStream(new File(imgName)); ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] arr = new byte[1024]; int len; while ((len = fis.read(arr)) != -1) { baos.write(arr, 0, len); } log.info(imgName + " ---> " + checkImg(baos.toByteArray()));
String imgUrl = "https://xxx/yyy.png"; log.info(imgUrl + " ---> " + checkImg(imgUrl)); } }
|