Package net.sourceforge.tess4j
Class Tesseract1
- java.lang.Object
-
- net.sourceforge.tess4j.TessAPI1
-
- net.sourceforge.tess4j.Tesseract1
-
- All Implemented Interfaces:
com.sun.jna.Library
,ITessAPI
,ITesseract
public class Tesseract1 extends TessAPI1 implements ITesseract
An object layer on top ofTessAPI1
, provides character recognition support for common image formats, and multi-page TIFF images beyond the uncompressed, binary TIFF format supported by Tesseract OCR engine. The extended capabilities are provided by theJava Advanced Imaging Image I/O Tools
.
Support for PDF documents is available throughGhost4J
, aJNA
wrapper forGPL Ghostscript
, which should be installed and included in system path. If Ghostscript is not available, PDFBox will be used.
Any program that uses the library will need to ensure that the required libraries (the.jar
files forjna
,jai-imageio
, andghost4j
) are in its compile and run-timeclasspath
.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface net.sourceforge.tess4j.ITessAPI
ITessAPI.CANCEL_FUNC, ITessAPI.EANYCODE_CHAR, ITessAPI.ETEXT_DESC, ITessAPI.TessBaseAPI, ITessAPI.TessCancelFunc, ITessAPI.TessChoiceIterator, ITessAPI.TessMutableIterator, ITessAPI.TessOcrEngineMode, ITessAPI.TessOrientation, ITessAPI.TessPageIterator, ITessAPI.TessPageIteratorLevel, ITessAPI.TessPageSegMode, ITessAPI.TessParagraphJustification, ITessAPI.TessPolyBlockType, ITessAPI.TessProgressFunc, ITessAPI.TessResultIterator, ITessAPI.TessResultRenderer, ITessAPI.TessTextlineOrder, ITessAPI.TessWritingDirection, ITessAPI.TimeVal
-
Nested classes/interfaces inherited from interface net.sourceforge.tess4j.ITesseract
ITesseract.RenderedFormat
-
-
Field Summary
-
Fields inherited from interface net.sourceforge.tess4j.ITesseract
htmlBeginTag, htmlEndTag
-
-
Constructor Summary
Constructors Constructor Description Tesseract1()
-
Method Summary
Modifier and Type Method Description void
createDocuments(java.lang.String[] filenames, java.lang.String[] outputbases, java.util.List<ITesseract.RenderedFormat> formats)
Creates documents for given renderer.void
createDocuments(java.lang.String filename, java.lang.String outputbase, java.util.List<ITesseract.RenderedFormat> formats)
Creates documents for given renderer.java.util.List<OCRResult>
createDocumentsWithResults(java.awt.image.BufferedImage[] bis, java.lang.String[] filenames, java.lang.String[] outputbases, java.util.List<ITesseract.RenderedFormat> formats, int pageIteratorLevel)
Creates documents with OCR results for given renderers at specified page iterator level.OCRResult
createDocumentsWithResults(java.awt.image.BufferedImage bi, java.lang.String filename, java.lang.String outputbase, java.util.List<ITesseract.RenderedFormat> formats, int pageIteratorLevel)
Creates documents with OCR result for given renderers at specified page iterator level.java.util.List<OCRResult>
createDocumentsWithResults(java.lang.String[] filenames, java.lang.String[] outputbases, java.util.List<ITesseract.RenderedFormat> formats, int pageIteratorLevel)
Creates documents with OCR results for given renderers at specified page iterator level.OCRResult
createDocumentsWithResults(java.lang.String filename, java.lang.String outputbase, java.util.List<ITesseract.RenderedFormat> formats, int pageIteratorLevel)
Creates documents with OCR result for given renderers at specified page iterator level.protected void
dispose()
Releases all of the native resources used by this instance.java.lang.String
doOCR(int xsize, int ysize, java.nio.ByteBuffer buf, java.awt.Rectangle rect, int bpp)
Performs OCR operation.java.lang.String
doOCR(int xsize, int ysize, java.nio.ByteBuffer buf, java.lang.String filename, java.awt.Rectangle rect, int bpp)
Performs OCR operation.java.lang.String
doOCR(java.awt.image.BufferedImage bi)
Performs OCR operation.java.lang.String
doOCR(java.awt.image.BufferedImage bi, java.awt.Rectangle rect)
Performs OCR operation.java.lang.String
doOCR(java.io.File imageFile)
Performs OCR operation.java.lang.String
doOCR(java.io.File inputFile, java.awt.Rectangle rect)
Performs OCR operation.java.lang.String
doOCR(java.util.List<javax.imageio.IIOImage> imageList, java.awt.Rectangle rect)
Performs OCR operation.java.lang.String
doOCR(java.util.List<javax.imageio.IIOImage> imageList, java.lang.String filename, java.awt.Rectangle rect)
Performs OCR operation.protected ITessAPI.TessBaseAPI
getHandle()
Returns API handle.protected java.lang.String
getOCRText(java.lang.String filename, int pageNum)
Gets recognized text.java.util.List<java.awt.Rectangle>
getSegmentedRegions(java.awt.image.BufferedImage bi, int pageIteratorLevel)
Gets segmented regions at specified page iterator level.java.util.List<Word>
getWords(java.awt.image.BufferedImage bi, int pageIteratorLevel)
Gets recognized words at specified page iterator level.protected void
init()
Initializes Tesseract engine.void
setConfigs(java.util.List<java.lang.String> configs)
Sets configs to be passed to Tesseract'sInit
method.void
setDatapath(java.lang.String datapath)
Sets path totessdata
.void
setHocr(boolean hocr)
Enables hocr output.protected void
setImage(int xsize, int ysize, java.nio.ByteBuffer buf, java.awt.Rectangle rect, int bpp)
Sets image to be processed.protected void
setImage(java.awt.image.RenderedImage image, java.awt.Rectangle rect)
A wrapper forsetImage(int, int, ByteBuffer, Rectangle, int)
.void
setLanguage(java.lang.String language)
Sets language for OCR.void
setOcrEngineMode(int ocrEngineMode)
Sets OCR engine mode.void
setPageSegMode(int mode)
Sets page segmentation mode.void
setTessVariable(java.lang.String key, java.lang.String value)
Set the value of Tesseract's internal parameter.protected void
setTessVariables()
Sets Tesseract's internal parameters.-
Methods inherited from class net.sourceforge.tess4j.TessAPI1
TessAltoRendererCreate, TessBaseAPIAdaptToWordStr, TessBaseAPIAllWordConfidences, TessBaseAPIAnalyseLayout, TessBaseAPIClear, TessBaseAPIClearAdaptiveClassifier, TessBaseAPIClearPersistentCache, TessBaseAPICreate, TessBaseAPIDelete, TessBaseAPIDetectOrientationScript, TessBaseAPIEnd, TessBaseAPIGetAltoText, TessBaseAPIGetAvailableLanguagesAsVector, TessBaseAPIGetBoolVariable, TessBaseAPIGetBoxText, TessBaseAPIGetComponentImages, TessBaseAPIGetComponentImages1, TessBaseAPIGetConnectedComponents, TessBaseAPIGetDatapath, TessBaseAPIGetDoubleVariable, TessBaseAPIGetHOCRText, TessBaseAPIGetInitLanguagesAsString, TessBaseAPIGetInputImage, TessBaseAPIGetInputName, TessBaseAPIGetIntVariable, TessBaseAPIGetIterator, TessBaseAPIGetLoadedLanguagesAsVector, TessBaseAPIGetLSTMBoxText, TessBaseAPIGetMutableIterator, TessBaseAPIGetPageSegMode, TessBaseAPIGetRegions, TessBaseAPIGetSourceYResolution, TessBaseAPIGetStringVariable, TessBaseAPIGetStrips, TessBaseAPIGetTextDirection, TessBaseAPIGetTextlines, TessBaseAPIGetTextlines1, TessBaseAPIGetThresholdedImage, TessBaseAPIGetThresholdedImageScaleFactor, TessBaseAPIGetTsvText, TessBaseAPIGetUnichar, TessBaseAPIGetUNLVText, TessBaseAPIGetUTF8Text, TessBaseAPIGetWords, TessBaseAPIGetWordStrBoxText, TessBaseAPIInit1, TessBaseAPIInit2, TessBaseAPIInit3, TessBaseAPIInit4, TessBaseAPIInitForAnalysePage, TessBaseAPIInitLangMod, TessBaseAPIIsValidWord, TessBaseAPIMeanTextConf, TessBaseAPIPrintVariablesToFile, TessBaseAPIProcessPage, TessBaseAPIProcessPages, TessBaseAPIReadConfigFile, TessBaseAPIRecognize, TessBaseAPIRecognizeForChopTest, TessBaseAPIRect, TessBaseAPISetImage, TessBaseAPISetImage2, TessBaseAPISetInputImage, TessBaseAPISetInputName, TessBaseAPISetOutputName, TessBaseAPISetPageSegMode, TessBaseAPISetRectangle, TessBaseAPISetSourceResolution, TessBaseAPISetVariable, TessBoxTextRendererCreate, TessChoiceIteratorConfidence, TessChoiceIteratorDelete, TessChoiceIteratorGetUTF8Text, TessChoiceIteratorNext, TessDeleteIntArray, TessDeleteResultRenderer, TessDeleteText, TessDeleteTextArray, TessHOcrRendererCreate, TessHOcrRendererCreate2, TessLSTMBoxRendererCreate, TessMonitorCreate, TessMonitorDelete, TessMonitorGetCancelThis, TessMonitorGetProgress, TessMonitorSetCancelFunc, TessMonitorSetCancelThis, TessMonitorSetDeadlineMSecs, TessMonitorSetProgressFunc, TessPageIteratorBaseline, TessPageIteratorBegin, TessPageIteratorBlockType, TessPageIteratorBoundingBox, TessPageIteratorCopy, TessPageIteratorDelete, TessPageIteratorGetBinaryImage, TessPageIteratorGetImage, TessPageIteratorIsAtBeginningOf, TessPageIteratorIsAtFinalElement, TessPageIteratorNext, TessPageIteratorOrientation, TessPageIteratorParagraphInfo, TessPDFRendererCreate, TessResultIteratorConfidence, TessResultIteratorCopy, TessResultIteratorDelete, TessResultIteratorGetChoiceIterator, TessResultIteratorGetPageIterator, TessResultIteratorGetPageIteratorConst, TessResultIteratorGetUTF8Text, TessResultIteratorNext, TessResultIteratorSymbolIsDropcap, TessResultIteratorSymbolIsSubscript, TessResultIteratorSymbolIsSuperscript, TessResultIteratorWordFontAttributes, TessResultIteratorWordIsFromDictionary, TessResultIteratorWordIsNumeric, TessResultIteratorWordRecognitionLanguage, TessResultRendererAddImage, TessResultRendererBeginDocument, TessResultRendererEndDocument, TessResultRendererExtention, TessResultRendererImageNum, TessResultRendererInsert, TessResultRendererNext, TessResultRendererTitle, TessTextRendererCreate, TessTsvRendererCreate, TessUnlvRendererCreate, TessVersion, TessWordStrBoxRendererCreate
-
-
-
-
Method Detail
-
getHandle
protected ITessAPI.TessBaseAPI getHandle()
Returns API handle.- Returns:
- handle
-
setDatapath
public void setDatapath(java.lang.String datapath)
Sets path totessdata
.- Specified by:
setDatapath
in interfaceITesseract
- Parameters:
datapath
- the tessdata path to set
-
setLanguage
public void setLanguage(java.lang.String language)
Sets language for OCR.- Specified by:
setLanguage
in interfaceITesseract
- Parameters:
language
- the language code, which follows ISO 639-3 standard.
-
setOcrEngineMode
public void setOcrEngineMode(int ocrEngineMode)
Sets OCR engine mode.- Specified by:
setOcrEngineMode
in interfaceITesseract
- Parameters:
ocrEngineMode
- the OcrEngineMode to set
-
setPageSegMode
public void setPageSegMode(int mode)
Sets page segmentation mode.- Specified by:
setPageSegMode
in interfaceITesseract
- Parameters:
mode
- the page segmentation mode to set
-
setHocr
public void setHocr(boolean hocr)
Enables hocr output.- Parameters:
hocr
- to enable or disable hocr output
-
setTessVariable
public void setTessVariable(java.lang.String key, java.lang.String value)
Set the value of Tesseract's internal parameter.- Specified by:
setTessVariable
in interfaceITesseract
- Parameters:
key
- variable name, e.g.,tessedit_create_hocr
,tessedit_char_whitelist
, etc.value
- value for corresponding variable, e.g., "1", "0", "0123456789", etc.
-
setConfigs
public void setConfigs(java.util.List<java.lang.String> configs)
Sets configs to be passed to Tesseract'sInit
method.- Specified by:
setConfigs
in interfaceITesseract
- Parameters:
configs
- list of config filenames, e.g., "digits", "bazaar", "quiet"
-
doOCR
public java.lang.String doOCR(java.io.File imageFile) throws TesseractException
Performs OCR operation.- Specified by:
doOCR
in interfaceITesseract
- Parameters:
imageFile
- an image file- Returns:
- the recognized text
- Throws:
TesseractException
-
doOCR
public java.lang.String doOCR(java.io.File inputFile, java.awt.Rectangle rect) throws TesseractException
Performs OCR operation.- Specified by:
doOCR
in interfaceITesseract
- Parameters:
inputFile
- an image filerect
- the bounding rectangle defines the region of the image to be recognized. A rectangle of zero dimension ornull
indicates the whole image.- Returns:
- the recognized text
- Throws:
TesseractException
-
doOCR
public java.lang.String doOCR(java.awt.image.BufferedImage bi) throws TesseractException
Performs OCR operation.- Specified by:
doOCR
in interfaceITesseract
- Parameters:
bi
- a buffered image- Returns:
- the recognized text
- Throws:
TesseractException
-
doOCR
public java.lang.String doOCR(java.awt.image.BufferedImage bi, java.awt.Rectangle rect) throws TesseractException
Performs OCR operation.- Specified by:
doOCR
in interfaceITesseract
- Parameters:
bi
- a buffered imagerect
- the bounding rectangle defines the region of the image to be recognized. A rectangle of zero dimension ornull
indicates the whole image.- Returns:
- the recognized text
- Throws:
TesseractException
-
doOCR
public java.lang.String doOCR(java.util.List<javax.imageio.IIOImage> imageList, java.awt.Rectangle rect) throws TesseractException
Performs OCR operation.- Specified by:
doOCR
in interfaceITesseract
- Parameters:
imageList
- a list ofIIOImage
objectsrect
- the bounding rectangle defines the region of the image to be recognized. A rectangle of zero dimension ornull
indicates the whole image.- Returns:
- the recognized text
- Throws:
TesseractException
-
doOCR
public java.lang.String doOCR(java.util.List<javax.imageio.IIOImage> imageList, java.lang.String filename, java.awt.Rectangle rect) throws TesseractException
Performs OCR operation.- Specified by:
doOCR
in interfaceITesseract
- Parameters:
imageList
- a list ofIIOImage
objectsfilename
- input file namerect
- the bounding rectangle defines the region of the image to be recognized. A rectangle of zero dimension ornull
indicates the whole image.- Returns:
- the recognized text
- Throws:
TesseractException
-
doOCR
public java.lang.String doOCR(int xsize, int ysize, java.nio.ByteBuffer buf, java.awt.Rectangle rect, int bpp) throws TesseractException
Performs OCR operation. UseSetImage
, (optionally)SetRectangle
, and one or more of theGet*Text
functions.- Specified by:
doOCR
in interfaceITesseract
- Parameters:
xsize
- width of imageysize
- height of imagebuf
- pixel datarect
- the bounding rectangle defines the region of the image to be recognized. A rectangle of zero dimension ornull
indicates the whole image.bpp
- bits per pixel, represents the bit depth of the image, with 1 for binary bitmap, 8 for gray, and 24 for color RGB.- Returns:
- the recognized text
- Throws:
TesseractException
-
doOCR
public java.lang.String doOCR(int xsize, int ysize, java.nio.ByteBuffer buf, java.lang.String filename, java.awt.Rectangle rect, int bpp) throws TesseractException
Performs OCR operation. UseSetImage
, (optionally)SetRectangle
, and one or more of theGet*Text
functions.- Specified by:
doOCR
in interfaceITesseract
- Parameters:
xsize
- width of imageysize
- height of imagebuf
- pixel datafilename
- input file name. Needed only for training and reading a UNLV zone file.rect
- the bounding rectangle defines the region of the image to be recognized. A rectangle of zero dimension ornull
indicates the whole image.bpp
- bits per pixel, represents the bit depth of the image, with 1 for binary bitmap, 8 for gray, and 24 for color RGB.- Returns:
- the recognized text
- Throws:
TesseractException
-
init
protected void init()
Initializes Tesseract engine.
-
setTessVariables
protected void setTessVariables()
Sets Tesseract's internal parameters.
-
setImage
protected void setImage(java.awt.image.RenderedImage image, java.awt.Rectangle rect) throws java.io.IOException
A wrapper forsetImage(int, int, ByteBuffer, Rectangle, int)
.- Parameters:
image
- a rendered imagerect
- region of interest- Throws:
java.io.IOException
-
setImage
protected void setImage(int xsize, int ysize, java.nio.ByteBuffer buf, java.awt.Rectangle rect, int bpp)
Sets image to be processed.- Parameters:
xsize
- width of imageysize
- height of imagebuf
- pixel datarect
- the bounding rectangle defines the region of the image to be recognized. A rectangle of zero dimension ornull
indicates the whole image.bpp
- bits per pixel, represents the bit depth of the image, with 1 for binary bitmap, 8 for gray, and 24 for color RGB.
-
getOCRText
protected java.lang.String getOCRText(java.lang.String filename, int pageNum)
Gets recognized text.- Parameters:
filename
- input file name. Needed only for reading a UNLV zone file.pageNum
- page number; needed for hocr paging.- Returns:
- the recognized text
-
createDocuments
public void createDocuments(java.lang.String filename, java.lang.String outputbase, java.util.List<ITesseract.RenderedFormat> formats) throws TesseractException
Creates documents for given renderer.- Specified by:
createDocuments
in interfaceITesseract
- Parameters:
filename
- input imageoutputbase
- output filename without extensionformats
- types of renderer- Throws:
TesseractException
-
createDocuments
public void createDocuments(java.lang.String[] filenames, java.lang.String[] outputbases, java.util.List<ITesseract.RenderedFormat> formats) throws TesseractException
Creates documents for given renderer.- Specified by:
createDocuments
in interfaceITesseract
- Parameters:
filenames
- array of input filesoutputbases
- array of output filenames without extensionformats
- types of renderer- Throws:
TesseractException
-
getSegmentedRegions
public java.util.List<java.awt.Rectangle> getSegmentedRegions(java.awt.image.BufferedImage bi, int pageIteratorLevel) throws TesseractException
Gets segmented regions at specified page iterator level.- Specified by:
getSegmentedRegions
in interfaceITesseract
- Parameters:
bi
- input buffered imagepageIteratorLevel
- TessPageIteratorLevel enum- Returns:
- list of
Rectangle
- Throws:
TesseractException
-
getWords
public java.util.List<Word> getWords(java.awt.image.BufferedImage bi, int pageIteratorLevel)
Gets recognized words at specified page iterator level.- Specified by:
getWords
in interfaceITesseract
- Parameters:
bi
- input buffered imagepageIteratorLevel
- TessPageIteratorLevel enum- Returns:
- list of
Word
-
createDocumentsWithResults
public OCRResult createDocumentsWithResults(java.awt.image.BufferedImage bi, java.lang.String filename, java.lang.String outputbase, java.util.List<ITesseract.RenderedFormat> formats, int pageIteratorLevel) throws TesseractException
Creates documents with OCR result for given renderers at specified page iterator level.- Specified by:
createDocumentsWithResults
in interfaceITesseract
- Parameters:
bi
- input buffered imagefilename
- filename (optional)outputbase
- output filenames without extensionformats
- types of rendererpageIteratorLevel
- TessPageIteratorLevel enum- Returns:
- OCR result
- Throws:
TesseractException
-
createDocumentsWithResults
public java.util.List<OCRResult> createDocumentsWithResults(java.awt.image.BufferedImage[] bis, java.lang.String[] filenames, java.lang.String[] outputbases, java.util.List<ITesseract.RenderedFormat> formats, int pageIteratorLevel) throws TesseractException
Creates documents with OCR results for given renderers at specified page iterator level.- Specified by:
createDocumentsWithResults
in interfaceITesseract
- Parameters:
bis
- array of input buffered imagesfilenames
- array of filenamesoutputbases
- array of output filenames without extensionformats
- types of rendererpageIteratorLevel
- TessPageIteratorLevel enum- Returns:
- list of OCR results
- Throws:
TesseractException
-
createDocumentsWithResults
public OCRResult createDocumentsWithResults(java.lang.String filename, java.lang.String outputbase, java.util.List<ITesseract.RenderedFormat> formats, int pageIteratorLevel) throws TesseractException
Creates documents with OCR result for given renderers at specified page iterator level.- Specified by:
createDocumentsWithResults
in interfaceITesseract
- Parameters:
filename
- input fileoutputbase
- output filenames without extensionformats
- types of rendererpageIteratorLevel
- TessPageIteratorLevel enum- Returns:
- OCR result
- Throws:
TesseractException
-
createDocumentsWithResults
public java.util.List<OCRResult> createDocumentsWithResults(java.lang.String[] filenames, java.lang.String[] outputbases, java.util.List<ITesseract.RenderedFormat> formats, int pageIteratorLevel) throws TesseractException
Creates documents with OCR results for given renderers at specified page iterator level.- Specified by:
createDocumentsWithResults
in interfaceITesseract
- Parameters:
filenames
- array of input filesoutputbases
- array of output filenames without extensionformats
- types of rendererpageIteratorLevel
- TessPageIteratorLevel enum- Returns:
- list of OCR results
- Throws:
TesseractException
-
dispose
protected void dispose()
Releases all of the native resources used by this instance.
-
-