All Products
Search
Document Center

ImageAsyncScanResultsRequest

Last Updated: Jun 06, 2019

Interface description

An interface used to obtain the asynchronous image moderation results

SDK interface

  1. ImageAsyncScanResultsRequest

region

  1. ap-southeast-1

Depended interface

ImageAsyncScanResultsRequest is applied after ImageAsyncScanRequest is used to perform asynchronous image moderation (the interface assigns a taskId to every image), and obtains asynchronous image moderation results based on the taskIds.

Sample code

  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import com.alibaba.fastjson.JSON;
  4. import com.alibaba.fastjson.JSONArray;
  5. import com.alibaba.fastjson.JSONObject;
  6. import com.aliyuncs.DefaultAcsClient;
  7. import com.aliyuncs.IAcsClient;
  8. import com.aliyuncs.exceptions.ClientException;
  9. import com.aliyuncs.exceptions.ServerException;
  10. import com.aliyuncs.green.model.v20170112.ImageAsyncScanResultsRequest;
  11. import com.aliyuncs.http.FormatType;
  12. import com.aliyuncs.http.HttpResponse;
  13. import com.aliyuncs.profile.DefaultProfile;
  14. import com.aliyuncs.profile.IClientProfile;
  15. /**
  16. * Created by liuhai.lh on 2017/2/17.
  17. * ImageAsyncScanResultsRequest
  18. * @author liuhai.lh
  19. * @date 2017/02/17
  20. */
  21. public class ImageAsyncScanResultsSample extends BaseSample{
  22. public static void main(String[] args) throws Exception {
  23. //Replace with your accessKeyId and accessKeySecret
  24. IClientProfile profile = DefaultProfile.getProfile("ap-southeast-1", "your accessKeyId", "your accessKeySecret");
  25. DefaultProfile.addEndpoint("ap-southeast-1", "ap-southeast-1", "Green", "green.ap-southeast-1.aliyuncs.com");
  26. IAcsClient client = new DefaultAcsClient(profile);
  27. ImageAsyncScanResultsRequest imageAsyncScanResultsRequest = new ImageAsyncScanResultsRequest();
  28. imageAsyncScanResultsRequest.setAcceptFormat(FormatType.JSON); // set the returned format
  29. imageAsyncScanResultsRequest.setContentType(FormatType.JSON);
  30. imageAsyncScanResultsRequest.setMethod(com.aliyuncs.http.MethodType.POST); // set request method
  31. imageAsyncScanResultsRequest.setEncoding("utf-8");
  32. imageAsyncScanResultsRequest.setRegionId("ap-southeast-1");
  33. List<String> taskIds = new ArrayList<String>();
  34. taskIds.add("img68DrTo03b$A71qx$57Iq$0-1m@Njn");
  35. imageAsyncScanResultsRequest.setContent(JSON.toJSONString(taskIds).getBytes("UTF-8"), "UTF-8", FormatType.JSON);
  36. /**
  37. * Set timeout
  38. */
  39. imageAsyncScanResultsRequest.setConnectTimeout(3000);
  40. imageAsyncScanResultsRequest.setReadTimeout(6000);
  41. try {
  42. HttpResponse httpResponse = client.doAction(imageAsyncScanResultsRequest);
  43. if(httpResponse.isSuccess()){
  44. JSONObject scrResponse = JSON.parseObject(new String(httpResponse.getContent(), "UTF-8"));
  45. System.out.println(JSON.toJSONString(scrResponse, true));
  46. if (200 == scrResponse.getInteger("code")) {
  47. JSONArray taskResults = scrResponse.getJSONArray("data");
  48. for (Object taskResult : taskResults) {
  49. if(200 == ((JSONObject)taskResult).getInteger("code")){
  50. JSONArray sceneResults = ((JSONObject)taskResult).getJSONArray("results");
  51. for (Object sceneResult : sceneResults) {
  52. String scene = ((JSONObject)sceneResult).getString("scene");
  53. String suggestion = ((JSONObject)sceneResult).getString("suggestion");
  54. //Take actions accodring to suggestion in returned results
  55. //do something
  56. }
  57. }else{
  58. System.out.println("task process fail:" + ((JSONObject)taskResult).getInteger("code"));
  59. }
  60. }
  61. } else {
  62. System.out.println("detect not success. code:" + scrResponse.getInteger("code"));
  63. }
  64. }else{
  65. System.out.println("response not success. status:" + httpResponse.getStatus());
  66. }
  67. } catch (ServerException e) {
  68. e.printStackTrace();
  69. } catch (ClientException e) {
  70. e.printStackTrace();
  71. } catch (Exception e) {
  72. e.printStackTrace();
  73. }
  74. }
  75. }