搜索 .gif 图像
警告
2020 年 10 月 30 日,必应搜索 API 从 Azure AI 服务迁移到必应搜索服务。 本文档仅供参考。 有关更新的文档,请参阅必应搜索 API 文档。 关于为必应搜索创建新的 Azure 资源的说明,请参阅通过 Azure 市场创建必应搜索资源。
借助必应图像搜索 API,还可以在整个 Web 上搜索最相关的 .gif 图像。 开发人员可以在各种会话场景中集成动人的 GIF。
以下 URL 是对动态 .gif 图像的查询。
https://api.cognitive.microsoft.com/bing/v7.0/images/search?q=interesting&imageType=AnimatedGif&mkt=en-us
q 参数指定搜索词。 前面的查询还使用 imageType 筛选器参数指定 animatedGif
。
若要查看结果的示例,请使用以下 URL 搜索 bing.com。
https://www.bing.com/images/search?q=interesting&qft=%20filterui%3Aphoto-animatedgif
查询参数
有关查询参数和选项的详细信息,请参阅图像搜索 API 参考。 在标题使用 Java 搜索动态 gif 的示例下面有一个示例。
提示和建议
- 可以指定 maxFileSize 和 minFileSize 参数。 我们建议设置 maxFileSize=2000000,因为我们索引中的大多数 gif 都小于 2MB。 如果需要考虑带宽(例如在移动蜂窝场景中),这也有助于控制数据大小。
- 为了帮助提高感知性能,请在加载源 URL 之前先加载缩略图。
- 对于还没有用户查询的首次运行或登陆页面体验,请尝试使用我们的热门 gif 搜索来通过热门图像 API 提供帮助。
- safeSearch 参数有三种设置。
strict
选项会阻止成人内容。 - 有关支持的语言和位置的完整列表,请参阅 mkt。
- AnimatedGifHttps 仅返回来自 https 地址的动态 gif 图像。 为了安全起见,许多应用程序需要通过 https 连接到外部 Web 链接。 例如,Apple App Store 需要通过 HTTPS 连接到 Web 服务,以便在传输过程中安全地加密用户数据。
使用 Java 搜索动态 gif 的示例
以下 URL 搜索动态 .gif 图像:q=interesting
https://api.cognitive.microsoft.com/bing/v7.0/images/search?q=interesting&imageType=AnimatedGif&mkt=en-us
如以下示例所示,URL 查询需要 Ocp-Apim-Subscription-Key 标头。
以下 Java 示例生成并发送请求。
package gifSearch;
import java.net.*;
import java.util.*;
import java.io.*;
import javax.net.ssl.HttpsURLConnection;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
/*
* Gson: https://github.com/google/gson
* Maven info:
* groupId: com.google.code.gson
* artifactId: gson
* version: 2.8.1
*
* Once you have compiled or downloaded gson-2.8.1.jar, assuming you have placed it in the
* same folder as this file (BingImageSearch.java), you can compile and run this program at
* the command line as follows.
*
* javac GIFsearch.java -classpath .;gson-2.8.1.jar -encoding UTF-8
* java -cp .;gson-2.8.1.jar GIFsearch
*/
public class GIFsearch {
// Replace the subscriptionKey string value with your valid subscription key.
static String subscriptionKey = "YOUR-ACCESS-KEY";
static String host = "https://api.cognitive.microsoft.com";
static String path = "/bing/v7.0/images/search";
static String searchTerm = "interesting";
public static SearchResults SearchImages (String searchQuery) throws Exception {
// construct URL of search request (endpoint + query string)
URL url = new URL(host + path + "?q=" + URLEncoder.encode(searchQuery, "UTF-8") + "&imageType=AnimatedGif&mkt=en-us");
HttpsURLConnection connection = (HttpsURLConnection)url.openConnection();
connection.setRequestProperty("Ocp-Apim-Subscription-Key", subscriptionKey);
// receive JSON body
InputStream stream = connection.getInputStream();
String response = new Scanner(stream).useDelimiter("\\A").next();
// construct result object for return
SearchResults results = new SearchResults(new HashMap<String, String>(), response);
// extract Bing-related HTTP headers
Map<String, List<String>> headers = connection.getHeaderFields();
for (String header : headers.keySet()) {
if (header == null) continue; // may have null key
if (header.startsWith("BingAPIs-") || header.startsWith("X-MSEdge-")) {
results.relevantHeaders.put(header, headers.get(header).get(0));
}
}
stream.close();
return results;
}
// pretty-printer for JSON; uses GSON parser to parse and re-serialize
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);
}
public static void main (String[] args) {
if (subscriptionKey.length() != 32) {
System.out.println("Invalid Bing Search API subscription key!");
System.out.println("Please paste yours into the source code.");
System.exit(1);
}
try {
System.out.println("Searching the Web for: " + searchTerm);
SearchResults result = SearchImages(searchTerm);
System.out.println("\nRelevant HTTP Headers:\n");
for (String header : result.relevantHeaders.keySet())
System.out.println(header + ": " + result.relevantHeaders.get(header));
System.out.println("\nJSON Response:\n");
System.out.println(prettify(result.jsonResponse));
}
catch (Exception e) {
e.printStackTrace(System.out);
System.exit(1);
}
}
}
//Container class for search results encapsulates relevant headers and JSON data
class SearchResults{
HashMap<String, String> relevantHeaders;
String jsonResponse;
SearchResults(HashMap<String, String> headers, String json) {
relevantHeaders = headers;
jsonResponse = json;
}
}
结果
该代码获得以下结果作为 JSON 对象:
{
"webSearchUrl": "https://www.bing.com/images/search?view\u003ddetai...",
"name": "Very Interesting GIF - Thats Very Interesting - ...",
"thumbnailUrl": "https://tse1.mm.bing.net/th?id\u003dOIP.yJX6Vz345JPK...",
"datePublished": "2017-03-12T01:35:00.0000000Z",
"contentUrl": "https://media.contoso.co/images/c895fa573df8e493ca8d0dec7d93b/raw",
"hostPageUrl": "https://www.contoso.co/view/thats-very-interesting-christi...",
"contentSize": "1295633 B",
"encodingFormat": "animatedgif",
"hostPageDisplayUrl": "https://www.contoso.co/view/thats-very-christian...",
"width": 440,
"height": 186,
"thumbnail": {
"width": 474,
"height": 200
},
"imageInsightsToken": "ccid_yJX6Vz34*mid_9FF0FFA42AADA1357F042443D2103B40EA...",
"insightsMetadata": {
"recipeSourcesCount": 0,
"bestRepresentativeQuery": {
"text": "That\u0027s Very Interesting",
"displayText": "That\u0027s Very Interesting",
"webSearchUrl": "https://www.bing.com/images/search?q\u003dThat..."
},
"pagesIncludingCount": 19,
"availableSizesCount": 2
},
"imageId": "9FF0FFA42AADA1357F042443D21030EAAA225F",
"accentColor": "62452D"
},