一、导入maven依赖
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.5.29</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-ocr</artifactId>
<version>1.0.11</version>
</dependency>
<!-- 内容处理-->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-imageaudit</artifactId>
<version>1.0.8</version>
</dependency>
<!-- 图像识别-->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-imagerecog</artifactId>
<version>1.0.12</version>
</dependency>
二、根据官方文档案例测试代码:可根据自己的需求自行添加
链接
package com.xiaoxu.admin.kafka.aliyun;
import com.alibaba.fastjson.JSON;
import com.aliyuncs.AcsResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.RpcAcsRequest;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.imageaudit.model.v20191230.ScanImageRequest;
import com.aliyuncs.imageaudit.model.v20191230.ScanImageRequest.Task;
import com.aliyuncs.imageaudit.model.v20191230.ScanImageResponse;
import com.aliyuncs.imageaudit.model.v20191230.ScanTextRequest;
import com.aliyuncs.imageaudit.model.v20191230.ScanTextResponse;
import com.aliyuncs.imagerecog.model.v20190930.RecognizeSceneRequest;
import com.aliyuncs.imagerecog.model.v20190930.RecognizeSceneResponse;
import com.aliyuncs.imagerecog.model.v20190930.TaggingImageRequest;
import com.aliyuncs.imagerecog.model.v20190930.TaggingImageResponse;
import com.aliyuncs.ocr.model.v20191230.*;
import com.aliyuncs.profile.DefaultProfile;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
public class ImageTest {
static IAcsClient client = null;
public static void main(String[] args) throws Exception {
DefaultProfile profile = DefaultProfile.getProfile(
"cn-shanghai",
"",
"");
client = new DefaultAcsClient(profile);
testScanText();
System.out.println("--------------------------------------------------------------");
}
private static <R extends RpcAcsRequest<T>, T extends AcsResponse> T getAcsResponse(R req) throws Exception {
try {
return client.getAcsResponse(req);
} catch (ServerException e) {
System.out.println(String.format("ServerException: errCode=%s, errMsg=%s", e.getErrCode(), e.getErrMsg()));
throw e;
} catch (ClientException e) {
System.out.println(String.format("ClientException: errCode=%s, errMsg=%s", e.getErrCode(), e.getErrMsg()));
throw e;
} catch (Exception e) {
System.out.println("Exception:" + e.getMessage());
throw e;
}
}
public static void testScanImage() throws Exception {
System.out.println("-------- 内容审核 --------------");
ScanImageRequest req = new ScanImageRequest();
List<String> scenes = new ArrayList<String>();
scenes.add("porn");
req.setScenes(scenes);
List<Task> tasks = new ArrayList<Task>();
com.aliyuncs.imageaudit.model.v20191230.ScanImageRequest.Task task = new Task();
task.setDataId(UUID.randomUUID().toString());
task.setImageURL("https://viapi-demo.oss-cn-shanghai.aliyuncs.com/viapi-demo/images/ChangeImageSize/change-image-size-src.png");
tasks.add(task);
req.setTasks(tasks);
ScanImageResponse resp = getAcsResponse(req);
printResponse(req.getSysActionName(), resp.getRequestId(), resp);
}
public static void testScanText() throws Exception {
System.out.println("-------- 文章内容审核 --------------");
ScanTextRequest req = new ScanTextRequest();
List<ScanTextRequest.Labels> labelss = new ArrayList<>();
ScanTextRequest.Labels label1 = new ScanTextRequest.Labels();
label1.setLabel("politics");
labelss.add(label1);
ScanTextRequest.Labels label2 = new ScanTextRequest.Labels();
label2.setLabel("abuse");
labelss.add(label2);
ScanTextRequest.Labels label3 = new ScanTextRequest.Labels();
label3.setLabel("terrorism");
labelss.add(label3);
ScanTextRequest.Labels label4 = new ScanTextRequest.Labels();
label4.setLabel("porn");
labelss.add(label4);
ScanTextRequest.Labels label5 = new ScanTextRequest.Labels();
label5.setLabel("ad");
labelss.add(label5);
req.setLabelss(labelss);
List<ScanTextRequest.Tasks> tasks = new ArrayList<>();
ScanTextRequest.Tasks task1 = new ScanTextRequest.Tasks();
task1.setContent("本校小额贷款,安全、快捷、方便、无抵押,随机随贷,当天放款,上门服务。联系weixin 123456");
tasks.add(task1);
req.setTaskss(tasks);
ScanTextResponse response = getAcsResponse(req);
System.out.println(JSON.toJSONString(response));
}
public static void testTaggingImage() throws Exception {
System.out.println("-------- 通用图像打标 --------------");
TaggingImageRequest req = new TaggingImageRequest();
req.setImageURL("https://viapi-demo.oss-cn-shanghai.aliyuncs.com/viapi-demo/xxx.png");
TaggingImageResponse resp = getAcsResponse(req);
printResponse(req.getSysActionName(), resp.getRequestId(), resp);
}
public static void printResponse(String actionName, String requestId, AcsResponse data) {
System.out.println( String.format("data=%s",JSON.toJSONString(data)) );
}
}
三、测试结果
审核结果: 链接
|