All Products
Search
Document Center

Examples

Last Updated: Jan 22, 2019

Download Demo

Prerequisites

  1. Obtain the Access Key ID and Access Key Secret.Log on to the Image Search console, hover the cursor over your avatar in the upper-right corner, and select accesskeys to view your Access Key ID and Access Key Secret.

  2. Add the Image Search SDK.
    Import the Image Search SDK as a Maven dependency through Maven.

    1. <dependency>
    2. <groupId>com.aliyun</groupId>
    3. <artifactId>aliyun-java-sdk-imagesearch</artifactId>
    4. <version>1.0.4</version>
    5. </dependency>
    6. <dependency>
    7. <groupId>com.aliyun</groupId>
    8. <artifactId>aliyun-java-sdk-core</artifactId>
    9. <version>3.2.8</version>
    10. </dependency>

Create a client

  1. Add an endpoint to Image Search.

    In this example, an endpoint is added to the image search instance for the China (Shanghai) region.

    1. DefaultProfile.addEndpoint("cn-shanghai", "cn-shanghai", "ImageSearch", "imagesearch.cn-shanghai.aliyuncs.com");
  2. Create a profile.

    Create an instance of IClientProfile named profile. This object contains data such as the Access Key ID, Access Key Secret, and region.

    1. IClientProfile profile = DefaultProfile.getProfile("cn-shanghai", ak, aks);
  3. Create a client.

    Create an IAcsClient object using the IClientProfile class. You will use the IAcsClient object to receive responses.

    1. IAcsClient client = new DefaultAcsClient(profile);

Add images

For product search instances, category IDs are not necessary. For generic search instances, no category ID is required.

  1. AddItemRequest request = new AddItemRequest();
  2. request.setInstanceName("testinstance");
  3. request.setCatId("0");
  4. request.setItemId("1000");
  5. request.setCustContent("{\"key\":\"value\"}");
  6. String dressPicPath = "./resources/dress.jpg";
  7. String clothesPicPath = "./resources/clothes.jpg";
  8. // The image content is input by using byte stream. The key represents the image name and the value represents the image content.
  9. request.addPicture("dress", getBytes(dressPicPath));
  10. request.addPicture("clothes", getBytes(clothesPicPath));
  11. // Use the body structure specified in the Image Search API to build the proceeding fields. If the request is successful, the system returns true. If the request is failed, the system returns false.
  12. if (!request.buildPostContent()) {
  13. System.out.println("build post content failed.");
  14. return;
  15. }
  16. AddItemResponse response = null;
  17. try {
  18. response = client.getAcsResponse(request);
  19. System.out.println(response.getSuccess());
  20. System.out.println(response.getRequestId());
  21. System.out.println(response.getCode());
  22. System.out.println(response.getMessage());
  23. } catch (ServerException e) { // An exception occurs when a server error occurs.
  24. e.printStackTrace();
  25. } catch (ClientException e) { // An exception occurs when a client error occurs, such as invalid parameters or unavailable instances.
  26. e.printStackTrace();
  27. }

Search images

When you use product search, you can specify a category ID. The system will perform a lookup only on products that belong to the specified category. If you do not specify a category ID, the system will determine the category for you. For generic search, no category ID is required.

  1. SearchItemRequest request = new SearchItemRequest();
  2. request.setCatId("0");
  3. request.setInstanceName("testintance");
  4. request.setNum(10);
  5. request.setStart(0);
  6. String filePath = "./resources/dress.jpg";
  7. byte[] bytes = getBytes(filePath);
  8. request.setSearchPicture(bytes);
  9. if (!request.buildPostContent()) {
  10. System.out.println("build post content failed.");
  11. return;
  12. }
  13. SearchItemResponse response = null;
  14. try {
  15. response = client.getAcsResponse(request);
  16. System.out.println(response.getSuccess());
  17. System.out.println(response.getRequestId());
  18. System.out.println(response.getCode());
  19. System.out.println(response.getMessage());
  20. System.out.println(JSONObject.toJSONString(response.getHead()));
  21. System.out.println(JSONObject.toJSONString(response.getPicInfo()));
  22. System.out.println(JSONObject.toJSONString(response.getAuctions()));
  23. } catch (ServerException e) {
  24. e.printStackTrace();
  25. } catch (ClientException e) {
  26. e.printStackTrace();
  27. }

Delete images

  1. DeleteItemRequest request = new DeleteItemRequest();
  2. request.setInstanceName("testinstance");
  3. request.setItemId("1000");
  4. request.addPicture("shoes1.jpg");
  5. request.addPicture("shoes2.jpg");
  6. if (!request.buildPostContent()) {
  7. System.out.println("build post content failed.");
  8. return;
  9. }
  10. DeleteItemResponse response = null;
  11. try {
  12. response = client.getAcsResponse(request);
  13. System.out.println(response.getSuccess());
  14. System.out.println(response.getRequestId());
  15. System.out.println(response.getCode());
  16. System.out.println(response.getMessage());
  17. } catch (ServerException e) {
  18. e.printStackTrace();
  19. } catch (ClientException e) {
  20. e.printStackTrace();
  21. }