Search in sources :

Example 1 with TessResultIterator

use of net.sourceforge.tess4j.ITessAPI.TessResultIterator in project tess4j by nguyenq.

the class Tesseract method getWords.

/**
 * Gets recognized words at specified page iterator level.
 *
 * @param bi input image
 * @param pageIteratorLevel TessPageIteratorLevel enum
 * @return list of <code>Word</code>
 */
@Override
public List<Word> getWords(BufferedImage bi, int pageIteratorLevel) {
    this.init();
    this.setTessVariables();
    List<Word> words = new ArrayList<Word>();
    try {
        setImage(bi, null);
        api.TessBaseAPIRecognize(handle, null);
        TessResultIterator ri = api.TessBaseAPIGetIterator(handle);
        TessPageIterator pi = api.TessResultIteratorGetPageIterator(ri);
        api.TessPageIteratorBegin(pi);
        do {
            Pointer ptr = api.TessResultIteratorGetUTF8Text(ri, pageIteratorLevel);
            String text = ptr.getString(0);
            api.TessDeleteText(ptr);
            float confidence = api.TessResultIteratorConfidence(ri, pageIteratorLevel);
            IntBuffer leftB = IntBuffer.allocate(1);
            IntBuffer topB = IntBuffer.allocate(1);
            IntBuffer rightB = IntBuffer.allocate(1);
            IntBuffer bottomB = IntBuffer.allocate(1);
            api.TessPageIteratorBoundingBox(pi, pageIteratorLevel, leftB, topB, rightB, bottomB);
            int left = leftB.get();
            int top = topB.get();
            int right = rightB.get();
            int bottom = bottomB.get();
            Word word = new Word(text, confidence, new Rectangle(left, top, right - left, bottom - top));
            words.add(word);
        } while (api.TessPageIteratorNext(pi, pageIteratorLevel) == TRUE);
        return words;
    } catch (Exception e) {
        return words;
    } finally {
        dispose();
    }
}
Also used : TessResultIterator(net.sourceforge.tess4j.ITessAPI.TessResultIterator) TessPageIterator(net.sourceforge.tess4j.ITessAPI.TessPageIterator) IntBuffer(java.nio.IntBuffer) Rectangle(java.awt.Rectangle) Pointer(com.sun.jna.Pointer)

Aggregations

Pointer (com.sun.jna.Pointer)1 Rectangle (java.awt.Rectangle)1 IntBuffer (java.nio.IntBuffer)1 TessPageIterator (net.sourceforge.tess4j.ITessAPI.TessPageIterator)1 TessResultIterator (net.sourceforge.tess4j.ITessAPI.TessResultIterator)1