Bing Image Search API
The Bing Image Search API provides a similar (but not exact) experience to Bing.com/Images (overview on MSDN). The Image Search API lets partners send a search query to Bing and get back a list of relevant images.
To get started, read our Getting Started guide, which describes how you can obtain your own subscription keys and start making calls to the API. If you already have a subscription, try our API Testing Console API Testing Console where you can easily craft API requests in a sandbox environment.
For information that shows you how to use the Image Search API, see Image Search API Guide on MSDN. You can also refer to a bot sample here that uses this API to find visually similar products.
For information about the programming elements that you'd use to request and consume the search results, see Image Search API Reference.
For additional guide and reference content that is common to all Bing APIs, such as Paging Results and Error Codes, see Shared Guides and Shared Reference.
Below is an example to search images from Bing and get results in a ArrayList
public abstract class BingImageSearch
{
private String getSearchUrlBing()
{
return "https://api.cognitive.microsoft.com/bing/v5.0/images/search?q=" + getSearchKeyword() +
"&count=" + getCount() +
"&mkt=en-us" +
"&size=" + getSize() +
"&safeSearch=" + getSearchType();
}
public void execute()
{
new AsyncTask<Void, Void, List<ImageSearchModel>>()
{
@Override
protected List<ImageSearchModel> doInBackground(Void... voids)
{
String result = performGetCallBingImageSearch(getSearchUrlBing());
if (result.isEmpty())
{
return null;
}
return ImageSearchModel.parseJsonArrayBing(result);
}
@Override
protected void onPostExecute(List<ImageSearchModel> aVoid)
{
if (aVoid != null)
{
showSearchResult(aVoid);
}
else
{
showSearchResult(new ArrayList<ImageSearchModel>());
}
super.onPostExecute(aVoid);
}
}.execute();
}
private String performGetCallBingImageSearch(String url)
{
StringBuffer response = null;
try
{
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setConnectTimeout(25000);
// optional default is GET
con.setRequestMethod("GET");
con.setRequestProperty("Ocp-Apim-Subscription-Key", "YOUR_KEY");
int responseCode = con.getResponseCode();
System.out.println("\nSending 'GET' request to URL : " + url);
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
response = new StringBuffer();
while ((inputLine = in.readLine()) != null)
{
response.append(inputLine);
}
in.close();
}
catch (Exception e)
{
e.printStackTrace();
return "";
}
//print result
return response.toString(); //this is your response
}
protected int getCount()
{
return 20;
}
protected String getSize()
{
return ImageSize.Medium;
}
protected String getSearchType()
{
return SearchType.Off;
}
// Off — Return images with adult content. The Image API response will include thumbnail images that are clear (non-fuzzy); however, the Search API response will include thumbnail images that are pixelated (fuzzy).
//
// Moderate — For Image API requests, do not return images with adult content. For Search API requests, return images with adult content (the thumbnail images will be pixelated (fuzzy)).
//
// Strict — Do not return images with adult content.
@interface SearchType
{
String Off = "Off";
String Moderate = "Moderate";
String Strict = "Strict";
}
@interface ImageSize
{
String Small = "Small";
String Medium = "Medium";
String Large = "Large";
}
protected abstract String getSearchKeyword();
protected abstract void showSearchResult(List<ImageSearchModel> list);
}
//ImageSearchModel --> is your model class, add variables according to your need. like imageName,imagePath etc.
Example:
new BingImageSearch()
{
@Override
protected String getSearchKeyword()
{
return "Cars";
}
@Override
protected void showSearchResult(List<ImageSearchModel> list)
{
// list-- ArrayList with results.
}
}.execute();
Learn more about parameters here.
The Bing Image Search API provides a similar (but not exact) experience to Bing.com/Images (overview on MSDN). The Image Search API lets partners send a search query to Bing and get back a list of relevant images.
To get started, read our Getting Started guide, which describes how you can obtain your own subscription keys and start making calls to the API. If you already have a subscription, try our API Testing Console API Testing Console where you can easily craft API requests in a sandbox environment.
For information that shows you how to use the Image Search API, see Image Search API Guide on MSDN. You can also refer to a bot sample here that uses this API to find visually similar products.
For information about the programming elements that you'd use to request and consume the search results, see Image Search API Reference.
For additional guide and reference content that is common to all Bing APIs, such as Paging Results and Error Codes, see Shared Guides and Shared Reference.
Below is an example to search images from Bing and get results in a ArrayList
public abstract class BingImageSearch
{
private String getSearchUrlBing()
{
return "https://api.cognitive.microsoft.com/bing/v5.0/images/search?q=" + getSearchKeyword() +
"&count=" + getCount() +
"&mkt=en-us" +
"&size=" + getSize() +
"&safeSearch=" + getSearchType();
}
public void execute()
{
new AsyncTask<Void, Void, List<ImageSearchModel>>()
{
@Override
protected List<ImageSearchModel> doInBackground(Void... voids)
{
String result = performGetCallBingImageSearch(getSearchUrlBing());
if (result.isEmpty())
{
return null;
}
return ImageSearchModel.parseJsonArrayBing(result);
}
@Override
protected void onPostExecute(List<ImageSearchModel> aVoid)
{
if (aVoid != null)
{
showSearchResult(aVoid);
}
else
{
showSearchResult(new ArrayList<ImageSearchModel>());
}
super.onPostExecute(aVoid);
}
}.execute();
}
private String performGetCallBingImageSearch(String url)
{
StringBuffer response = null;
try
{
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setConnectTimeout(25000);
// optional default is GET
con.setRequestMethod("GET");
con.setRequestProperty("Ocp-Apim-Subscription-Key", "YOUR_KEY");
int responseCode = con.getResponseCode();
System.out.println("\nSending 'GET' request to URL : " + url);
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
response = new StringBuffer();
while ((inputLine = in.readLine()) != null)
{
response.append(inputLine);
}
in.close();
}
catch (Exception e)
{
e.printStackTrace();
return "";
}
//print result
return response.toString(); //this is your response
}
protected int getCount()
{
return 20;
}
protected String getSize()
{
return ImageSize.Medium;
}
protected String getSearchType()
{
return SearchType.Off;
}
// Off — Return images with adult content. The Image API response will include thumbnail images that are clear (non-fuzzy); however, the Search API response will include thumbnail images that are pixelated (fuzzy).
//
// Moderate — For Image API requests, do not return images with adult content. For Search API requests, return images with adult content (the thumbnail images will be pixelated (fuzzy)).
//
// Strict — Do not return images with adult content.
@interface SearchType
{
String Off = "Off";
String Moderate = "Moderate";
String Strict = "Strict";
}
@interface ImageSize
{
String Small = "Small";
String Medium = "Medium";
String Large = "Large";
}
protected abstract String getSearchKeyword();
protected abstract void showSearchResult(List<ImageSearchModel> list);
}
//ImageSearchModel --> is your model class, add variables according to your need. like imageName,imagePath etc.
Example:
new BingImageSearch()
{
@Override
protected String getSearchKeyword()
{
return "Cars";
}
@Override
protected void showSearchResult(List<ImageSearchModel> list)
{
// list-- ArrayList with results.
}
}.execute();
Learn more about parameters here.
0 comments:
Post a Comment