Skip to content

Commit

Permalink
Merge pull request #102 from aahill/master
Browse files Browse the repository at this point in the history
Adding code tags, and fixing Post request
  • Loading branch information
aahill authored Oct 2, 2019
2 parents 2476e75 + f6a855a commit 6da235c
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions java/InkRecognition/quickstart/RecognizeInk.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
// 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.HttpPost;
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");

Expand All @@ -22,23 +23,26 @@ public class RecognizeInk {
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()) {
HttpPost request = new HttpPost(endpoint + apiAddress);
HttpPut request = new HttpPut(endpoint + apiAddress);
// Request headers.
request.setHeader("Content-Type", "application/json");
request.setHeader("Ocp-Apim-Subscription-Key", subscriptionKey);
Expand All @@ -57,4 +61,5 @@ static String sendRequest(String endpoint, String apiAddress, String subscriptio
}
return null;
}
// </sendRequest>
}

0 comments on commit 6da235c

Please sign in to comment.