URDU SPELL CHECKING API REFERENCE
This section presents code snippets for using CLE Urdu OCR API with JAVA.
1.The do UrduOCR method of CLE URDUOCR web service takes four arguments (encoded image, font size, speed, and access token) in JSON format.
String accessToken = "<<your access token>>";
// IMAGE_TYPE is JPEG,JPG...etc
String encoded_input = encodeToString(PLACE_YOUR_BUFFERED_IMAGE,PLACE_YOUR_IMAGE_TYPE);
//Font size must be an integer even number in range between [14 to 44]
//If font size is unknown, please set it to -1
int fontsize= PLACE_YOUR_FONTSIZE;
//Speed must be an integer number "0" or "1".
// 1 is for FAST Speed
// 0 is for Accurate Speed
int speed=PLACE_YOUR_OCRSPEED_TYPE;
//The Resolution of image in DPI
//dpi value must be an integer number which can be either "300" or "150"
int dpi_val=PLACE_YOUR_DPI_VALUE;
String JSON_MSG = "{\"token\" : \"" + accessToken+ "\", \"fontsize\" : " + fontsize + ", \"speed\" : " + speed + ", \"dpi_val\" : " + dpi_val + ",\"image\" : \"" + encoded_input + "\" }";
2. Use JAVA HTTP client for connecting to the web service as shown below. The Apache HttpCore and Apache HttpClient libraries are required which can be downloaded from the Link.
String URL = "api.cle.org.pk";
String postURL = "https://" + URL + "/v1/ocr";
HttpClient httpClient = HttpClientBuilder.create().build();
HttpPost post = new HttpPost(postURL);
StringEntity postingString = new StringEntity(JSON_MSG,"UTF-8");
post.setEntity(postingString);
post.setHeader("Content-type", "application/json;odata=verbose");
HttpResponse response = httpClient.execute(post);
String JSON_Response=convertStreamToString(response.getEntity().getContent());
3. This method take BufferedImage and Image Type as input and return the base64 encoded string.
private static String encodeToString(BufferedImage image, String type) {
String imageString = null;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
ImageIO.write(image, type, bos);
byte[] imageBytes = bos.toByteArray();
imageString = Base64.encodeBase64String(imageBytes);
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
return imageString;
}
4. The method will return a HTTP response message. The JSON message can be extracted from the HTTP response using the following function. The JSON message contains status and tagged_text which can be processed.
private static String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}