java阿里云图片检测同/异步sdk调用详解
java阿里云图片检测同/异步sdk调用
sdk调用流程:注册阿里云账号→创建RAM子账号→子账号授权→复制子账号的AccessKey ID&Access Key Secret→sdk&api调用
阿里云RAM子账号创建、授权、api文档详情查看
注意:调用阿里云接口的时候最容易出错的俩个地方
1.AccessKey ID&Access Key Secret
因为阿里云是主张接口调用都是给子账号权限然后用子账号的AccessKey ID&Access Key Secret来调用接口,所有我们复制这俩个参数的时候很容易复制成我们主账号的这俩参数去了,切记一定要复制子账号的这俩参数!!
2.权限添加
很多时候我们所有步骤都作对了就是连不通的原因,就是子账号的权限没给,如我现在要调文本反垃圾的接口但是我没给子账号这个权限是无论如何都调不通的切记!!
阿里云不仅提供了api还提供了sdk,有了sdk我们就方便多了,阿里云帮我们封装了所有的方法我们只需要复制粘贴就能用这些接口了,但是前提得注意我上面说的一些注意事项切记嗷java阿里云sdk地址
1.同步图片检测:同步检测图片中的违规内容,实时返回检测结果。
支持检测的场景包括:图片智能鉴黄、图片暴恐涉政识别、图片广告识别、图片二维码识别、图片不良场景识别、图片logo识别。
阿里云图片同步检测接口详情及请求、返回参数详情查看
1):同步图片检测代码如下:
/**
* 图片同步检测
*
* @throws Exception
*/
@org.junit.Test
public void aliyunImageSyncCheck() throws Exception {
//请替换成你自己的accessKeyId、accessKeySecret
IClientProfile profile = DefaultProfile.getProfile(ConfigAliyun.regionId, ConfigAliyun.accessKeyId, ConfigAliyun.accessKeySecret);
IAcsClient client = new DefaultAcsClient(profile);
ImageSyncScanRequest imageSyncScanRequest = new ImageSyncScanRequest();
imageSyncScanRequest.setAcceptFormat(FormatType.JSON); // 指定api返回格式
imageSyncScanRequest.setMethod(com.aliyuncs.http.MethodType.POST); // 指定请求方法
imageSyncScanRequest.setEncoding("utf-8");
imageSyncScanRequest.setRegionId(ConfigAliyun.regionId);
List<Map<String, Object>> tasks = new ArrayList<Map<String, Object>>();
Map<String, Object> task = new LinkedHashMap<String, Object>();
task.put("dataId", UUID.randomUUID().toString());
//https://img.alicdn.com/tfs/TB1Xk_qvwmTBuNjy1XbXXaMrVXa-550-407.jpg
task.put("url", "https://img.alicdn.com/tfs/TB1k_g9l26H8KJjSspmXXb2WXXa-600-600.jpg");
task.put("time", new Date());
tasks.add(task);
JSONObject data = new JSONObject();
/**
* porn: 色情
* terrorism: 暴恐
* qrcode: 二维码
* ad: 图片广告
* ocr: 文字识别
*/
data.put("scenes", Arrays.asList("porn", "terrorism"));
data.put("tasks", tasks);
imageSyncScanRequest.setHttpContent(data.toJSONString().getBytes("UTF-8"), "UTF-8", FormatType.JSON);
/**
* 请务必设置超时时间
*/
imageSyncScanRequest.setConnectTimeout(3000);
imageSyncScanRequest.setReadTimeout(10000);
try {
HttpResponse httpResponse = client.doAction(imageSyncScanRequest);
if (httpResponse.isSuccess()) {
JSONObject scrResponse = JSON.parseObject(new String(httpResponse.getHttpContent(), "UTF-8"));
System.out.println(JSON.toJSONString(scrResponse, true));
if (200 == scrResponse.getInteger("code")) {
JSONArray taskResults = scrResponse.getJSONArray("data");
for (Object taskResult : taskResults) {
if (200 == ((JSONObject) taskResult).getInteger("code")) {
JSONArray sceneResults = ((JSONObject) taskResult).getJSONArray("results");
for (Object sceneResult : sceneResults) {
String scene = ((JSONObject) sceneResult).getString("scene");
String suggestion = ((JSONObject) sceneResult).getString("suggestion");
//根据scene和suggetion做相关的处理
//do something
System.out.println("scene = [" + scene + "]");
System.out.println("suggestion = [" + suggestion + "]");
}
} else {
System.out.println("task process fail:" + ((JSONObject) taskResult).getInteger("code"));
}
}
} else {
System.out.println("detect not success. code:" + scrResponse.getInteger("code"));
}
} else {
System.out.println("response not success. status:" + httpResponse.getStatus());
}
} catch (ServerException e) {
e.printStackTrace();
} catch (ClientException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
2):同步图片检测结果如下:
{
"msg":"OK",
"code":200,
"data":[
{
"msg":"OK",
"code":200,
"dataId":"3d583265-d9cd-4159-83d8-68e8a2616ddf",
"extras":{
},
"results":[
{
"rate":99.700005,
"suggestion":"block",
"label":"porn",
"scene":"porn"
},
{
"rate":99.99,
"suggestion":"pass",
"label":"normal",
"scene":"terrorism"
}
],
"taskId":"imgitaNeo8XlI5fNtKt93rzT-1r9AuE",
"url":"https://img.alicdn.com/tfs/TB1k_g9l26H8KJjSspmXXb2WXXa-600-600.jpg"
}
],
"requestId":"0C0A9B1C-BB89-47B8-8AB8-7FE7BA65D31D"
}
scene = [porn]
suggestion = [block]
scene = [terrorism]
suggestion = [pass]
2.异步图片检测:提交图片异步检测任务,检测图片违规或识别图片中的不良信息。
支持检测的场景包括:图片智能鉴黄、图片暴恐涉政识别、图片广告识别、图片二维码识别、图片不良场景识别、图片logo识别。
异步检测任务不会实时返回检测结果,您需要通过轮询的方式或者callback的方式获取检测结果。。
阿里云图片异步检测接口详情及请求、返回参数详情查看
1):异步图片检测代码如下:
@org.junit.Test
public void aliyunImagecheck()throws Exception{
//图片url
String srcUrl = "http://pic12.nipic.com/20110221/6727421_210944911000_2.jpg";
System.out.println("srcUrl:"+srcUrl);
//获取图片异步检测taskId
String taskId = aliyunImageASyncCheck(srcUrl);
System.out.println("taskId:"+taskId);
//根据taskId查询图片异步检测结果
String imgCheckResult = aliyunImageAsyncResultCheck(taskId);
System.out.println("imgCheckResult:"+imgCheckResult);
}
/**
* 图片异步检测
* @throws Exception
*/
public String aliyunImageASyncCheck(String srcUrl) throws Exception {
String result = "";
//请替换成你自己的accessKeyId、accessKeySecret
IClientProfile profile = DefaultProfile.getProfile("你的regionId", "你的accessKeyId", "你的accessKeySecret");
IAcsClient client = new DefaultAcsClient(profile);
ImageAsyncScanRequest imageAsyncScanRequest = new ImageAsyncScanRequest();
imageAsyncScanRequest.setAcceptFormat(FormatType.JSON); // 指定api返回格式
imageAsyncScanRequest.setMethod(com.aliyuncs.http.MethodType.POST); // 指定请求方法
imageAsyncScanRequest.setEncoding("utf-8");
imageAsyncScanRequest.setRegionId(ConfigAliyun.regionId);
List<Map<String, Object>> tasks = new ArrayList<Map<String, Object>>();
Map<String, Object> task1 = new LinkedHashMap<String, Object>();
task1.put("dataId", UUID.randomUUID().toString());
//"http://pic12.nipic.com/20110221/6727421_210944911000_2.jpg"
task1.put("url", srcUrl);
task1.put("time", new Date());
tasks.add(task1);
JSONObject data = new JSONObject();
/**
* porn: 色情
* terrorism: 暴恐
* qrcode: 二维码
* ad: 图片广告
* ocr: 文字识别
*/
data.put("scenes", Arrays.asList("porn", "ocr", "qrcode", "sface"));
data.put("tasks", tasks);
imageAsyncScanRequest.setHttpContent(data.toJSONString().getBytes("UTF-8"), "UTF-8", FormatType.JSON);
/**
* 请务必设置超时时间
*/
imageAsyncScanRequest.setConnectTimeout(3000);
imageAsyncScanRequest.setReadTimeout(6000);
try {
HttpResponse httpResponse = client.doAction(imageAsyncScanRequest);
if (httpResponse.isSuccess()) {
JSONObject scrResponse = JSON.parseObject(new String(httpResponse.getHttpContent(), "UTF-8"));
System.out.println(JSON.toJSONString(scrResponse, true));
if (200 == scrResponse.getInteger("code")) {
JSONArray taskResults = scrResponse.getJSONArray("data");
for (Object taskResult : taskResults) {
if (200 == ((JSONObject) taskResult).getInteger("code")) {
String taskId = ((JSONObject) taskResult).getString("taskId");
// 将taskId 保存下来,间隔一段时间来轮询结果, 参照ImageAsyncScanResultsRequest
System.out.println("args = [" + taskId + "]");
result = taskId;
} else {
System.out.println("task process fail:" + ((JSONObject) taskResult).getInteger("code"));
}
}
} else {
System.out.println("detect not success. code:" + scrResponse.getInteger("code"));
}
} else {
System.out.println("response not success. status:" + httpResponse.getStatus());
}
} catch (ServerException e) {
e.printStackTrace();
} catch (ClientException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 图片异步检测结果查询
* @throws Exception
*/
public static String aliyunImageAsyncResultCheck(String taskId)throws Exception{
String result = "";
//请替换成你自己的accessKeyId、accessKeySecret
IClientProfile profile = DefaultProfile.getProfile("你的regionId", "你的accessKeyId", "你的accessKeySecret");
IAcsClient client = new DefaultAcsClient(profile);
ImageAsyncScanResultsRequest imageAsyncScanResultsRequest = new ImageAsyncScanResultsRequest();
imageAsyncScanResultsRequest.setAcceptFormat(FormatType.JSON); // 指定api返回格式
imageAsyncScanResultsRequest.setMethod(com.aliyuncs.http.MethodType.POST); // 指定请求方法
imageAsyncScanResultsRequest.setEncoding("utf-8");
imageAsyncScanResultsRequest.setRegionId(ConfigAliyun.regionId);
List<String> taskIds = new ArrayList<String>();
taskIds.add(taskId);
imageAsyncScanResultsRequest.setHttpContent(JSON.toJSONString(taskIds).getBytes("UTF-8"), "UTF-8", FormatType.JSON);
/**
* 请务必设置超时时间
*/
imageAsyncScanResultsRequest.setConnectTimeout(3000);
imageAsyncScanResultsRequest.setReadTimeout(6000);
try {
HttpResponse httpResponse = client.doAction(imageAsyncScanResultsRequest);
if(httpResponse.isSuccess()){
JSONObject scrResponse = JSON.parseObject(new String(httpResponse.getHttpContent(), "UTF-8"));
// System.out.println(JSON.toJSONString(scrResponse, true));
if (200 == scrResponse.getInteger("code")) {
JSONArray taskResults = scrResponse.getJSONArray("data");
for (Object taskResult : taskResults) {
if(200 == ((JSONObject)taskResult).getInteger("code")){
JSONArray sceneResults = ((JSONObject)taskResult).getJSONArray("results");
for (Object sceneResult : sceneResults) {
String scene = ((JSONObject)sceneResult).getString("scene");
String suggestion = ((JSONObject)sceneResult).getString("suggestion");
result = suggestion;
//根据scene和suggetion做相关的处理
//do something
}
}else{
System.out.println("task process fail:" + ((JSONObject)taskResult).getInteger("code"));
}
}
} else {
System.out.println("detect not success. code:" + scrResponse.getInteger("code"));
}
}else{
System.out.println("response not success. status:" + httpResponse.getStatus());
}
} catch (ServerException e) {
e.printStackTrace();
} catch (ClientException e) {
e.printStackTrace();
} catch (Exception e){
e.printStackTrace();
}
return result;
}
2):异步图片检测结果如下:
srcUrl:http://pic12.nipic.com/20110221/6727421_210944911000_2.jpg
{
"msg":"OK",
"code":200,
"data":[
{
"msg":"OK",
"code":200,
"dataId":"a0285d1c-d423-4fd3-a9ed-3005725b88e4",
"taskId":"img3QGf$fM4AzB4UUroFJI2GF-1r9AKR",
"url":"http://pic12.nipic.com/20110221/6727421_210944911000_2.jpg"
}
],
"requestId":"DF984416-28D6-4088-B4BE-7552481DCB4E"
}
args = [img3QGf$fM4AzB4UUroFJI2GF-1r9AKR]
taskId:img3QGf$fM4AzB4UUroFJI2GF-1r9AKR
imgCheckResult:review
https://blog.csdn.net/csdn_ljl/article/details/99677488
相关信息
- C# Func的同步、异步调用
- 阿里云系列——7.阿里云IIS系列详解(过程+通用+最新)
- java中的同步与异步
- 阿里云 API调用实践(python语言)
- Thinkphp3.2整合阿里云OSS图片上传
- C#(委托同步调用、异步调用、异步回调)
- koa2使用阿里云oss的nodejs sdk实现上传图片
- node.js中异步调用
- C#调用阿里云CDN API刷新缓存
- 阿里云 测试调用接口
- C#异步编程(async and await)及异步方法同步调用
- 阿里云解析域名步骤
- 阿里云使用笔记
- 域名解析是否生效实时检测(阿里云DNS检测)
- 阿里云 装 Android sdk
- 阿里云直播 C# SDK 如何使用
- 阿里云 CDN+OSS 解决方案
- 阿里云优惠
- 阿里云安全
- Java同步块(synchronized block)使用详解
- net 异步与同步
- 快速学习阿里云OSS存储-测试
- 对于同步、异步、阻塞、非阻塞的几点浅薄理解
- Android中线程同步异步方式小结
- 阿里云安装 oss sdk
- 阿里云oss解决资源跨域
- SpringBoot中异步请求和异步调用(看这一篇就够了)
- C#:Func的同步、异步调用
- 新手如何租用阿里云服务器(图文教程)
- Java多线程异步调用性能调优
- 亲测:阿里云的高效云盘真没有声称那么好
- js 异步转同步
- 阿里云CDN实践
- 坚果云 + SVN 实现异地代码同步
- spring boot注解 --@EnableAsync 异步调用
- AsyncTask用法和异步加载图片
- 阿里云云计算认证是什么?阿里云acp云计算考试内容有哪些?
- C#委托同步异步说明,并比较control调用Invoke和BeginInvoke的异同
- 同步(Synchronous)和异步(Asynchronous)
- angular promise $q 异步调用
- 阿里云系列——1.域名创建(详细步骤)---2015-11.12
- 阿里云公司简介
- 阿里云oss对象存储
- 阿里云ACA主要内容
- 阿里云对象存储OSS
- 阿里云安全组设置
- 阿里云acp云计算教材包括哪些内容?阿里云acp云计算教材推荐
- 如何在实际场景中使用异常检测?阿里云Prometheus智能检测算子来了
- C# 异步和同步
- 阿里云系列——3.网站备案初步核审(详细步骤)---2015-11.12
热门资讯
推荐资讯
最新资讯
- 每个程序员必学的10个Git命令
- Android开发之蓝牙详解(一)
- Android开发之蓝牙详解(三)
- 防止SQL注入的五种方法
- 5种方法防止 jsp被sql注入
- 【译】在React中实现条件渲染的7种方法
- winform防止界面卡死的三种方法 总结:使用委托 + 线程的方法实现 参考:https://blog.csdn.net/handsomexiaominge/article/details/4265...
- Android WifiManager.WifiLock 简介
- [译]Android内存泄漏的八种可能(上)
- python+appnium【第一章-环境搭建】
- 第一章:tornado入门环境搭建
- unittest框架核心要素及应用
- 黑马程序员前端-CSS用户界面样式源码
- 传智健康项目day01
- 传智健康项目day02
- 传智健康项目中相关知识点介绍(如图片存储,发送短信,定时调度,统计报表...)
- 黑马传智健康项目讲义第一章之ElementUI
- 黑马传智健康项目讲义第一章 之Power Designer
- 黑马传智健康项目讲义第一章之项目概述
- MYSQL的master/slave数据同步配置 ZT
评论