This topic describes how to use Alibaba Cloud Image Search SDKs for Java.

Preparations

  • Before you install and use Alibaba Cloud SDKs, make sure that you have created an Alibaba Cloud account and obtained an AccessKey pair. For more information, see Create an AccessKey pair.
  • Add the Alibaba Cloud Image Search SDK for Java to your project by performing the following operations:
    Import the Alibaba Cloud Image Search SDK for Java as a Maven dependency, and add the SDK to your project.
    <dependency>
        <groupId>com.aliyun</groupId>
        <artifactId>aliyun-java-sdk-imagesearch</artifactId>
        <version>1.0.4</version>
    </dependency>
    <dependency>
        <groupId>com.aliyun</groupId>
        <artifactId>aliyun-java-sdk-core</artifactId>
        <version>3.2.8</version>
    </dependency>

SDK demo

Download the demo of the Alibaba Cloud Image Search SDK for Java.

Create a client

  1. Specify an endpoint to access your Image Search instance. In this example, the Image Search instance resides in the China (Shanghai) region.
    DefaultProfile.addEndpoint("cn-shanghai", "cn-shanghai", "ImageSearch", "imagesearch.cn-shanghai.aliyuncs.com");
  2. Create a profile object. Create a profile object for IClientProfile. This object contains information about the AccessKey ID, the AccessKey secret, and the region.
    IClientProfile profile = DefaultProfile.getProfile("cn-shanghai", ak, aks);
  3. Create the client. Create an IAcsClient object named client from the IClientProfile class. Subsequent responses are retrieved from IAcsClient.
    IAcsClient client = new DefaultAcsClient(profile);

Add images

When you add images:
  • For product image searches, you can specify or do not specify category IDs.
  • For generic image searches, you do not need to specify category IDs.
AddItemRequest request = new AddItemRequest();
request.setInstanceName("testinstance");
request.setCatId("0");
request.setItemId("1000");
request.setCustContent("{\"key\":\"value\"}");

String dressPicPath = "./resources/dress.jpg";
String clothesPicPath = "./resources/clothes.jpg";

// The image content is read as byte streams. The key represents the image name and the value represents the image content.
request.addPicture("dress", getBytes(dressPicPath));
request.addPicture("clothes", getBytes(clothesPicPath));

// Use the body structure specified in the Image Search API to build the preceding fields. If the request is successful, the system returns true. If the request fails, the system returns false.
if (! request.buildPostContent()) {
        System.out.println("build post content failed.");
            return;
}
AddItemResponse response = null;
try {
        response = client.getAcsResponse(request);
        System.out.println(response.getSuccess());
        System.out.println(response.getRequestId());
        System.out.println(response.getCode());
        System.out.println(response.getMessage());
} catch (ServerException e) {    // An exception occurs due to a server error.
    e.printStackTrace();
    } catch (ClientException e) {    // An exception occurs due to a client error, such as invalid parameters or unavailable instances.
    e.printStackTrace();
}

Search for images

When your search for images:
  • For product image searches, you can specify or do not specify category IDs. If you specify a category ID, the system searches for images based on the specified category ID. If you do not specify a category ID, the system predicts the category ID and searches for images based on the predicted category ID.
  • For generic image searches, you do not need to specify category IDs.
SearchItemRequest request = new SearchItemRequest();
request.setCatId("0");
request.setInstanceName("testintance");
request.setNum(10);
request.setStart(0);

String filePath = "./resources/dress.jpg";
byte[] bytes = getBytes(filePath);
request.setSearchPicture(bytes);

if (! request.buildPostContent()) {
    System.out.println("build post content failed.");
    return;
}

SearchItemResponse response = null;
try {
    response = client.getAcsResponse(request);
    
    System.out.println(response.getSuccess());
    System.out.println(response.getRequestId());
    System.out.println(response.getCode());
    System.out.println(response.getMessage());
    System.out.println(JSONObject.toJSONString(response.getHead()));
    System.out.println(JSONObject.toJSONString(response.getPicInfo()));
    System.out.println(JSONObject.toJSONString(response.getAuctions()));
} catch (ServerException e) {
     e.printStackTrace();
} catch (ClientException e) {
        e.printStackTrace();
}

Delete images

The following sample code is used to delete images:
DeleteItemRequest request = new DeleteItemRequest();
request.setInstanceName("testinstance");
request.setItemId("1000");
request.addPicture("shoes1.jpg");
request.addPicture("shoes2.jpg");

if (! request.buildPostContent()) {
    System.out.println("build post content failed.");
    return;
}

DeleteItemResponse response = null;
try {
    response = client.getAcsResponse(request);
    
    System.out.println(response.getSuccess());
    System.out.println(response.getRequestId());
    System.out.println(response.getCode());
    System.out.println(response.getMessage());
} catch (ServerException e) {
        e.printStackTrace();
} catch (ClientException e) {
        e.printStackTrace();
}