I am working on Tesseract and I have OCR functionality working already. I wanted to optimize the image so that OCR results will be better. Currently I am only making the image monochrome and scaling it to double its size. Even after that I am having issues with smaller fonts.
I tried looking up, and here is one of the top answers I can find. Unfortunately, it works with Bitmap and I cannot find any native class in Java which works with Bitmap. There is also an answer with Java code, but it again uses Bitmap and doesn't specify from which package they get it.
Where does BitmapImageUtil.convertToGrayscale()
come from?
Code :
private 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);
ImageIO.write(bufferedImage,"png",outputfile);
ITesseract instance = Tesseract.getInstance();
// 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);
// result processing with regex.
}