1- //Copyright (c) Microsoft Corporation. All rights reserved.
2- //Licensed under the MIT License.
3-
4- // This sample uses the Apache HTTP client library(org.apache.httpcomponents:httpclient:4.2.4)
5- // and the org.json library (org.json:json:20170516). You can download these from Maven repo into lib directory of your project.
1+ // Copyright (c) Microsoft Corporation. All rights reserved.
2+ // Licensed under the MIT License.
63
74import java .net .URI ;
85import org .apache .http .HttpEntity ;
1512import org .apache .http .util .EntityUtils ;
1613import org .json .JSONObject ;
1714
15+ /**
16+ * Include these library jars (or newer +), add to a lib folder:
17+ * commons-logging-4.0.6+
18+ * httpclient-4.5.3+
19+ * httpcore-4.4.13+
20+ * json-20190722+
21+ *
22+ * To compile and run from command line:
23+ * javac Main.java -cp .;lib\*
24+ * java -cp .;lib\* Main
25+ */
26+
1827public class Main
1928{
20- // **********************************************
21- // *** Update or verify the following values. ***
22- // **********************************************
29+ // Add your Computer Vision subscription key and endpoint to your environment variables.
30+ private static String subscriptionKey = System . getenv ( "COMPUTER_VISION_SUBSCRIPTION_KEY" );
31+ private static String baseUrl = System . getenv ( "COMPUTER_VISION_ENDPOINT" );
2332
24- // Replace the subscriptionKey string value with your valid subscription key.
25- public static final String subscriptionKey = "Enter key here" ;
26-
27- // Replace or verify the region.
28- //
29- // You must use the same region in your REST API call as you used to obtain your subscription keys.
30- // For example, if you obtained your subscription keys from the westus region, replace
31- // "westcentralus" in the URI below with "westus".
32- //
33- // NOTE: Free trial subscription keys are generated in the westcentralus region, so if you are using
34- // a free trial subscription key, you should not need to change this region.
35- public static final String uriBase = "https://westcentralus.api.cognitive.microsoft.com/vision/v1.0/analyze" ;
33+ private static final String endpoint = baseUrl + "/vision/v2.1/analyze" ;
3634
35+ // Image of an entity (well-known place)
36+ private static String image = "https://raw.githubusercontent.com/Azure-Samples/cognitive-services-sample-data-files/master/ComputerVision/Images/landmark.jpg" ;
3737
3838 public static void main (String [] args )
3939 {
4040 HttpClient httpclient = new DefaultHttpClient ();
4141
4242 try
4343 {
44- URIBuilder builder = new URIBuilder (uriBase );
44+ URIBuilder builder = new URIBuilder (endpoint );
4545
4646 // Request parameters. All of them are optional.
4747 builder .setParameter ("visualFeatures" , "Categories,Description,Color" );
@@ -56,7 +56,7 @@ public static void main(String[] args)
5656 request .setHeader ("Ocp-Apim-Subscription-Key" , subscriptionKey );
5757
5858 // Request body.
59- StringEntity reqEntity = new StringEntity ("{\" url\" :\" https://upload.wikimedia.org/wikipedia/commons/1/12/Broadway_and_Times_Square_by_night.jpg \" }" );
59+ StringEntity reqEntity = new StringEntity ("{\" url\" : \" " + image + " \" }" );
6060 request .setEntity (reqEntity );
6161
6262 // Execute the REST API call and get the response entity.
@@ -77,5 +77,6 @@ public static void main(String[] args)
7777 // Display error message.
7878 System .out .println (e .getMessage ());
7979 }
80- }
80+ }
81+
8182}
0 commit comments