Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package;
- import java.net.URI;
- import java.util.Scanner;
- import org.apache.http.HttpEntity;
- import org.apache.http.HttpResponse;
- import org.apache.http.client.methods.HttpPost;
- import org.apache.http.client.utils.URIBuilder;
- import org.apache.http.entity.StringEntity;
- import org.apache.http.impl.client.CloseableHttpClient;
- import org.apache.http.impl.client.HttpClientBuilder;
- import org.apache.http.util.EntityUtils;
- import org.json.JSONObject;
- public class newIoT {
- // **********************************************
- // *** Update or verify the following values. ***
- // **********************************************
- // Replace <Subscription Key> with your valid subscription key.
- private static final String subscriptionKey = "**********************";
- // You must use the same region in your REST call as you used to get your
- // subscription keys. For example, if you got your subscription keys from
- // westus, replace "westcentralus" in the URI below with "westus".
- //
- // Free trial subscription keys are generated in the westcentralus region. If you
- // use a free trial subscription key, you shouldn't need to change this region.
- private static final String uriBase =
- "https://westcentralus.api.cognitive.microsoft.com/vision/v2.0/analyze";
- //private static final String imageToAnalyze =
- //"https://www.fashion-press.net/img/news/38545/Ej4.jpg";
- public static void main(String[] args) {
- System.out.println("タグ、文章を取得したい画像URLを入力");
- Scanner sc = new Scanner(System.in);
- String ImageURL = sc.next();
- CloseableHttpClient httpClient = HttpClientBuilder.create().build();
- try {
- URIBuilder builder = new URIBuilder(uriBase);
- // Request parameters. All of them are optional.
- builder.setParameter("visualFeatures", "Categories,Description,Color");
- builder.setParameter("language", "en");
- // Prepare the URI for the REST API call.
- URI uri = builder.build();
- HttpPost request = new HttpPost(uri);
- // Request headers.
- request.setHeader("Content-Type", "application/json");
- request.setHeader("Ocp-Apim-Subscription-Key", subscriptionKey);
- // Request body.
- StringEntity requestEntity =
- new StringEntity("{"url":"" + ImageURL + ""}");
- request.setEntity(requestEntity);
- // Make the REST API call and get the response entity.
- HttpResponse response = httpClient.execute(request);
- HttpEntity entity = response.getEntity();
Add Comment
Please, Sign In to add comment