Guest User

Untitled

a guest
Jul 22nd, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. package;
  2. import java.net.URI;
  3. import java.util.Scanner;
  4.  
  5. import org.apache.http.HttpEntity;
  6. import org.apache.http.HttpResponse;
  7. import org.apache.http.client.methods.HttpPost;
  8. import org.apache.http.client.utils.URIBuilder;
  9. import org.apache.http.entity.StringEntity;
  10. import org.apache.http.impl.client.CloseableHttpClient;
  11. import org.apache.http.impl.client.HttpClientBuilder;
  12. import org.apache.http.util.EntityUtils;
  13. import org.json.JSONObject;
  14.  
  15. public class newIoT {
  16.  
  17. // **********************************************
  18. // *** Update or verify the following values. ***
  19. // **********************************************
  20.  
  21. // Replace <Subscription Key> with your valid subscription key.
  22. private static final String subscriptionKey = "**********************";
  23.  
  24. // You must use the same region in your REST call as you used to get your
  25. // subscription keys. For example, if you got your subscription keys from
  26. // westus, replace "westcentralus" in the URI below with "westus".
  27. //
  28. // Free trial subscription keys are generated in the westcentralus region. If you
  29. // use a free trial subscription key, you shouldn't need to change this region.
  30. private static final String uriBase =
  31. "https://westcentralus.api.cognitive.microsoft.com/vision/v2.0/analyze";
  32.  
  33. //private static final String imageToAnalyze =
  34. //"https://www.fashion-press.net/img/news/38545/Ej4.jpg";
  35.  
  36. public static void main(String[] args) {
  37. System.out.println("タグ、文章を取得したい画像URLを入力");
  38.  
  39. Scanner sc = new Scanner(System.in);
  40. String ImageURL = sc.next();
  41.  
  42. CloseableHttpClient httpClient = HttpClientBuilder.create().build();
  43.  
  44. try {
  45. URIBuilder builder = new URIBuilder(uriBase);
  46.  
  47. // Request parameters. All of them are optional.
  48. builder.setParameter("visualFeatures", "Categories,Description,Color");
  49. builder.setParameter("language", "en");
  50.  
  51. // Prepare the URI for the REST API call.
  52. URI uri = builder.build();
  53. HttpPost request = new HttpPost(uri);
  54.  
  55. // Request headers.
  56. request.setHeader("Content-Type", "application/json");
  57. request.setHeader("Ocp-Apim-Subscription-Key", subscriptionKey);
  58.  
  59. // Request body.
  60. StringEntity requestEntity =
  61. new StringEntity("{"url":"" + ImageURL + ""}");
  62. request.setEntity(requestEntity);
  63.  
  64. // Make the REST API call and get the response entity.
  65. HttpResponse response = httpClient.execute(request);
  66. HttpEntity entity = response.getEntity();
Add Comment
Please, Sign In to add comment