Skip to content

Commit c2f0196

Browse files
authored
Updated sample, replaced deprecated code, updated libs
1 parent 4078c1c commit c2f0196

File tree

1 file changed

+25
-31
lines changed

1 file changed

+25
-31
lines changed

java/Search/BingVisualSearchv7.java

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,11 @@
1-
package uploadimage;
2-
31
import java.util.*;
42
import java.io.*;
53

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-
214
import com.google.gson.Gson;
225
import com.google.gson.GsonBuilder;
236
import com.google.gson.JsonObject;
247
import com.google.gson.JsonParser;
258

26-
// http://hc.apache.org/downloads.cgi (HttpComponents Downloads) HttpClient 4.5.5
27-
289
import org.apache.http.HttpEntity;
2910
import org.apache.http.HttpResponse;
3011
import org.apache.http.client.methods.HttpPost;
@@ -33,18 +14,32 @@
3314
import org.apache.http.impl.client.CloseableHttpClient;
3415
import org.apache.http.impl.client.HttpClientBuilder;
3516

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 {
3636

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.
4238
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";
4442

45-
/**
46-
* @param args the command line arguments
47-
*/
4843
public static void main(String[] args) {
4944

5045
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
@@ -77,10 +72,9 @@ public static void main(String[] args) {
7772
}
7873
}
7974

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
8176
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();
8478
Gson gson = new GsonBuilder().setPrettyPrinting().create();
8579
return gson.toJson(json);
8680
}

0 commit comments

Comments
 (0)