本文提供了使用Java SDK将给定的相似图图片添加到图库中的示例代码。
说明 具体参数说明请参见添加样本图片。
准备工作
在进行具体的服务调用之前,请参见以下步骤,完成准备工作:
- 创建阿里云AccessKeyId和AccessKeySecret。具体请参见创建AccessKey。
- 安装Java依赖。具体请参见安装Java依赖。
- 如果使用本地文件或者二进制文件检测,请下载并在项目工程中引入Extension.Uploader工具类。
传入URL示例代码
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.green.model.v20180509.AddSimilarityImageRequest;
import com.aliyuncs.http.FormatType;
import com.aliyuncs.http.HttpResponse;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.http.ProtocolType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
IClientProfile profile = DefaultProfile
.getProfile("cn-shanghai", "您自己的AccessId", "您自己的AccessSecret");
DefaultProfile
.addEndpoint("cn-shanghai", "cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
IAcsClient client = new DefaultAcsClient(profile);
AddSimilarityImageRequest addSimilarityImageRequest = new AddSimilarityImageRequest();
// 指定api返回格式、请求方法
addSimilarityImageRequest.setAcceptFormat(FormatType.JSON);
addSimilarityImageRequest.setMethod(MethodType.POST);
addSimilarityImageRequest.setEncoding("utf-8");
addSimilarityImageRequest.setProtocol(ProtocolType.HTTP);
JSONObject httpBody = new JSONObject();
/**
* 一次请求中可以同时添加多张图片(最大20张/次),
* 计费按照单张图片添加的单价×添加的图片数量计算
*/
JSONObject task = new JSONObject();
task.put("dataId", UUID.randomUUID().toString());
// 添加图片
task.put("url", "https://pics5.baidu.com/feed/5ab5c9ea15ce36d3b0a4017b7dbacc83e850b1a3.jpeg?token=313a1d92dba0ff15504f4265a2fd3d7c&s=55F53CC468531DD28C80A1900300008B");
// 添加最多3个自定义标签(非必须)
JSONArray tags = new JSONArray();
tags.add("自定义标签1");
tags.add("自定义标签2");
tags.add("自定义标签3");
task.put("tag", tags);
httpBody.put("tasks", Arrays.asList(task));
addSimilarityImageRequest.setHttpContent(org.apache.commons.codec.binary.StringUtils.getBytesUtf8(httpBody.toJSONString()),
"UTF-8", FormatType.JSON);
/**
* 请设置超时时间, 服务端全链路处理超时时间为10秒,请做相应设置
* 如果您设置的ReadTimeout 小于服务端处理的时间,程序中会获得一个read timeout 异常
*/
addSimilarityImageRequest.setConnectTimeout(3000);
addSimilarityImageRequest.setReadTimeout(10000);
HttpResponse httpResponse = null;
try {
httpResponse = client.doAction(addSimilarityImageRequest);
} catch (Exception e) {
e.printStackTrace();
}
//服务端接收到请求,并完成处理返回的结果
if (httpResponse != null && httpResponse.isSuccess()) {
JSONObject scrResponse = JSON.parseObject(org.apache.commons.codec.binary.StringUtils.newStringUtf8(httpResponse.getHttpContent()));
System.out.println(JSON.toJSONString(scrResponse, true));
int requestCode = scrResponse.getIntValue("code");
//每一张图片的检测结果
JSONArray taskResults = scrResponse.getJSONArray("data");
if (200 == requestCode) {
for (Object taskResult : taskResults) {
//单张图片的处理结果
int taskCode = ((JSONObject) taskResult).getIntValue("code");
if (200 == taskCode) {
System.out.println("成功添加1张图片 ");
} else {
//单张图片处理失败, 原因是具体的情况详细分析
System.out.println("task process fail. task response:" + JSON.toJSONString(taskResult));
}
}
} else {
/**
* 表明请求整体处理失败,原因视具体的情况详细分析
*/
System.out.println("the whole image scan request failed. response:" + JSON.toJSONString(scrResponse));
}
}
}
}
传入本地文件示例代码
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.green.extension.uploader.ClientUploader;
import com.aliyuncs.green.model.v20180509.AddSimilarityImageRequest;
import com.aliyuncs.http.FormatType;
import com.aliyuncs.http.HttpResponse;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.http.ProtocolType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
IClientProfile profile = DefaultProfile
.getProfile("cn-shanghai", "您自己的AccessId", "您自己的AccessSecret");
DefaultProfile
.addEndpoint("cn-shanghai", "cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
IAcsClient client = new DefaultAcsClient(profile);
AddSimilarityImageRequest addSimilarityImageRequest = new AddSimilarityImageRequest();
// 指定api返回格式、请求方法
addSimilarityImageRequest.setAcceptFormat(FormatType.JSON);
addSimilarityImageRequest.setMethod(MethodType.POST);
addSimilarityImageRequest.setEncoding("utf-8");
addSimilarityImageRequest.setProtocol(ProtocolType.HTTP);
JSONObject httpBody = new JSONObject();
/**
* 一次请求中可以同时添加多张图片(最大20张/次),
* 计费按照单张图片添加的单价×添加的图片数量计算
*/
JSONObject task = new JSONObject();
task.put("dataId", UUID.randomUUID().toString());
/**
* 如果您要检测的文件存于本地服务器上,可以通过下述代码片生成url,
* 再将返回的url作为图片地址传递到服务端
*/
String url = null;
ClientUploader clientUploader = ClientUploader.getImageClientUploader(profile, false);
try{
url = clientUploader.uploadFile("d:/test.jpg");
}catch (Exception e){
e.printStackTrace();
}
task.put("url", url);
// 添加最多3个自定义标签(非必须)
JSONArray tags = new JSONArray();
tags.add("自定义标签1");
tags.add("自定义标签2");
tags.add("自定义标签3");
task.put("tag", tags);
httpBody.put("tasks", Arrays.asList(task));
addSimilarityImageRequest.setHttpContent(org.apache.commons.codec.binary.StringUtils.getBytesUtf8(httpBody.toJSONString()),
"UTF-8", FormatType.JSON);
/**
* 请设置超时时间, 服务端全链路处理超时时间为10秒,请做相应设置
* 如果您设置的ReadTimeout 小于服务端处理的时间,程序中会获得一个read timeout 异常
*/
addSimilarityImageRequest.setConnectTimeout(3000);
addSimilarityImageRequest.setReadTimeout(10000);
HttpResponse httpResponse = null;
try {
httpResponse = client.doAction(addSimilarityImageRequest);
} catch (Exception e) {
e.printStackTrace();
}
//服务端接收到请求,并完成处理返回的结果
if (httpResponse != null && httpResponse.isSuccess()) {
JSONObject scrResponse = JSON.parseObject(org.apache.commons.codec.binary.StringUtils.newStringUtf8(httpResponse.getHttpContent()));
System.out.println(JSON.toJSONString(scrResponse, true));
int requestCode = scrResponse.getIntValue("code");
//每一张图片的检测结果
JSONArray taskResults = scrResponse.getJSONArray("data");
if (200 == requestCode) {
for (Object taskResult : taskResults) {
//单张图片的处理结果
int taskCode = ((JSONObject) taskResult).getIntValue("code");
if (200 == taskCode) {
System.out.println("成功添加1张图片 ");
} else {
//单张图片处理失败, 原因是具体的情况详细分析
System.out.println("task process fail. task response:" + JSON.toJSONString(taskResult));
}
}
} else {
/**
* 表明请求整体处理失败,原因视具体的情况详细分析
*/
System.out.println("the whole image scan request failed. response:" + JSON.toJSONString(scrResponse));
}
}
}
}
在文档使用中是否遇到以下问题
更多建议
匿名提交