快速入門:使用 Bing 圖像式搜尋 REST API 和 Java 來取得影像見解

警告

在 2020 年 10 月 30 日,Bing 搜尋 API 已從 Azure AI 服務移至 Bing 搜尋 服務。 本文件僅供參考之用。 如需更新的文件,請參閱 Bing 搜尋 API 文件。 如需針對 Bing 搜尋建立新 Azure 資源的指示,請參閱透過 Azure Marketplace 建立 Bing 搜尋資源

使用本快速入門,第一次呼叫 Bing 圖像式搜尋 API。 此 Java 應用程式會將影像上傳至 API,並顯示它傳回的資訊。 雖然此應用程式是以 Java 撰寫的,但 API 是一種與大多數程式設計語言都相容的 RESTful Web 服務。

Prerequisites

建立 Azure 資源

藉由建立下列其中一項 Azure 資源,開始使用 Bing 圖像式搜尋 API:

Bing 搜尋 v7 資源

  • 您可以透過 Azure 入口網站取得該資源,直到將其刪除為止。
  • 選取 S9 定價層。

多服務資源

  • 您可以透過 Azure 入口網站取得該資源,直到將其刪除為止。
  • 針對您的應用程式,跨多個 Azure AI 服務使用相同的金鑰和端點。

建立專案並將其初始化

  1. 在您慣用的 IDE 或編輯器中建立新的 Java 專案,並匯入下列程式庫:

    import java.util.*;
    import java.io.*;
    import com.google.gson.Gson;
    import com.google.gson.GsonBuilder;
    import com.google.gson.JsonObject;
    import com.google.gson.JsonParser;
    
    // HttpClient libraries
    
    import org.apache.http.HttpEntity;
    import org.apache.http.HttpResponse;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.entity.ContentType;
    import org.apache.http.entity.mime.MultipartEntityBuilder;
    import org.apache.http.impl.client.CloseableHttpClient;
    import org.apache.http.impl.client.HttpClientBuilder;
    
  2. 為您的 API 端點、訂用帳戶金鑰以及您影像的路徑,建立變數。 對於 endpoint 值,您可以使用下列程式碼中的全域端點,或使用 Azure 入口網站中針對您的資源所顯示的自訂子網域端點。

    static String endpoint = "https://api.cognitive.microsoft.com/bing/v7.0/images/visualsearch";
    static String subscriptionKey = "your-key-here";
    static String imagePath = "path-to-your-image";
    
  3. 上傳本機影像時,表單資料必須包含 Content-Disposition 標頭。 將其 name 參數設為 "image",以及將 filename 參數設為影像的檔案名稱。 表單的內容包含影像的二進位資料。 您可以上傳的影像大小上限為 1 MB。

    --boundary_1234-abcd
    Content-Disposition: form-data; name="image"; filename="myimagefile.jpg"
    
    ÿØÿà JFIF ÖÆ68g-¤CWŸþ29ÌÄøÖ‘º«™æ±èuZiÀ)"óÓß°Î= ØJ9á+*G¦...
    
    --boundary_1234-abcd--
    

建立 JSON 剖析器

建立一個方法,讓來自 API 的 JSON 回應更容易使用 JsonParser 閱讀。

public static String prettify(String json_text) {
        JsonParser parser = new JsonParser();
        JsonObject json = parser.parse(json_text).getAsJsonObject();
        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        return gson.toJson(json);
    }

建構搜尋要求和查詢

  1. 在應用程式的 Main 方法中,使用 HttpClientBuilder.create().build(); 建立 HTTP 用戶端。

    CloseableHttpClient httpClient = HttpClientBuilder.create().build();
    
  2. 建立 HttpEntity 物件以將影像上傳至 API。

    HttpEntity entity = MultipartEntityBuilder
        .create()
        .addBinaryBody("image", new File(imagePath))
        .build();
    
  3. 建立端點的 httpPost 物件,並設定要使用您訂用帳戶金鑰的標頭。

    HttpPost httpPost = new HttpPost(endpoint);
    httpPost.setHeader("Ocp-Apim-Subscription-Key", subscriptionKey);
    httpPost.setEntity(entity);
    

接收及處理 JSON 回應

  1. 使用 HttpClient.execute() 方法將要求傳送給 API,並且在 InputStream 物件中儲存回應。

    HttpResponse response = httpClient.execute(httpPost);
    InputStream stream = response.getEntity().getContent();
    
  2. 儲存 JSON 字串並列印回應。

    String json = new Scanner(stream).useDelimiter("\\A").next();
    System.out.println("\nJSON Response:\n");
    System.out.println(prettify(json));
    

後續步驟