-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecognizeInk.java
More file actions
65 lines (61 loc) · 2.75 KB
/
RecognizeInk.java
File metadata and controls
65 lines (61 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
// <imports>
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
// </imports>
public class RecognizeInk {
// <vars>
// Add your Azure Ink Recognition subscription key to your environment variables.
private static final String subscriptionKey = System.getenv("INK_RECOGNITION_SUBSCRIPTION_KEY");
// Add your Azure Ink Recognition endpoint to your environment variables.
public static final String rootUrl = System.getenv("INK_RECOGNITION_ENDPOINT");
public static final String inkRecognitionUrl = "/inkrecognizer/v1.0-preview/recognize";
// Replace the dataPath string with a path to the JSON formatted ink stroke data file.
private static final String dataPath = "PATH_TO_INK_STROKE_DATA";
// </vars>
// <main>
public static void main(String[] args) throws Exception {
String requestData = new String(Files.readAllBytes(Paths.get(dataPath)), "utf-8");
recognizeInk(requestData);
}
// </main>
// <recognizeInk>
static void recognizeInk(String requestData) {
System.out.println("Sending an Ink recognition request.");
String result = sendRequest(rootUrl, inkRecognitionUrl, subscriptionKey, requestData);
System.out.println(result);
}
// </recognizeInk>
// <sendRequest>
static String sendRequest(String endpoint, String apiAddress, String subscriptionKey, String requestData) {
try (CloseableHttpClient client = HttpClients.createDefault()) {
HttpPut request = new HttpPut(endpoint + apiAddress);
// Request headers.
request.setHeader("Content-Type", "application/json");
request.setHeader("Ocp-Apim-Subscription-Key", subscriptionKey);
request.setEntity(new StringEntity(requestData));
try (CloseableHttpResponse response = client.execute(request)) {
HttpEntity respEntity = response.getEntity();
if (respEntity != null) {
return EntityUtils.toString(respEntity, "utf-8");
}
} catch (Exception respEx) {
respEx.printStackTrace();
}
} catch (IOException ex) {
System.err.println("Exception on Anomaly Detector: " + ex.getMessage());
ex.printStackTrace();
}
return null;
}
// </sendRequest>
}