Hello Team,
I am trying to call OCR APi from java, but getting the below response
“{OCRExitCode=99, IsErroredOnProcessing=true, ErrorMessage=[Unable to recognize the file type, Unable to detect the file extension, or the file extension is incorrect, and no ‘file type’ provided in request. Please provide a file with a proper content type or extension, or provide a file type in the request to manually set the file extension.], ProcessingTimeInMilliseconds=0}”
I tried with file and base64Image but same response in both cases.
Please find the below code which I was trying to implement.
@SuppressWarnings({ “deprecation”, “resource” })
public static String getCaptchaData(String imagePath) {
final String url = “https://api.ocr.space/parse/image”;
try {
FormBodyPart cmisactionPart_1 = FormBodyPartBuilder.create(“language”, new StringBody(“eng”, ContentType.TEXT_PLAIN)).build();
FormBodyPart cmisactionPart_2 = FormBodyPartBuilder.create(“isOverlayRequired”, new StringBody(“false”, ContentType.TEXT_PLAIN)).build();
FormBodyPart cmisactionPart_3 = FormBodyPartBuilder.create(“iscreatesearchablepdf”, new StringBody(“false”, ContentType.TEXT_PLAIN)).build();
FormBodyPart cmisactionPart_4 = FormBodyPartBuilder.create(“issearchablepdfhidetextlayer”, new StringBody(“false”, ContentType.TEXT_PLAIN)).build();
FormBodyPart cmisactionPart_5 = FormBodyPartBuilder.create(“filetype”, new StringBody(“png”, ContentType.TEXT_PLAIN)).build();
FormBodyPart cmisactionPart_6 = FormBodyPartBuilder.create(“scale”, new StringBody(“true”, ContentType.TEXT_PLAIN)).build();
//Base64 Start
FileInputStream stream = new FileInputStream(imagePath);
int bufLength = 2048;
byte[] buffer = new byte[2048];
byte[] data;
ByteArrayOutputStream out = new ByteArrayOutputStream();
int readLength;
while ((readLength = stream.read(buffer, 0, bufLength)) != -1) {
out.write(buffer, 0, readLength);
}
data = out.toByteArray();
String imageString = "data:image/png;base64,"+Base64.getEncoder().withoutPadding().encodeToString(data);
System.out.println("data :-" +imageString);
FormBodyPart cmisactionPart_7 = FormBodyPartBuilder.create("base64Image", new StringBody(imageString, ContentType.TEXT_PLAIN)).build();
//Base64 End
MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart(cmisactionPart_1);
reqEntity.addPart(cmisactionPart_2);
reqEntity.addPart(cmisactionPart_3);
reqEntity.addPart(cmisactionPart_4);
reqEntity.addPart(cmisactionPart_5);
reqEntity.addPart(cmisactionPart_6);
reqEntity.addPart(cmisactionPart_7);
FileBody image_1 = new FileBody(new File(imagePath));
ContentBody image = new FileBody(new File(imagePath));
reqEntity.addPart("file", image_1);
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(reqEntity);
httpPost.setHeader("apikey", "helloworld");
HttpClient httpClient = new DefaultHttpClient();
HttpResponse response = httpClient.execute(httpPost);
HttpEntity resEntity = response.getEntity();
System.out.println(resEntity.toString());
String resultString = EntityUtils.toString(resEntity);
EntityUtils.consume(resEntity);
httpClient.getConnectionManager().shutdown();
return resultString;
} catch (Exception e) {
e.printStackTrace();
}
return imagePath;
}
Can you please help me to fix the issue.