Java | OCR
Setup
brew install tesseractfor support of other languages
brew install tesseract-langUsing custom tessdata
Download trained tessdata for several languages and put them under folder, it could be located under resource folder or any other path outside of the application If folder is located under resource folder, use the next code
var tesseract = new Tesseract();
var tessdata = LoadLibs.extractTessResources("tessdata");
tesseract.setDatapath(tessdata.getAbsolutePath());or if you know the absolute path, just write it in the setDataPath
var tesseract = new Tesseract();
tesseract.setDataPath("/opt/ocr/tessdata");Under the folder you will have files with the next names:
deu.traineddata
eng.traineddata
osd.traineddata
the first part of the name is important, we will use for setting languages
Set language
The image that you will OCRing could contain one or multiple languages, in that case you will need to define which language is expected, by default eng is enabled
Get OCR result
The library tess4j supports multiple options to work with input content, it could be File or BufferedImage or ByteBuffer, also you could define do you want to extract the text from the whole file, or a concrete Bounding Box
Troubleshooting
Unable to load library 'tesseract'
This one was resolved by providing the path to jna libraries system property
Links
Last updated