@@ -35,21 +35,20 @@ public RecognitionState(tesseract.TessBaseAPI apiHandle, tesseract.ResultIterato
3535 * @return requested box
3636 */
3737 public Box getBoundingBox (int level ) {
38- // pointers to the bounding box coordinates
39- final IntPointer left = new IntPointer (1 );
40- final IntPointer top = new IntPointer (1 );
41- final IntPointer right = new IntPointer (1 );
42- final IntPointer bottom = new IntPointer (1 );
38+ try (final IntPointer left = new IntPointer (1 );
39+ final IntPointer top = new IntPointer (1 );
40+ final IntPointer right = new IntPointer (1 );
41+ final IntPointer bottom = new IntPointer (1 )) {
4342
44- // get bounding box
45- tesseract .TessPageIteratorBoundingBox (pageIt , level , left , top , right , bottom );
43+ tesseract .TessPageIteratorBoundingBox (pageIt , level , left , top , right , bottom );
4644
47- final int x = left .get ();
48- final int y = top .get ();
49- final int width = right .get () - x ;
50- final int height = bottom .get () - y ;
45+ final int x = left .get ();
46+ final int y = top .get ();
47+ final int width = right .get () - x ;
48+ final int height = bottom .get () - y ;
5149
52- return new Box (x , y , width , height );
50+ return new Box (x , y , width , height );
51+ }
5352 }
5453
5554 /**
@@ -72,22 +71,22 @@ public String getText(int level) {
7271 * @return baseline
7372 */
7473 public Baseline getBaseline (int level ) {
75- // pointers to the baseline coordinates
76- final IntPointer x1 = new IntPointer (1 );
77- final IntPointer y1 = new IntPointer (1 );
78- final IntPointer x2 = new IntPointer (1 );
79- final IntPointer y2 = new IntPointer (1 );
74+ try (final IntPointer x1 = new IntPointer (1 );
75+ final IntPointer y1 = new IntPointer (1 );
76+ final IntPointer x2 = new IntPointer (1 );
77+ final IntPointer y2 = new IntPointer (1 )) {
8078
81- tesseract .TessPageIteratorBaseline (pageIt , level , x1 , y1 , x2 , y2 );
79+ tesseract .TessPageIteratorBaseline (pageIt , level , x1 , y1 , x2 , y2 );
8280
83- final int width = x2 .get () - x1 .get ();
84- final float height = y2 .get () - y1 .get ();
85- final float slope = height / width ;
81+ final int width = x2 .get () - x1 .get ();
82+ final float height = y2 .get () - y1 .get ();
83+ final float slope = height / width ;
8684
87- final Box bbox = getBoundingBox (tesseract .RIL_WORD );
88- final int yOffset = bbox .getY () + bbox .getHeight () - y1 .get ();
85+ final Box bbox = getBoundingBox (tesseract .RIL_WORD );
86+ final int yOffset = bbox .getY () + bbox .getHeight () - y1 .get ();
8987
90- return new Baseline (yOffset , slope );
88+ return new Baseline (yOffset , slope );
89+ }
9190 }
9291
9392 /**
@@ -117,51 +116,54 @@ public List<AlternativeChoice> getAlternatives() {
117116 }
118117
119118 private void getSymbolFeatures (lept .PIX image ) {
120- final IntPointer left = new IntPointer (1 );
121- final IntPointer top = new IntPointer (1 );
122119
123- final lept .PIX pix = tesseract .TessPageIteratorGetImage (pageIt , tesseract .RIL_SYMBOL , 1 , image , left , top );
124- final tesseract .TBLOB blob = tesseract .TessMakeTBLOB (pix );
120+ try (final IntPointer left = new IntPointer (1 );
121+ final IntPointer top = new IntPointer (1 );
122+ final IntPointer numFeatures = new IntPointer (1 );
123+ final IntPointer featOutlineIndex = new IntPointer (1 );
124+ final tesseract .INT_FEATURE_STRUCT intFeatures = new tesseract .INT_FEATURE_STRUCT (
125+ new BytePointer (4 * 512 ))) {
125126
126- final IntPointer numFeatures = new IntPointer ( 1 );
127- final IntPointer featOutlineIndex = new IntPointer ( 1 );
127+ final lept . PIX pix = tesseract . TessPageIteratorGetImage ( pageIt , tesseract . RIL_SYMBOL , 1 , image , left , top );
128+ final tesseract . TBLOB blob = tesseract . TessMakeTBLOB ( pix );
128129
129- final tesseract .INT_FEATURE_STRUCT intFeatures = new tesseract .INT_FEATURE_STRUCT ().capacity (512 );
130-
131- tesseract .TessBaseAPIGetFeaturesForBlob (apiHandle , blob , intFeatures , numFeatures , featOutlineIndex );
130+ tesseract .TessBaseAPIGetFeaturesForBlob (apiHandle , blob , intFeatures , numFeatures , featOutlineIndex );
131+ }
132132 }
133133
134134 /**
135135 * @return font attributes for the current word.
136136 */
137137 public FontAttributes getWordFontAttributes () {
138- // pointers to integers for every attribute
139- final BoolPointer isBold = new BoolPointer (1 );
140- final BoolPointer isItalic = new BoolPointer (1 );
141- final BoolPointer isUnderlined = new BoolPointer (1 );
142- final BoolPointer isMonospace = new BoolPointer (1 );
143- final BoolPointer isSerif = new BoolPointer (1 );
144- final BoolPointer isSmallCaps = new BoolPointer (1 );
145- final IntPointer fontSize = new IntPointer (1 );
146- final IntPointer fontID = new IntPointer (1 );
147-
148- // set values
149- tesseract .TessResultIteratorWordFontAttributes (resultIt , isBold , isItalic , isUnderlined , isMonospace , isSerif ,
150- isSmallCaps , fontSize , fontID );
151-
152- // build and return FontAttributes
153- final FontAttributes fontAttributes = new FontAttributes .Builder ()
154- .bold (isBold .get ())
155- .italic (isItalic .get ())
156- .underlined (isUnderlined .get ())
157- .monospace (isMonospace .get ())
158- .serif (isSerif .get ())
159- .smallcaps (isSmallCaps .get ())
160- .size (fontSize .get ())
161- .fontID (fontID .get ())
162- .build ();
163-
164- return fontAttributes ;
138+
139+ try (final BoolPointer isBold = new BoolPointer (1 );
140+ final BoolPointer isItalic = new BoolPointer (1 );
141+ final BoolPointer isUnderlined = new BoolPointer (1 );
142+ final BoolPointer isMonospace = new BoolPointer (1 );
143+ final BoolPointer isSerif = new BoolPointer (1 );
144+ final BoolPointer isSmallCaps = new BoolPointer (1 );
145+ final IntPointer fontSize = new IntPointer (1 );
146+ final IntPointer fontID = new IntPointer (1 )) {
147+
148+ // set values
149+ tesseract .TessResultIteratorWordFontAttributes (resultIt , isBold , isItalic , isUnderlined , isMonospace ,
150+ isSerif ,
151+ isSmallCaps , fontSize , fontID );
152+
153+ // build and return FontAttributes
154+ final FontAttributes fontAttributes = new FontAttributes .Builder ()
155+ .bold (isBold .get ())
156+ .italic (isItalic .get ())
157+ .underlined (isUnderlined .get ())
158+ .monospace (isMonospace .get ())
159+ .serif (isSerif .get ())
160+ .smallcaps (isSmallCaps .get ())
161+ .size (fontSize .get ())
162+ .fontID (fontID .get ())
163+ .build ();
164+
165+ return fontAttributes ;
166+ }
165167 }
166168
167169 /**
0 commit comments