|
1 | | -package uploadimage; |
2 | | - |
3 | 1 | import java.util.*; |
4 | 2 | import java.io.*; |
5 | 3 |
|
6 | | -/* |
7 | | - * Gson: https://github.com/google/gson |
8 | | - * Maven info: |
9 | | - * groupId: com.google.code.gson |
10 | | - * artifactId: gson |
11 | | - * version: 2.8.2 |
12 | | - * |
13 | | - * Once you have compiled or downloaded gson-2.8.2.jar, assuming you have placed it in the |
14 | | - * same folder as this file (BingImageSearch.java), you can compile and run this program at |
15 | | - * the command line as follows. |
16 | | - * |
17 | | - * javac BingImageSearch.java -classpath .;gson-2.8.2.jar -encoding UTF-8 |
18 | | - * java -cp .;gson-2.8.2.jar BingImageSearch |
19 | | - */ |
20 | | - |
21 | 4 | import com.google.gson.Gson; |
22 | 5 | import com.google.gson.GsonBuilder; |
23 | 6 | import com.google.gson.JsonObject; |
24 | 7 | import com.google.gson.JsonParser; |
25 | 8 |
|
26 | | -// http://hc.apache.org/downloads.cgi (HttpComponents Downloads) HttpClient 4.5.5 |
27 | | - |
28 | 9 | import org.apache.http.HttpEntity; |
29 | 10 | import org.apache.http.HttpResponse; |
30 | 11 | import org.apache.http.client.methods.HttpPost; |
|
33 | 14 | import org.apache.http.impl.client.CloseableHttpClient; |
34 | 15 | import org.apache.http.impl.client.HttpClientBuilder; |
35 | 16 |
|
| 17 | +/** |
| 18 | + * Add the Gson, HttpComponents, and HttpClient Mime jars to a lib folder: |
| 19 | + * Gson: https://github.com/google/gson |
| 20 | + * HttpComponents (HttpClient and HttpCore): http://hc.apache.org/downloads.cgi |
| 21 | + * HttpClient Mime: https://mvnrepository.com/artifact/org.apache.httpcomponents/httpmime |
| 22 | + * Commons Logging: https://mvnrepository.com/artifact/commons-logging/commons-logging |
| 23 | + * |
| 24 | + * Add your Bing Search V7 key and endpoint to your environment variables. |
| 25 | + * |
| 26 | + * Maven info: |
| 27 | + * groupId: com.google.code.gson |
| 28 | + * artifactId: gson |
| 29 | + * version: x.x.x |
| 30 | + * |
| 31 | + * Compile and run from the command line: |
| 32 | + * javac BingVisualSearch.java -cp .;lib\* -encoding UTF-8 |
| 33 | + * java -cp .;lib\* BingVisualSearch |
| 34 | + */ |
| 35 | +public class BingVisualSearch { |
36 | 36 |
|
37 | | -public class UploadImage2 { |
38 | | - |
39 | | - // Add your Bing Search V7 endpoint to your environment variables. |
40 | | - static String endpoint = System.getenv("BING_SEARCH_V7_ENDPOINT") + "/bing/v7.0/images/visualsearch"; |
41 | | - // Add your Bing Search V7 subscription key to your environment variables. |
| 37 | + // Add your Bing Search V7 key endpoint to your environment variables. |
42 | 38 | static String subscriptionKey = System.getenv("BING_SEARCH_V7_SUBSCRIPTION_KEY"); |
43 | | - static String imagePath = "<pathtoyourimagetouploadgoeshere>"; |
| 39 | + static String endpoint = System.getenv("BING_SEARCH_V7_ENDPOINT") + "/bing/v7.0/images/visualsearch"; |
| 40 | + // Use your own URL image if desired. |
| 41 | + static String imagePath = "https://raw.githubusercontent.com/Azure-Samples/cognitive-services-sample-data-files/master/ComputerVision/Images/objects.jpg"; |
44 | 42 |
|
45 | | - /** |
46 | | - * @param args the command line arguments |
47 | | - */ |
48 | 43 | public static void main(String[] args) { |
49 | 44 |
|
50 | 45 | CloseableHttpClient httpClient = HttpClientBuilder.create().build(); |
@@ -77,10 +72,9 @@ public static void main(String[] args) { |
77 | 72 | } |
78 | 73 | } |
79 | 74 |
|
80 | | - // pretty-printer for JSON; uses GSON parser to parse and re-serialize |
| 75 | + // Pretty-printer for JSON; uses GSON parser to parse and re-serialize |
81 | 76 | public static String prettify(String json_text) { |
82 | | - JsonParser parser = new JsonParser(); |
83 | | - JsonObject json = parser.parse(json_text).getAsJsonObject(); |
| 77 | + JsonObject json = JsonParser.parseString(json_text).getAsJsonObject(); |
84 | 78 | Gson gson = new GsonBuilder().setPrettyPrinting().create(); |
85 | 79 | return gson.toJson(json); |
86 | 80 | } |
|
0 commit comments