このトピックでは、Alibaba Cloud Image Search SDK forJava の使用方法について説明します。

準備

  • Alibaba Cloud SDK をインストールして使用する前に、Alibaba Cloud アカウントが作成済みであり、AccessKey ペアを取得済みである必要があります。 詳細については、「t13782.html#concept_53045_zh」をご参照ください。
  • 次の操作を実行して、Alibaba Cloud Image Search SDK forJava をプロジェクトに追加します。
    Alibaba Cloud Image Search SDK for Java を Maven 依存関係としてインポートし、SDK をプロジェクトに追加します。
    <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 デモ

Alibaba Cloud Image Search SDK for Java のデモをダウロードします。

クライアントの作成

  1. Image Search インスタンスにアクセスするためのエンドポイントを指定します。 この例では、Image Search インスタンスは中国 (上海) リージョンに存在します。
    DefaultProfile.addEndpoint("cn-shanghai", "cn-shanghai", "ImageSearch", "imagesearch.cn-shanghai.aliyuncs.com");
  2. プロファイルオブジェクトを作成します。 IClientProfile のプロファイルオブジェクトを作成します。 このオブジェクトには、AccessKey ID、AccessKey Secret、リージョンに関する情報が含まれています。
    IClientProfile profile = DefaultProfile.getProfile("cn-shanghai", ak, aks);
  3. クライアントを作成します。 IClientProfile クラスから client という名前の IAcsClient オブジェクトを作成します。 後続のレスポンスは IAcsClient から取得されます。
    IAcsClient client = new DefaultAcsClient(profile);

画像の追加

画像を追加する場合:
  • 商品画像を検索する場合、カテゴリ ID を指定することも指定しないこともできます。
  • 一般的な画像検索の場合、カテゴリ ID を指定する必要はありません。
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();
}

画像の検索

画像を検索する場合:
  • 商品画像を検索する場合、カテゴリ ID を指定することも指定しないこともできます。 カテゴリ ID を指定すると、指定したカテゴリ ID に基づいて画像が検索されます。 カテゴリ ID を指定しない場合、システムによって予測されたカテゴリ ID に基づいて画像を検索します。
  • 一般的な画像検索の場合、カテゴリ ID を指定する必要はありません。
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();
}

画像の削除

次のコード例は画像を削除するために使用されます。
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();
}