I am working on integrating OCR functionality in our application. Currently whenever I run doOCr method, JVM crashes and entire application is stopped. I checked out and found this, but the answer is very unclear as it just tells to create bean, but for what, is unknown. Any ideas? Thank you.
COde :
public synchronized String testOcr(String fileLocation, int attachId) {
try {
File imageFile = new File(fileLocation);
BufferedImage img = ImageIO.read(imageFile);
String identifier = String.valueOf(new BigInteger(130, random).toString(32));
String blackAndWhiteImage = previewPath + identifier + ".png";
File outputfile = new File(blackAndWhiteImage);
BufferedImage bufferedImage = BitmapImageUtil.convertToGrayscale(img, new Dimension(img.getWidth(), img.getHeight()));
bufferedImage = Scalr.resize(bufferedImage,img.getWidth()*2,img.getHeight()*2);
ITesseract instance = Tesseract.getInstance();
synchronized (instance) {
// Point to one folder above tessdata directory, must contain training data
instance.setDatapath("/usr/share/tesseract-ocr/");
// ISO 693-3 standard
instance.setLanguage("deu");
String result = instance.doOCR(outputfile);
//System.out.println("Result is "+result);
result = result.replaceAll("[^a-zA-Z0-9öÖäÄüÜß@\\s]", "");
Files.delete(new File(blackAndWhiteImage).toPath());
GroupAttachments groupAttachments = this.groupAttachmentsDAO.getAttachmenById(attachId);
if (groupAttachments != null) {
saveIndexes(result, groupAttachments.getFileName(), null, groupAttachments.getGroupId(), false, attachId);
}
return result;
}
} catch (Exception ignored) {
}
return null;
}