Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Aspose.OCR-Cloud-SDK-Java
ThIs GIST contains SDK Examples of Aspose.OCR Cloud SDK for Java.
private static OcrApi api;
public static void main(String args[]) throws IOException {
try {
setUpConfig();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String text;
text = recognizeByContent();
System.out.println(text);
}
private static String recognizeByContent() {
try {
File f = new File(Configuration.getTestSrcDir(), "0.png");
if (!f.exists()) {
return "Error: recognizeByContentLang: file not found";
}
api = new ApiClient().createService(OcrApi.class);
RequestBody requestBody = RequestBody.create(f,MediaType.parse("application/octet-stream"));
Call<ResponseBody> call = api.RecognizeFromContent(requestBody);
Response<ResponseBody> res = call.execute();
ResponseBody answer = res.body();
com.aspose.ocr.OCRResponse ocrResponse = com.aspose.ocr.OCRResponse.Deserialize(answer);
String text = ocrResponse.text;
return text;
} catch (IOException e) {
e.printStackTrace();
return "";
}
}
private static void setUpConfig() throws Exception {
Configuration.setAPP_SID("xxxxx");
Configuration.setAPI_KEY("xxxxx");
Configuration.setAuthPath("https://api.aspose.cloud/connect/token");
Configuration.setBasePath("https://api.aspose.cloud/v3.0");
Configuration.setUserAgent("WebKit");
Configuration.setTestSrcDir("sourceTest");
Configuration.setTestDstDir("destTest");
if (Configuration.getAPI_KEY().isEmpty() || Configuration.getAPP_SID().isEmpty()) {
System.out.println("! Error: Setup AppSID & AppKey in BaseTest Configuration");
throw new Exception("Setup AppSID & AppKey in BaseTest Configuration");
}
}
private static OcrApi api;
private static final String url = "https://upload.wikimedia.org/wikipedia/commons/2/2f/Book_of_Abraham_FirstPage.png";
public static void main(String args[]) throws IOException {
try {
setUpConfig();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String text;
text = recognizeByURL();
System.out.println(text);
}
// method to recognize text from Image hosted on URL
private static String recognizeByURL() {
try {
api = new ApiClient().createService(OcrApi.class);
Call<ResponseBody> call = api.RecognizeFromUrl(url);
Response<ResponseBody> res = call.execute();
ResponseBody answer = res.body();
com.aspose.ocr.OCRResponse ocrResponse = com.aspose.ocr.OCRResponse.Deserialize(answer);
String text = ocrResponse.text;
return text;
} catch (IOException e) {
e.printStackTrace();
return "";
}
}
private static void setUpConfig() throws Exception {
Configuration.setAPP_SID("xxxxx");
Configuration.setAPI_KEY("xxxxx");
Configuration.setAuthPath("https://api.aspose.cloud/connect/token");
Configuration.setBasePath("https://api.aspose.cloud/v3.0");
Configuration.setUserAgent("WebKit");
Configuration.setTestSrcDir("sourceTest");
Configuration.setTestDstDir("destTest");
if (Configuration.getAPI_KEY().isEmpty() || Configuration.getAPP_SID().isEmpty()) {
System.out.println("! Error: Setup AppSID & AppKey in BaseTest Configuration");
throw new Exception("Setup AppSID & AppKey in BaseTest Configuration");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.