Skip to content

Commit 04e2e1e

Browse files
authored
Update java-thumb.md
1 parent ea8d9ee commit 04e2e1e

File tree

1 file changed

+53
-46
lines changed
  • articles/cognitive-services/Computer-vision/QuickStarts

1 file changed

+53
-46
lines changed

articles/cognitive-services/Computer-vision/QuickStarts/java-thumb.md

Lines changed: 53 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -30,56 +30,61 @@ To create and run the sample, do the following steps:
3030

3131
1. Create a new Java project in your favorite IDE or editor. If the option is available, create the Java project from a command line application template.
3232
1. Import the following libraries into your Java project. If you're using Maven, the Maven coordinates are provided for each library.
33-
- [Apache HTTP client](https://hc.apache.org/downloads.cgi) (org.apache.httpcomponents:httpclient:4.5.5)
34-
- [Apache HTTP core](https://hc.apache.org/downloads.cgi) (org.apache.httpcomponents:httpcore:4.4.9)
33+
- [Apache HTTP client](https://hc.apache.org/downloads.cgi) (org.apache.httpcomponents:httpclient:4.5.X)
34+
- [Apache HTTP core](https://hc.apache.org/downloads.cgi) (org.apache.httpcomponents:httpcore:4.4.X)
3535
- [JSON library](https://github.com/stleary/JSON-java) (org.json:json:20180130)
36-
1. Add the following `import` statements to the file that contains the `Main` public class for your project.
36+
1. Add the following `import` statements to your main class:
3737

3838
```java
39-
import java.awt.*;
40-
import javax.swing.*;
41-
import java.net.URI;
42-
import java.io.InputStream;
43-
import javax.imageio.ImageIO;
44-
import java.awt.image.BufferedImage;
45-
import org.apache.http.HttpEntity;
46-
import org.apache.http.HttpResponse;
47-
import org.apache.http.client.methods.HttpPost;
48-
import org.apache.http.entity.StringEntity;
49-
import org.apache.http.client.utils.URIBuilder;
50-
import org.apache.http.impl.client.CloseableHttpClient;
51-
import org.apache.http.impl.client.HttpClientBuilder;
52-
import org.apache.http.util.EntityUtils;
53-
import org.json.JSONObject;
39+
import java.awt.*;
40+
import javax.swing.*;
41+
import java.net.URI;
42+
import java.io.InputStream;
43+
import javax.imageio.ImageIO;
44+
import java.awt.image.BufferedImage;
45+
import org.apache.http.HttpEntity;
46+
import org.apache.http.HttpResponse;
47+
import org.apache.http.client.methods.HttpPost;
48+
import org.apache.http.entity.StringEntity;
49+
import org.apache.http.client.utils.URIBuilder;
50+
import org.apache.http.impl.client.CloseableHttpClient;
51+
import org.apache.http.impl.client.HttpClientBuilder;
52+
import org.apache.http.util.EntityUtils;
53+
import org.json.JSONObject;
5454
```
5555

56-
1. Replace the `Main` public class with the following code.
57-
1. Optionally, replace the value of `imageToAnalyze` with the URL of a different image for which you want to generate a thumbnail.
56+
1. Add the rest of the sample code below, beneath the imports (change to your class name if needed).
57+
1. Add your Computer Vision subscription key and endpoint to your environment variables.
58+
1. Optionally, replace the value of `imageToAnalyze` with the URL of your own image.
5859
1. Save, then build the Java project.
59-
1. If you're using an IDE, run `Main`. Otherwise, open a command prompt window and then use the `java` command to run the compiled class. For example, `java Main`.
60+
1. If you're using an IDE, run `GenerateThumbnail`. Otherwise, run from the command line (commands below).
6061

6162
```java
62-
// This sample uses the following libraries:
63-
// - Apache HTTP client (org.apache.httpcomponents:httpclient:4.5.5)
64-
// - Apache HTTP core (org.apache.httpcomponents:httpccore:4.4.9)
65-
// - JSON library (org.json:json:20180130).
66-
67-
68-
public class Main {
69-
// **********************************************
70-
// *** Update or verify the following values. ***
71-
// **********************************************
72-
73-
// Add your Computer Vision subscription key and endpoint to your environment variables.
74-
// After setting, close and then re-open your command shell or project for the changes to take effect.
75-
String subscriptionKey = System.getenv("COMPUTER_VISION_SUBSCRIPTION_KEY");
76-
String endpoint = ("COMPUTER_VISION_ENDPOINT");
77-
78-
private static final String uriBase = endpoint +
79-
"vision/v2.1/generateThumbnail";
80-
81-
private static final String imageToAnalyze =
82-
"https://upload.wikimedia.org/wikipedia/commons/9/94/Bloodhound_Puppy.jpg";
63+
/**
64+
* This sample uses the following libraries (create a "lib" folder to place them in):
65+
* Apache HTTP client:
66+
* org.apache.httpcomponents:httpclient:4.5.X
67+
* Apache HTTP core:
68+
* org.apache.httpcomponents:httpccore:4.4.X
69+
* JSON library:
70+
* org.json:json:20180130
71+
*
72+
* To build/run from the command line:
73+
* javac GenerateThumbnail.java -cp .;lib\*
74+
* java -cp .;lib\* GenerateThumbnail
75+
*/
76+
77+
public class GenerateThumbnail {
78+
79+
// Add your Computer Vision subscription key and endpoint to your environment
80+
// variables. Then, close and then re-open your command shell or project for the
81+
// changes to take effect.
82+
private static String subscriptionKey = System.getenv("COMPUTER_VISION_SUBSCRIPTION_KEY");
83+
private static String endpoint = System.getenv("COMPUTER_VISION_ENDPOINT");
84+
// The endpoint path
85+
private static final String uriBase = endpoint + "vision/v3.0/generateThumbnail";
86+
// It's optional if you'd like to use your own image instead of this one.
87+
private static final String imageToAnalyze = "https://upload.wikimedia.org/wikipedia/commons/9/94/Bloodhound_Puppy.jpg";
8388

8489
public static void main(String[] args) {
8590
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
@@ -101,14 +106,15 @@ public class Main {
101106
request.setHeader("Ocp-Apim-Subscription-Key", subscriptionKey);
102107

103108
// Request body.
104-
StringEntity requestEntity =
105-
new StringEntity("{\"url\":\"" + imageToAnalyze + "\"}");
109+
StringEntity requestEntity = new StringEntity("{\"url\":\"" + imageToAnalyze + "\"}");
106110
request.setEntity(requestEntity);
107111

108112
// Call the REST API method and get the response entity.
109113
HttpResponse response = httpClient.execute(request);
110114
HttpEntity entity = response.getEntity();
111115

116+
System.out.println("status" + response.getStatusLine().getStatusCode());
117+
112118
// Check for success.
113119
if (response.getStatusLine().getStatusCode() == 200) {
114120
// Display the thumbnail.
@@ -123,11 +129,12 @@ public class Main {
123129
}
124130
} catch (Exception e) {
125131
System.out.println(e.getMessage());
132+
e.printStackTrace();
126133
}
127134
}
128135

129136
// Displays the given input stream as an image.
130-
private static void displayImage(InputStream inputStream) {
137+
public static void displayImage(InputStream inputStream) {
131138
try {
132139
BufferedImage bufferedImage = ImageIO.read(inputStream);
133140

@@ -161,4 +168,4 @@ Explore a Java Swing application that uses Computer Vision to perform optical ch
161168
> [!div class="nextstepaction"]
162169
> [Computer Vision API Java Tutorial](../Tutorials/java-tutorial.md)
163170
164-
* To rapidly experiment with the Computer Vision API, try the [Open API testing console](https://westcentralus.dev.cognitive.microsoft.com/docs/services/5adf991815e1060e6355ad44/operations/56f91f2e778daf14a499e1fa/console).
171+
* To rapidly experiment with the Computer Vision API, try the [Open API testing console](https://westcentralus.dev.cognitive.microsoft.com/docs/services/5adf991815e1060e6355ad44/operations/56f91f2e778daf14a499e1fa/console).

0 commit comments

Comments
 (0)