Query the list of sample images in a similar-image library
This topic describes how to use the Java software development kit (SDK) to run a paged query for all sample images in a specified similar-image library.
Description
You can call this operation to run a paged query for information about all sample images in a specified similar-image library. For more information about the parameters, see Query the list of sample images.
You must use the Content Moderation endpoint to call the service using the SDK. For more information about API endpoints, see Endpoints.
Prerequisites
Java dependencies are installed. For more information, see Installation.
NoteYou must use the Java version described in the Installation topic to install the dependencies. Otherwise, subsequent operation calls fail.
The Extension.Uploader utility class is downloaded and imported into your project if you submit a local image or a binary image stream for image moderation.
Query the list of sample images
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.ListSimilarityImagesRequest;
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;
public class ListSimilarityImagesSample {
public static void main(String[] args) throws Exception {
/**
* An AccessKey of an Alibaba Cloud account has permissions to call all API operations. Use a Resource Access Management (RAM) user to call API operations or perform routine O&M.
* Common methods to obtain environment variables:
* Method 1:
* Obtain the AccessKey ID of the RAM user: System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
* Obtain the AccessKey secret of the RAM user: System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
* Method 2:
* Obtain the AccessKey ID of the RAM user: System.getProperty("ALIBABA_CLOUD_ACCESS_KEY_ID");
* Obtain the AccessKey secret of the RAM user: System.getProperty("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
*/
DefaultProfile profile = DefaultProfile.getProfile(
"cn-shanghai",
"Obtain the AccessKey ID of the RAM user from an environment variable",
"Obtain the AccessKey secret of the RAM user from an environment variable");
DefaultProfile.addEndpoint("cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
IAcsClient client = new DefaultAcsClient(profile);
ListSimilarityImagesRequest request = new ListSimilarityImagesRequest();
// Specify the API response format and the request method.
request.setAcceptFormat(FormatType.JSON);
request.setMethod(MethodType.POST);
request.setEncoding("utf-8");
request.setProtocol(ProtocolType.HTTP);
// pageSize: the number of entries to return on each page. Valid values: (0, 50]. currentPage: the number of the page to return. Valid values: (0, 50].
JSONObject httpBody = new JSONObject();
httpBody.put("library", "The name of the similar-image library");
httpBody.put("pageSize", "5");
httpBody.put("currentPage", "1");
request.setHttpContent(org.apache.commons.codec.binary.StringUtils.getBytesUtf8(httpBody.toJSONString()),
"UTF-8", FormatType.JSON);
/**
* Set the timeout period. The server-side timeout period for the entire link is 10 seconds. Set the timeout period accordingly.
* If the ReadTimeout value that you set is less than the server-side processing time, a ReadTimeout exception is returned.
*/
request.setConnectTimeout(3000);
request.setReadTimeout(10000);
HttpResponse httpResponse = null;
try {
httpResponse = client.doAction(request);
// The response that is returned after the server receives and processes the request.
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");
if (200 == requestCode) {
// The number of the current page.
int currentPage = scrResponse.getIntValue("currentPage");
// The number of entries per page.
int pageSize = scrResponse.getIntValue("pageSize");
// The total number of images.
int totalCount = scrResponse.getIntValue("totalCount");
// The list of results.
JSONArray results = scrResponse.getJSONArray("data");
for (Object data : results) {
// A single result.
// The timestamp when the image was created.
int createTime = ((JSONObject) data).getIntValue("createTime");
// The unique ID of the image.
String dataId = ((JSONObject) data).getString("dataId");
// The URL of the image.
String url = ((JSONObject) data).getString("url");
// The tags of the sample image.
String tags = ((JSONObject) data).getString("tags");
}
} else {
/**
* The request failed. Analyze the cause based on the specific situation.
*/
System.out.println("the whole request failed. response:" + JSON.toJSONString(scrResponse));
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}