├── .classpath
├── .gitattributes
├── .gitignore
├── .idea
├── .name
├── compiler.xml
├── copyright
│ └── profiles_settings.xml
├── dictionaries
│ └── zhanghong.xml
├── encodings.xml
├── libraries
│ └── libs.xml
├── misc.xml
├── modules.xml
├── scopes
│ └── scope_settings.xml
├── vcs.xml
└── workspace.xml
├── .project
├── .settings
└── org.eclipse.jdt.core.prefs
├── Java_OCR.iml
├── README.md
├── bin
├── com
│ ├── recognition
│ │ └── software
│ │ │ └── jdeskew
│ │ │ ├── ImageDeskew$HoughLine.class
│ │ │ ├── ImageDeskew.class
│ │ │ └── ImageUtil.class
│ └── ricky
│ │ └── java
│ │ └── ocr
│ │ ├── TessAPITest$TessDllAPIImpl.class
│ │ ├── TessAPITest.class
│ │ ├── Tesseract1Test$Tess1Extension.class
│ │ ├── Tesseract1Test$Word.class
│ │ └── Tesseract1Test.class
├── gsdll64.dll
├── liblept168.dll
├── libtesseract302.dll
├── net
│ └── sourceforge
│ │ ├── tess4j
│ │ ├── ITesseract.class
│ │ ├── TessAPI$ETEXT_DESC.class
│ │ ├── TessAPI$TessBaseAPI.class
│ │ ├── TessAPI$TessMutableIterator.class
│ │ ├── TessAPI$TessOcrEngineMode.class
│ │ ├── TessAPI$TessOrientation.class
│ │ ├── TessAPI$TessPageIterator.class
│ │ ├── TessAPI$TessPageIteratorLevel.class
│ │ ├── TessAPI$TessPageSegMode.class
│ │ ├── TessAPI$TessPolyBlockType.class
│ │ ├── TessAPI$TessResultIterator.class
│ │ ├── TessAPI$TessTextlineOrder.class
│ │ ├── TessAPI$TessWritingDirection.class
│ │ ├── TessAPI.class
│ │ ├── TessAPI1$ETEXT_DESC.class
│ │ ├── TessAPI1$TessBaseAPI.class
│ │ ├── TessAPI1$TessMutableIterator.class
│ │ ├── TessAPI1$TessOcrEngineMode.class
│ │ ├── TessAPI1$TessOrientation.class
│ │ ├── TessAPI1$TessPageIterator.class
│ │ ├── TessAPI1$TessPageIteratorLevel.class
│ │ ├── TessAPI1$TessPageSegMode.class
│ │ ├── TessAPI1$TessPolyBlockType.class
│ │ ├── TessAPI1$TessResultIterator.class
│ │ ├── TessAPI1$TessTextlineOrder.class
│ │ ├── TessAPI1$TessWritingDirection.class
│ │ ├── TessAPI1.class
│ │ ├── Tesseract.class
│ │ ├── Tesseract1.class
│ │ ├── TesseractException.class
│ │ └── util
│ │ │ └── Utils.class
│ │ └── vietocr
│ │ ├── ImageHelper.class
│ │ ├── ImageIOHelper.class
│ │ ├── PdfUtilities$1.class
│ │ ├── PdfUtilities$2.class
│ │ └── PdfUtilities.class
└── testimg
│ ├── OCR_tesseract.class
│ ├── TesseractExp.class
│ └── TestOCR.class
├── chi.jpg
├── chi1.jpg
├── eurotext.bmp
├── eurotext.gif
├── eurotext.pdf
├── eurotext.png
├── eurotext.tif
├── libs
├── ghost4j-0.5.1.jar
├── hamcrest-core-1.3.jar
├── jai_imageio.jar
├── jna-4.1.0.jar
└── log4j-1.2.17.jar
├── src
├── com
│ └── recognition
│ │ └── software
│ │ └── jdeskew
│ │ ├── ImageDeskew.java
│ │ └── ImageUtil.java
├── gsdll64.dll
├── liblept168.dll
├── libtesseract302.dll
└── net
│ └── sourceforge
│ ├── tess4j
│ ├── ITesseract.java
│ ├── TessAPI.java
│ ├── TessAPI1.java
│ ├── Tesseract.java
│ ├── Tesseract1.java
│ ├── TesseractException.java
│ └── util
│ │ └── Utils.java
│ └── vietocr
│ ├── ImageHelper.java
│ ├── ImageIOHelper.java
│ └── PdfUtilities.java
├── tessdata
├── chi_sim.traineddata
├── chi_tra.traineddata
├── configs
│ ├── api_config
│ ├── digits
│ └── hocr
└── eng.traineddata
└── test
├── com
└── ricky
│ └── java
│ └── ocr
│ ├── TessAPITest.java
│ └── Tesseract1Test.java
└── testimg
├── OCR_tesseract.java
├── TesseractExp.java
└── TestOCR.java
/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
null
indicates
53 | * the whole image.
54 | * @return the recognized text
55 | * @throws TesseractException
56 | */
57 | String doOCR(File imageFile, Rectangle rect) throws TesseractException;
58 |
59 | /**
60 | * Performs OCR operation.
61 | *
62 | * @param bi a buffered image
63 | * @return the recognized text
64 | * @throws TesseractException
65 | */
66 | String doOCR(BufferedImage bi) throws TesseractException;
67 |
68 | /**
69 | * Performs OCR operation.
70 | *
71 | * @param bi a buffered image
72 | * @param rect the bounding rectangle defines the region of the image to be
73 | * recognized. A rectangle of zero dimension or null
indicates
74 | * the whole image.
75 | * @return the recognized text
76 | * @throws TesseractException
77 | */
78 | String doOCR(BufferedImage bi, Rectangle rect) throws TesseractException;
79 |
80 | /**
81 | * Performs OCR operation.
82 | *
83 | * @param imageList a list of IIOImage
objects
84 | * @param rect the bounding rectangle defines the region of the image to be
85 | * recognized. A rectangle of zero dimension or null
indicates
86 | * the whole image.
87 | * @return the recognized text
88 | * @throws TesseractException
89 | */
90 | String doOCR(ListSetImage
, (optionally)
94 | * SetRectangle
, and one or more of the Get*Text
95 | * functions.
96 | *
97 | * @param xsize width of image
98 | * @param ysize height of image
99 | * @param buf pixel data
100 | * @param rect the bounding rectangle defines the region of the image to be
101 | * recognized. A rectangle of zero dimension or null
indicates
102 | * the whole image.
103 | * @param bpp bits per pixel, represents the bit depth of the image, with 1
104 | * for binary bitmap, 8 for gray, and 24 for color RGB.
105 | * @return the recognized text
106 | * @throws TesseractException
107 | */
108 | String doOCR(int xsize, int ysize, ByteBuffer buf, Rectangle rect, int bpp) throws TesseractException;
109 |
110 | /**
111 | * Sets tessdata path.
112 | *
113 | * @param datapath the tessdata path to set
114 | */
115 | void setDatapath(String datapath);
116 |
117 | /**
118 | * Sets language for OCR.
119 | *
120 | * @param language the language code, which follows ISO 639-3 standard.
121 | */
122 | void setLanguage(String language);
123 |
124 | /**
125 | * Sets OCR engine mode.
126 | *
127 | * @param ocrEngineMode the OcrEngineMode to set
128 | */
129 | void setOcrEngineMode(int ocrEngineMode);
130 |
131 | /**
132 | * Sets page segmentation mode.
133 | *
134 | * @param mode the page segmentation mode to set
135 | */
136 | void setPageSegMode(int mode);
137 |
138 | /**
139 | * Sets the value of Tesseract's internal parameter.
140 | *
141 | * @param key variable name, e.g., tessedit_create_hocr
,
142 | * tessedit_char_whitelist
, etc.
143 | * @param value value for corresponding variable, e.g., "1", "0",
144 | * "0123456789", etc.
145 | */
146 | void setTessVariable(String key, String value);
147 | }
148 |
--------------------------------------------------------------------------------
/src/net/sourceforge/tess4j/TessAPI.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright @ 2012 Quan Nguyen
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 | * use this file except in compliance with the License. You may obtain a copy of
6 | * the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 | * License for the specific language governing permissions and limitations under
14 | * the License.
15 | */
16 | package net.sourceforge.tess4j;
17 |
18 | import com.sun.jna.*;
19 | import com.sun.jna.ptr.*;
20 | import java.nio.*;
21 |
22 | /**
23 | * A Java wrapper for
24 | * Tesseract OCR 3.02 API
using
25 | * JNA Interface Mapping
.
26 | */
27 | public interface TessAPI extends Library {
28 |
29 | static final boolean WINDOWS = System.getProperty("os.name").toLowerCase().startsWith("windows");
30 | /**
31 | * Native library name.
32 | */
33 | public static final String LIB_NAME = "libtesseract302";
34 | public static final String LIB_NAME_NON_WIN = "tesseract";
35 | /**
36 | * An instance of the class library.
37 | */
38 | public static final TessAPI INSTANCE = (TessAPI) Native.loadLibrary(WINDOWS ? LIB_NAME : LIB_NAME_NON_WIN, TessAPI.class);
39 |
40 | /**
41 | * When Tesseract/Cube is initialized we can choose to instantiate/load/run
42 | * only the Tesseract part, only the Cube part or both along with the
43 | * combiner. The preference of which engine to use is stored in
44 | * tessedit_ocr_engine_mode
.OSD_ONLY
, so that the inequality test macros below work.
72 | */
73 | public static interface TessPageSegMode {
74 |
75 | /** Orientation and script detection only. */
76 | public static final int PSM_OSD_ONLY = (int) 0;
77 | /** Automatic page segmentation with orientation and script detection. (OSD) */
78 | public static final int PSM_AUTO_OSD = (int) 1;
79 | /** Automatic page segmentation, but no OSD, or OCR. */
80 | public static final int PSM_AUTO_ONLY = (int) 2;
81 | /** Fully automatic page segmentation, but no OSD. */
82 | public static final int PSM_AUTO = (int) 3;
83 | /** Assume a single column of text of variable sizes. */
84 | public static final int PSM_SINGLE_COLUMN = (int) 4;
85 | /** Assume a single uniform block of vertically aligned text. */
86 | public static final int PSM_SINGLE_BLOCK_VERT_TEXT = (int) 5;
87 | /** Assume a single uniform block of text. */
88 | public static final int PSM_SINGLE_BLOCK = (int) 6;
89 | /** Treat the image as a single text line. */
90 | public static final int PSM_SINGLE_LINE = (int) 7;
91 | /** Treat the image as a single word. */
92 | public static final int PSM_SINGLE_WORD = (int) 8;
93 | /** Treat the image as a single word in a circle. */
94 | public static final int PSM_CIRCLE_WORD = (int) 9;
95 | /** Treat the image as a single character. */
96 | public static final int PSM_SINGLE_CHAR = (int) 10;
97 | /** Find as much text as possible in no particular order. */
98 | public static final int PSM_SPARSE_TEXT = (int) 11;
99 | /** Sparse text with orientation and script detection. */
100 | public static final int PSM_SPARSE_TEXT_OSD = (int) 12;
101 | /** Number of enum entries. */
102 | public static final int PSM_COUNT = (int) 13;
103 | };
104 |
105 | /**
106 | * Enum of the elements of the page hierarchy, used in
107 | * ResultIterator
to provide functions that operate on each
108 | * level without having to have 5x as many functions.
109 | */
110 | public static interface TessPageIteratorLevel {
111 |
112 | /** Block of text/image/separator line. */
113 | public static final int RIL_BLOCK = (int) 0;
114 | /** Paragraph within a block. */
115 | public static final int RIL_PARA = (int) 1;
116 | /** Line within a paragraph. */
117 | public static final int RIL_TEXTLINE = (int) 2;
118 | /** Word within a textline. */
119 | public static final int RIL_WORD = (int) 3;
120 | /** Symbol/character within a word. */
121 | public static final int RIL_SYMBOL = (int) 4;
122 | };
123 |
124 | public static interface TessPolyBlockType {
125 |
126 | /** Type is not yet known. Keep as the first element. */
127 | public static final int PT_UNKNOWN = (int) 0;
128 | /** Text that lives inside a column. */
129 | public static final int PT_FLOWING_TEXT = (int) 1;
130 | /** Text that spans more than one column. */
131 | public static final int PT_HEADING_TEXT = (int) 2;
132 | /** Text that is in a cross-column pull-out region. */
133 | public static final int PT_PULLOUT_TEXT = (int) 3;
134 | /** Partition belonging to an equation region. */
135 | public static final int PT_EQUATION = (int) 4;
136 | /** Partition has inline equation. */
137 | public static final int PT_INLINE_EQUATION = (int) 5;
138 | /** Partition belonging to a table region. */
139 | public static final int PT_TABLE = (int) 6;
140 | /** Text-line runs vertically. */
141 | public static final int PT_VERTICAL_TEXT = (int) 7;
142 | /** Text that belongs to an image. */
143 | public static final int PT_CAPTION_TEXT = (int) 8;
144 | /** Image that lives inside a column. */
145 | public static final int PT_FLOWING_IMAGE = (int) 9;
146 | /** Image that spans more than one column. */
147 | public static final int PT_HEADING_IMAGE = (int) 10;
148 | /** Image that is in a cross-column pull-out region. */
149 | public static final int PT_PULLOUT_IMAGE = (int) 11;
150 | /** Horizontal Line. */
151 | public static final int PT_HORZ_LINE = (int) 12;
152 | /** Vertical Line. */
153 | public static final int PT_VERT_LINE = (int) 13;
154 | /** Lies outside of any column. */
155 | public static final int PT_NOISE = (int) 14;
156 | /** Number of enum entries. */
157 | public static final int PT_COUNT = (int) 15;
158 | };
159 |
160 | /**
161 | * 162 | * +------------------+ 163 | * | 1 Aaaa Aaaa Aaaa | 164 | * | Aaa aa aaa aa | 165 | * | aaaaaa A aa aaa. | 166 | * | 2 | 167 | * | ####### c c C | 168 | * | ####### c c c | 169 | * | < ####### c c c | 170 | * | < ####### c c | 171 | * | < ####### . c | 172 | * | 3 ####### c | 173 | * +------------------+ 174 | *175 | * Orientation Example:
WRITING_DIRECTION_LEFT_TO_RIGHT
implies
226 | * TEXTLINE_ORDER_TOP_TO_BOTTOM
.
227 | */
228 | public static interface TessTextlineOrder {
229 |
230 | public static final int TEXTLINE_ORDER_LEFT_TO_RIGHT = (int) 0;
231 | public static final int TEXTLINE_ORDER_RIGHT_TO_LEFT = (int) 1;
232 | public static final int TEXTLINE_ORDER_TOP_TO_BOTTOM = (int) 2;
233 | };
234 | public static final int TRUE = (int) 1;
235 | public static final int FALSE = (int) 0;
236 |
237 | /**
238 | * Returns the version identifier.
239 | */
240 | String TessVersion();
241 |
242 | void TessDeleteText(Pointer text);
243 |
244 | void TessDeleteTextArray(PointerByReference arr);
245 |
246 | void TessDeleteIntArray(IntBuffer arr);
247 |
248 | /**
249 | * Creates an instance of the base class for all Tesseract APIs.
250 | */
251 | TessAPI.TessBaseAPI TessBaseAPICreate();
252 |
253 | /**
254 | * Disposes the TesseractAPI instance.
255 | */
256 | void TessBaseAPIDelete(TessAPI.TessBaseAPI handle);
257 |
258 | /**
259 | * Set the name of the input file. Needed only for training and reading a
260 | * UNLV zone file.
261 | */
262 | void TessBaseAPISetInputName(TessAPI.TessBaseAPI handle, String name);
263 |
264 | /**
265 | * Set the name of the bonus output files. Needed only for debugging.
266 | */
267 | void TessBaseAPISetOutputName(TessAPI.TessBaseAPI handle, String name);
268 |
269 | /**
270 | * Set the value of an internal "parameter." Supply the name of the
271 | * parameter and the value as a string, just as you would in a config file.
272 | * Returns false if the name lookup failed. E.g.,
273 | * SetVariable("tessedit_char_blacklist", "xyz");
to ignore x,
274 | * y and z. Or
275 | * SetVariable("classify_bln_numeric_mode", "1");
to set
276 | * numeric-only mode.
277 | * SetVariable
may be used before
278 | * Init
, but settings will revert to defaults on
279 | * End()
.Init()
. Only works for non-init variables (init variables
281 | * should be passed to
282 | * Init()
).
283 | */
284 | int TessBaseAPISetVariable(TessAPI.TessBaseAPI handle, String name, String value);
285 |
286 | /**
287 | * Returns true (1) if the parameter was found among Tesseract parameters.
288 | * Fills in value with the value of the parameter.
289 | */
290 | int TessBaseAPIGetIntVariable(TessAPI.TessBaseAPI handle, String name, IntBuffer value);
291 |
292 | int TessBaseAPIGetBoolVariable(TessAPI.TessBaseAPI handle, String name, IntBuffer value);
293 |
294 | int TessBaseAPIGetDoubleVariable(TessAPI.TessBaseAPI handle, String name, DoubleBuffer value);
295 |
296 | String TessBaseAPIGetStringVariable(TessAPI.TessBaseAPI handle, String name);
297 |
298 | /**
299 | * Print Tesseract parameters to the given file.SetVariable
on some of the Params in classify and textord.
309 | * If you do, then the effect will be to change it for all your
310 | * instances.datapath
must be the name of the parent directory of
314 | * tessdata and must end in / . Any name after the last / will be stripped.
315 | * The language is (usually) an
316 | * ISO 639-3
string or
317 | * NULL
will default to eng. It is entirely safe (and
318 | * eventually will be efficient too) to call Init multiple times on the same
319 | * instance to change language, or just to reset the classifier. The
320 | * language may be a string of the form [~]Init
you should explicitly call
334 | * End()
and then use
335 | * SetVariable
before
336 | * Init
. This is only a very rare use case, since there are
337 | * very few uses that require any parameters to be set before
338 | * Init
.set_only_non_debug_params
is true, only params that do not
340 | * contain "debug" in the name will be set.
341 | */
342 | int TessBaseAPIInit1(TessAPI.TessBaseAPI handle, String datapath, String language, int oem, PointerByReference configs, int configs_size);
343 |
344 | int TessBaseAPIInit2(TessAPI.TessBaseAPI handle, String datapath, String language, int oem);
345 |
346 | int TessBaseAPIInit3(TessAPI.TessBaseAPI handle, String datapath, String language);
347 |
348 | /**
349 | * Returns the languages string used in the last valid initialization. If
350 | * the last initialization specified "deu+hin" then that will be returned.
351 | * If hin loaded eng automatically as well, then that will not be included
352 | * in this list. To find the languages actually loaded, use
353 | * GetLoadedLanguagesAsVector
. The returned string should NOT
354 | * be deleted.
355 | */
356 | String TessBaseAPIGetInitLanguagesAsString(TessAPI.TessBaseAPI handle);
357 |
358 | /**
359 | * Returns the loaded languages in the vector of STRINGs. Includes all
360 | * languages loaded by the last
361 | * Init
, including those loaded as dependencies of other loaded
362 | * languages.
363 | */
364 | PointerByReference TessBaseAPIGetLoadedLanguagesAsVector(TessAPI.TessBaseAPI handle);
365 |
366 | /**
367 | * Returns the available languages in the vector of STRINGs.
368 | */
369 | PointerByReference TessBaseAPIGetAvailableLanguagesAsVector(TessAPI.TessBaseAPI handle);
370 |
371 | /**
372 | * Init only the lang model component of Tesseract. The only functions that
373 | * work after this init are
374 | * SetVariable
and
375 | * IsValidWord
. WARNING: temporary! This function will be
376 | * removed from here and placed in a separate API at some future time.
377 | */
378 | int TessBaseAPIInitLangMod(TessAPI.TessBaseAPI handle, String datapath, String language);
379 |
380 | /**
381 | * Init only for page layout analysis. Use only for calls to
382 | * SetImage
and
383 | * AnalysePage
. Calls that attempt recognition will generate an
384 | * error.
385 | */
386 | void TessBaseAPIInitForAnalysePage(TessAPI.TessBaseAPI handle);
387 |
388 | /**
389 | * Read a "config" file containing a set of param, value pairs. Searches the
390 | * standard places:
391 | * tessdata/configs
,
392 | * tessdata/tessconfigs
and also accepts a relative or absolute
393 | * path name. Note: only non-init params will be set (init params are set by
394 | * Init()
).
395 | */
396 | void TessBaseAPIReadConfigFile(TessAPI.TessBaseAPI handle, String filename, int init_only);
397 |
398 | /**
399 | * Set the current page segmentation mode. Defaults to PSM_SINGLE_BLOCK. The
400 | * mode is stored as an IntParam so it can also be modified by
401 | * ReadConfigFile
or
402 | * SetVariable("tessedit_pageseg_mode", mode as string)
.
403 | */
404 | void TessBaseAPISetPageSegMode(TessAPI.TessBaseAPI handle, int mode);
405 |
406 | /**
407 | * Return the current page segmentation mode.
408 | */
409 | int TessBaseAPIGetPageSegMode(TessAPI.TessBaseAPI handle);
410 |
411 | /**
412 | * Recognize a rectangle from an image and return the result as a string.
413 | * May be called many times for a single
414 | * Init
. Currently has no error checking. Greyscale of 8 and
415 | * color of 24 or 32 bits per pixel may be given. Palette color images will
416 | * not work properly and must be converted to 24 bit. Binary images of 1 bit
417 | * per pixel may also be given but they must be byte packed with the MSB of
418 | * the first byte being the first pixel, and a 1 represents WHITE. For
419 | * binary images set bytes_per_pixel=0. The recognized text is returned as a
420 | * char* which is coded as UTF8 and must be freed with the delete []
421 | * operator.TesseractRect
is the simplified convenience interface. For
423 | * advanced uses, use
424 | * SetImage
, (optionally)
425 | * SetRectangle
,
426 | * Recognize
, and one or more of the
427 | * Get*Text
functions below.
428 | */
429 | Pointer TessBaseAPIRect(TessAPI.TessBaseAPI handle, ByteBuffer imagedata, int bytes_per_pixel, int bytes_per_line, int left, int top, int width, int height);
430 |
431 | /**
432 | * Call between pages or documents etc to free up memory and forget adaptive
433 | * data.
434 | */
435 | void TessBaseAPIClearAdaptiveClassifier(TessAPI.TessBaseAPI handle);
436 |
437 | /**
438 | * Provide an image for Tesseract to recognize. Format is as TesseractRect
439 | * above. Does not copy the image buffer, or take ownership. The source
440 | * image may be destroyed after Recognize is called, either explicitly or
441 | * implicitly via one of the
442 | * Get*Text
functions.
443 | * SetImage
clears all recognition results, and sets the
444 | * rectangle to the full image, so it may be followed immediately by a
445 | * GetUTF8Text
, and it will automatically perform recognition.
446 | */
447 | void TessBaseAPISetImage(TessAPI.TessBaseAPI handle, ByteBuffer imagedata, int width, int height, int bytes_per_pixel, int bytes_per_line);
448 |
449 | /**
450 | * Set the resolution of the source image in pixels per inch so font size
451 | * information can be calculated in results. Call this after SetImage().
452 | */
453 | void TessBaseAPISetSourceResolution(TessAPI.TessBaseAPI handle, int ppi);
454 |
455 | /**
456 | * Restrict recognition to a sub-rectangle of the image. Call after
457 | * SetImage
. Each
458 | * SetRectangle
clears the recognition results so multiple
459 | * rectangles can be recognized with the same image.
460 | */
461 | void TessBaseAPISetRectangle(TessAPI.TessBaseAPI handle, int left, int top, int width, int height);
462 |
463 | /** Scale factor from original image. */
464 | int TessBaseAPIGetThresholdedImageScaleFactor(TessAPI.TessBaseAPI handle);
465 |
466 | /** Dump the internal binary image to a PGM file. */
467 | void TessBaseAPIDumpPGM(TessAPI.TessBaseAPI handle, String filename);
468 |
469 | /**
470 | * Runs page layout analysis in the mode set by SetPageSegMode. May
471 | * optionally be called prior to Recognize to get access to just the page
472 | * layout results. Returns an iterator to the results. Returns NULL on
473 | * error. The returned iterator must be deleted after use. WARNING! This
474 | * class points to data held within the TessBaseAPI class, and therefore can
475 | * only be used while the TessBaseAPI class still exists and has not been
476 | * subjected to a call of
477 | * Init
,
478 | * SetImage
,
479 | * Recognize
,
480 | * Clear
,
481 | * End
, DetectOS, or anything else that changes the internal
482 | * PAGE_RES.
483 | */
484 | TessAPI.TessPageIterator TessBaseAPIAnalyseLayout(TessAPI.TessBaseAPI handle);
485 |
486 | /**
487 | * Recognize the image from SetAndThresholdImage, generating Tesseract
488 | * internal structures. Returns 0 on success. Optional. The
489 | * Get*Text
functions below will call
490 | * Recognize
if needed. After Recognize, the output is kept
491 | * internally until the next
492 | * SetImage
.
493 | */
494 | int TessBaseAPIRecognize(TessAPI.TessBaseAPI handle, TessAPI.ETEXT_DESC monitor);
495 |
496 | /**
497 | * Variant on Recognize used for testing chopper.
498 | */
499 | int TessBaseAPIRecognizeForChopTest(TessAPI.TessBaseAPI handle, TessAPI.ETEXT_DESC monitor);
500 |
501 | /**
502 | * Get a reading-order iterator to the results of LayoutAnalysis and/or
503 | * Recognize. The returned iterator must be deleted after use. WARNING! This
504 | * class points to data held within the TessBaseAPI class, and therefore can
505 | * only be used while the TessBaseAPI class still exists and has not been
506 | * subjected to a call of
507 | * Init
,
508 | * SetImage
,
509 | * Recognize
,
510 | * Clear
,
511 | * End
, DetectOS, or anything else that changes the internal
512 | * PAGE_RES.
513 | */
514 | TessAPI.TessResultIterator TessBaseAPIGetIterator(TessAPI.TessBaseAPI handle);
515 |
516 | /**
517 | * Get a mutable iterator to the results of LayoutAnalysis and/or Recognize.
518 | * The returned iterator must be deleted after use.
519 | * WARNING! This class points to data held within the TessBaseAPI class, and
520 | * therefore can only be used while the TessBaseAPI class still exists and
521 | * has not been subjected to a call of Init, SetImage, Recognize, Clear, End
522 | * DetectOS, or anything else that changes the internal PAGE_RES.
523 | */
524 | TessAPI.TessMutableIterator TessBaseAPIGetMutableIterator(TessAPI.TessBaseAPI handle);
525 |
526 | /**
527 | * Recognizes all the pages in the named file, as a multi-page tiff or list
528 | * of filenames, or single image, and gets the appropriate kind of text
529 | * according to parameters:
530 | * tessedit_create_boxfile
,
531 | * tessedit_make_boxes_from_boxes
,
532 | * tessedit_write_unlv
,
533 | * tessedit_create_hocr
. Calls ProcessPage on each page in the
534 | * input file, which may be a multi-page tiff, single-page other file
535 | * format, or a plain text list of images to read. If tessedit_page_number
536 | * is non-negative, processing begins at that page of a multi-page tiff
537 | * file, or filelist. The text is returned in text_out. Returns false on
538 | * error. If non-zero timeout_millisec terminates processing after the
539 | * timeout on a single page. If non-NULL and non-empty, and some page fails
540 | * for some reason, the page is reprocessed with the retry_config config
541 | * file. Useful for interactively debugging a bad page.
542 | */
543 | Pointer TessBaseAPIProcessPages(TessAPI.TessBaseAPI handle, String filename, String retry_config, int timeout_millisec);
544 |
545 | /**
546 | * The recognized text is returned as a char* which is coded as UTF-8 and
547 | * must be freed with the delete [] operator.
548 | */
549 | Pointer TessBaseAPIGetUTF8Text(TessAPI.TessBaseAPI handle);
550 |
551 | /**
552 | * Make a HTML-formatted string with hOCR markup from the internal data
553 | * structures. page_number is 0-based but will appear in the output as
554 | * 1-based.
555 | */
556 | Pointer TessBaseAPIGetHOCRText(TessAPI.TessBaseAPI handle, int page_number);
557 |
558 | /**
559 | * The recognized text is returned as a char* which is coded in the same
560 | * format as a box file used in training. Returned string must be freed with
561 | * the delete [] operator. Constructs coordinates in the original image -
562 | * not just the rectangle. page_number is a 0-based page index that will
563 | * appear in the box file.
564 | */
565 | Pointer TessBaseAPIGetBoxText(TessAPI.TessBaseAPI handle, int page_number);
566 |
567 | /**
568 | * The recognized text is returned as a char* which is coded as UNLV format
569 | * Latin-1 with specific reject and suspect codes and must be freed with the
570 | * delete [] operator.
571 | */
572 | Pointer TessBaseAPIGetUNLVText(TessAPI.TessBaseAPI handle);
573 |
574 | /**
575 | * Returns the (average) confidence value between 0 and 100.
576 | */
577 | int TessBaseAPIMeanTextConf(TessAPI.TessBaseAPI handle);
578 |
579 | /**
580 | * Returns all word confidences (between 0 and 100) in an array, terminated
581 | * by -1. The calling function must delete [] after use. The number of
582 | * confidences should correspond to the number of space-delimited words in
583 | * GetUTF8Text.
584 | */
585 | IntByReference TessBaseAPIAllWordConfidences(TessAPI.TessBaseAPI handle);
586 |
587 | /**
588 | * Applies the given word to the adaptive classifier if possible. The word
589 | * must be SPACE-DELIMITED UTF-8 - l i k e t h i s , so it can tell the
590 | * boundaries of the graphemes. Assumes that SetImage/SetRectangle have been
591 | * used to set the image to the given word. The mode arg should be
592 | * PSM_SINGLE_WORD or PSM_CIRCLE_WORD, as that will be used to control
593 | * layout analysis. The currently set PageSegMode is preserved. Returns
594 | * false if adaption was not possible for some reason.
595 | */
596 | int TessBaseAPIAdaptToWordStr(TessAPI.TessBaseAPI handle, int mode, String wordstr);
597 |
598 | /**
599 | * Free up recognition results and any stored image data, without actually
600 | * freeing any recognition data that would be time-consuming to reload.
601 | * Afterwards, you must call
602 | * SetImage
or
603 | * TesseractRect
before doing any
604 | * Recognize
or
605 | * Get*
operation.
606 | */
607 | void TessBaseAPIClear(TessAPI.TessBaseAPI handle);
608 |
609 | /**
610 | * Close down tesseract and free up all memory.
611 | * End()
is equivalent to destructing and reconstructing your
612 | * TessBaseAPI. Once
613 | * End()
has been used, none of the other API functions may be
614 | * used other than
615 | * Init
and anything declared above it in the class definition.
616 | */
617 | void TessBaseAPIEnd(TessAPI.TessBaseAPI handle);
618 |
619 | /**
620 | * Check whether a word is valid according to Tesseract's language model.
621 | *
622 | * @return 0 if the word is invalid, non-zero if valid. @warning temporary!
623 | * This function will be removed from here and placed in a separate API at
624 | * some future time.
625 | */
626 | int TessBaseAPIIsValidWord(TessAPI.TessBaseAPI handle, String word);
627 |
628 | int TessBaseAPIGetTextDirection(TessAPI.TessBaseAPI handle, IntBuffer out_offset, FloatBuffer out_slope);
629 |
630 | /**
631 | * This method returns the string form of the specified unichar.
632 | */
633 | String TessBaseAPIGetUnichar(TessAPI.TessBaseAPI handle, int unichar_id);
634 |
635 | /* Page iterator */
636 | void TessPageIteratorDelete(TessAPI.TessPageIterator handle);
637 |
638 | TessAPI.TessPageIterator TessPageIteratorCopy(TessAPI.TessPageIterator handle);
639 |
640 | void TessPageIteratorBegin(TessAPI.TessPageIterator handle);
641 |
642 | int TessPageIteratorNext(TessAPI.TessPageIterator handle, int level);
643 |
644 | int TessPageIteratorIsAtBeginningOf(TessAPI.TessPageIterator handle, int level);
645 |
646 | int TessPageIteratorIsAtFinalElement(TessAPI.TessPageIterator handle, int level, int element);
647 |
648 | int TessPageIteratorBoundingBox(TessAPI.TessPageIterator handle, int level, IntBuffer left, IntBuffer top, IntBuffer right, IntBuffer bottom);
649 |
650 | int TessPageIteratorBlockType(TessAPI.TessPageIterator handle);
651 |
652 | int TessPageIteratorBaseline(TessAPI.TessPageIterator handle, int level, IntBuffer x1, IntBuffer y1, IntBuffer x2, IntBuffer y2);
653 |
654 | void TessPageIteratorOrientation(TessAPI.TessPageIterator handle, IntBuffer orientation, IntBuffer writing_direction, IntBuffer textline_order, FloatBuffer deskew_angle);
655 |
656 | /* Result iterator */
657 | void TessResultIteratorDelete(TessAPI.TessResultIterator handle);
658 |
659 | TessAPI.TessResultIterator TessResultIteratorCopy(TessAPI.TessResultIterator handle);
660 |
661 | TessAPI.TessPageIterator TessResultIteratorGetPageIterator(TessAPI.TessResultIterator handle);
662 |
663 | TessAPI.TessPageIterator TessResultIteratorGetPageIteratorConst(TessAPI.TessResultIterator handle);
664 |
665 | Pointer TessResultIteratorGetUTF8Text(TessAPI.TessResultIterator handle, int level);
666 |
667 | float TessResultIteratorConfidence(TessAPI.TessResultIterator handle, int level);
668 |
669 | String TessResultIteratorWordFontAttributes(TessAPI.TessResultIterator handle, IntBuffer is_bold, IntBuffer is_italic, IntBuffer is_underlined, IntBuffer is_monospace, IntBuffer is_serif, IntBuffer is_smallcaps, IntBuffer pointsize, IntBuffer font_id);
670 |
671 | int TessResultIteratorWordIsFromDictionary(TessAPI.TessResultIterator handle);
672 |
673 | int TessResultIteratorWordIsNumeric(TessAPI.TessResultIterator handle);
674 |
675 | int TessResultIteratorSymbolIsSuperscript(TessAPI.TessResultIterator handle);
676 |
677 | int TessResultIteratorSymbolIsSubscript(TessAPI.TessResultIterator handle);
678 |
679 | int TessResultIteratorSymbolIsDropcap(TessAPI.TessResultIterator handle);
680 |
681 | public static class TessBaseAPI extends PointerType {
682 |
683 | public TessBaseAPI(Pointer address) {
684 | super(address);
685 | }
686 |
687 | public TessBaseAPI() {
688 | super();
689 | }
690 | };
691 |
692 | public static class ETEXT_DESC extends PointerType {
693 |
694 | public ETEXT_DESC(Pointer address) {
695 | super(address);
696 | }
697 |
698 | public ETEXT_DESC() {
699 | super();
700 | }
701 | };
702 |
703 | public static class TessPageIterator extends PointerType {
704 |
705 | public TessPageIterator(Pointer address) {
706 | super(address);
707 | }
708 |
709 | public TessPageIterator() {
710 | super();
711 | }
712 | };
713 |
714 | public static class TessMutableIterator extends PointerType {
715 |
716 | public TessMutableIterator(Pointer address) {
717 | super(address);
718 | }
719 |
720 | public TessMutableIterator() {
721 | super();
722 | }
723 | };
724 |
725 | public static class TessResultIterator extends PointerType {
726 |
727 | public TessResultIterator(Pointer address) {
728 | super(address);
729 | }
730 |
731 | public TessResultIterator() {
732 | super();
733 | }
734 | };
735 | }
736 |
--------------------------------------------------------------------------------
/src/net/sourceforge/tess4j/TessAPI1.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright @ 2012 Quan Nguyen
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 | * use this file except in compliance with the License. You may obtain a copy of
6 | * the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 | * License for the specific language governing permissions and limitations under
14 | * the License.
15 | */
16 | package net.sourceforge.tess4j;
17 |
18 | import com.sun.jna.*;
19 | import com.sun.jna.ptr.*;
20 | import java.nio.*;
21 |
22 | /**
23 | * A Java wrapper for
24 | * Tesseract OCR 3.02 API
using
25 | * JNA Direct Mapping
.
26 | */
27 | public class TessAPI1 implements Library {
28 |
29 | static final boolean WINDOWS = System.getProperty("os.name").toLowerCase().startsWith("windows");
30 | /**
31 | * Native library name.
32 | */
33 | public static final String LIB_NAME = "libtesseract302";
34 | public static final String LIB_NAME_NON_WIN = "tesseract";
35 |
36 | static {
37 | Native.register(WINDOWS ? LIB_NAME : LIB_NAME_NON_WIN);
38 | }
39 |
40 | /**
41 | * When Tesseract/Cube is initialized we can choose to instantiate/load/run
42 | * only the Tesseract part, only the Cube part or both along with the
43 | * combiner. The preference of which engine to use is stored in
44 | * tessedit_ocr_engine_mode
.OSD_ONLY
, so
72 | * that the inequality test macros below work.
73 | */
74 | public static interface TessPageSegMode {
75 |
76 | /** Orientation and script detection only. */
77 | public static final int PSM_OSD_ONLY = (int) 0;
78 | /** Automatic page segmentation with orientation and script detection. (OSD) */
79 | public static final int PSM_AUTO_OSD = (int) 1;
80 | /** Automatic page segmentation, but no OSD, or OCR. */
81 | public static final int PSM_AUTO_ONLY = (int) 2;
82 | /** Fully automatic page segmentation, but no OSD. */
83 | public static final int PSM_AUTO = (int) 3;
84 | /** Assume a single column of text of variable sizes. */
85 | public static final int PSM_SINGLE_COLUMN = (int) 4;
86 | /** Assume a single uniform block of vertically aligned text. */
87 | public static final int PSM_SINGLE_BLOCK_VERT_TEXT = (int) 5;
88 | /** Assume a single uniform block of text. */
89 | public static final int PSM_SINGLE_BLOCK = (int) 6;
90 | /** Treat the image as a single text line. */
91 | public static final int PSM_SINGLE_LINE = (int) 7;
92 | /** Treat the image as a single word. */
93 | public static final int PSM_SINGLE_WORD = (int) 8;
94 | /** Treat the image as a single word in a circle. */
95 | public static final int PSM_CIRCLE_WORD = (int) 9;
96 | /** Treat the image as a single character. */
97 | public static final int PSM_SINGLE_CHAR = (int) 10;
98 | /** Find as much text as possible in no particular order. */
99 | public static final int PSM_SPARSE_TEXT = (int) 11;
100 | /** Sparse text with orientation and script detection. */
101 | public static final int PSM_SPARSE_TEXT_OSD = (int) 12;
102 | /** Number of enum entries. */
103 | public static final int PSM_COUNT = (int) 13;
104 | };
105 |
106 | /**
107 | * Enum of the elements of the page hierarchy, used in ResultIterator
to
108 | * provide functions that operate on each level without having to have 5x as
109 | * many functions.
110 | */
111 | public static interface TessPageIteratorLevel {
112 |
113 | /** Block of text/image/separator line. */
114 | public static final int RIL_BLOCK = (int) 0;
115 | /** Paragraph within a block. */
116 | public static final int RIL_PARA = (int) 1;
117 | /** Line within a paragraph. */
118 | public static final int RIL_TEXTLINE = (int) 2;
119 | /** Word within a textline. */
120 | public static final int RIL_WORD = (int) 3;
121 | /** Symbol/character within a word. */
122 | public static final int RIL_SYMBOL = (int) 4;
123 | };
124 |
125 | public static interface TessPolyBlockType {
126 |
127 | /** Type is not yet known. Keep as the first element. */
128 | public static final int PT_UNKNOWN = (int) 0;
129 | /** Text that lives inside a column. */
130 | public static final int PT_FLOWING_TEXT = (int) 1;
131 | /** Text that spans more than one column. */
132 | public static final int PT_HEADING_TEXT = (int) 2;
133 | /** Text that is in a cross-column pull-out region. */
134 | public static final int PT_PULLOUT_TEXT = (int) 3;
135 | /** Partition belonging to an equation region. */
136 | public static final int PT_EQUATION = (int) 4;
137 | /** Partition has inline equation. */
138 | public static final int PT_INLINE_EQUATION = (int) 5;
139 | /** Partition belonging to a table region. */
140 | public static final int PT_TABLE = (int) 6;
141 | /** Text-line runs vertically. */
142 | public static final int PT_VERTICAL_TEXT = (int) 7;
143 | /** Text that belongs to an image. */
144 | public static final int PT_CAPTION_TEXT = (int) 8;
145 | /** Image that lives inside a column. */
146 | public static final int PT_FLOWING_IMAGE = (int) 9;
147 | /** Image that spans more than one column. */
148 | public static final int PT_HEADING_IMAGE = (int) 10;
149 | /** Image that is in a cross-column pull-out region. */
150 | public static final int PT_PULLOUT_IMAGE = (int) 11;
151 | /** Horizontal Line. */
152 | public static final int PT_HORZ_LINE = (int) 12;
153 | /** Vertical Line. */
154 | public static final int PT_VERT_LINE = (int) 13;
155 | /** Lies outside of any column. */
156 | public static final int PT_NOISE = (int) 14;
157 | /** Number of enum entries. */
158 | public static final int PT_COUNT = (int) 15;
159 | };
160 |
161 | /**
162 | * 163 | * +------------------+ 164 | * | 1 Aaaa Aaaa Aaaa | 165 | * | Aaa aa aaa aa | 166 | * | aaaaaa A aa aaa. | 167 | * | 2 | 168 | * | ####### c c C | 169 | * | ####### c c c | 170 | * | < ####### c c c | 171 | * | < ####### c c | 172 | * | < ####### . c | 173 | * | 3 ####### c | 174 | * +------------------+ 175 | *176 | * Orientation Example:
WRITING_DIRECTION_LEFT_TO_RIGHT
implies
228 | * TEXTLINE_ORDER_TOP_TO_BOTTOM
.
229 | */
230 | public static interface TessTextlineOrder {
231 |
232 | public static final int TEXTLINE_ORDER_LEFT_TO_RIGHT = (int) 0;
233 | public static final int TEXTLINE_ORDER_RIGHT_TO_LEFT = (int) 1;
234 | public static final int TEXTLINE_ORDER_TOP_TO_BOTTOM = (int) 2;
235 | };
236 | public static final int TRUE = (int) 1;
237 | public static final int FALSE = (int) 0;
238 |
239 | /**
240 | * Returns the version identifier.
241 | */
242 | public static native String TessVersion();
243 |
244 | public static native void TessDeleteText(Pointer text);
245 |
246 | public static native void TessDeleteTextArray(PointerByReference arr);
247 |
248 | public static native void TessDeleteIntArray(IntBuffer arr);
249 |
250 | /**
251 | * Creates an instance of the base class for all Tesseract APIs.
252 | */
253 | public static native TessAPI1.TessBaseAPI TessBaseAPICreate();
254 |
255 | /**
256 | * Disposes the TesseractAPI instance.
257 | */
258 | public static native void TessBaseAPIDelete(TessAPI1.TessBaseAPI handle);
259 |
260 | /**
261 | * Set the name of the input file. Needed only for training and reading a
262 | * UNLV zone file.
263 | */
264 | public static native void TessBaseAPISetInputName(TessAPI1.TessBaseAPI handle, String name);
265 |
266 | /**
267 | * Set the name of the bonus output files. Needed only for debugging.
268 | */
269 | public static native void TessBaseAPISetOutputName(TessAPI1.TessBaseAPI handle, String name);
270 |
271 | /**
272 | * Set the value of an internal "parameter." Supply the name of the
273 | * parameter and the value as a string, just as you would in a config file.
274 | * Returns false if the name lookup failed. E.g.,
275 | * SetVariable("tessedit_char_blacklist", "xyz");
to ignore x, y and z. Or
276 | * SetVariable("classify_bln_numeric_mode", "1");
to set numeric-only mode.
277 | * SetVariable
may be used before Init
, but settings will revert to defaults
278 | * on End()
.Init()
. Only works for non-init variables
281 | * (init variables should be passed to Init()
).
282 | */
283 | public static native int TessBaseAPISetVariable(TessAPI1.TessBaseAPI handle, String name, String value);
284 |
285 | /**
286 | * Returns true (1) if the parameter was found among Tesseract parameters.
287 | * Fills in value with the value of the parameter.
288 | */
289 | public static native int TessBaseAPIGetIntVariable(TessAPI1.TessBaseAPI handle, String name, IntBuffer value);
290 |
291 | public static native int TessBaseAPIGetBoolVariable(TessAPI1.TessBaseAPI handle, String name, IntBuffer value);
292 |
293 | public static native int TessBaseAPIGetDoubleVariable(TessAPI1.TessBaseAPI handle, String name, DoubleBuffer value);
294 |
295 | public static native String TessBaseAPIGetStringVariable(TessAPI1.TessBaseAPI handle, String name);
296 |
297 | /**
298 | * Print Tesseract parameters to the given file.SetVariable
on some of the Params in classify and textord. If you do,
309 | * then the effect will be to change it for all your instances.datapath
must be the name of the parent directory of tessdata and
316 | * must end in / . Any name after the last / will be stripped. The language
317 | * is (usually) an ISO 639-3
string or NULL
will default to eng. It is
318 | * entirely safe (and eventually will be efficient too) to call Init
319 | * multiple times on the same instance to change language, or just to reset
320 | * the classifier. The language may be a string of the form
321 | * [~]Init
you
333 | * should explicitly call End()
and then use SetVariable
before Init
. This
334 | * is only a very rare use case, since there are very few uses that require
335 | * any parameters to be set before Init
.set_only_non_debug_params
is true, only params that do not contain
338 | * "debug" in the name will be set.
339 | */
340 | public static native int TessBaseAPIInit1(TessAPI1.TessBaseAPI handle, String datapath, String language, int oem, PointerByReference configs, int configs_size);
341 |
342 | public static native int TessBaseAPIInit2(TessAPI1.TessBaseAPI handle, String datapath, String language, int oem);
343 |
344 | public static native int TessBaseAPIInit3(TessAPI1.TessBaseAPI handle, String datapath, String language);
345 |
346 | /**
347 | * Returns the languages string used in the last valid initialization. If
348 | * the last initialization specified "deu+hin" then that will be returned.
349 | * If hin loaded eng automatically as well, then that will not be included
350 | * in this list. To find the languages actually loaded, use
351 | * GetLoadedLanguagesAsVector
. The returned string should NOT be deleted.
352 | */
353 | public static native String TessBaseAPIGetInitLanguagesAsString(TessAPI1.TessBaseAPI handle);
354 |
355 | /**
356 | * Returns the loaded languages in the vector of STRINGs. Includes all
357 | * languages loaded by the last Init
, including those loaded as dependencies
358 | * of other loaded languages.
359 | */
360 | public static native PointerByReference TessBaseAPIGetLoadedLanguagesAsVector(TessAPI1.TessBaseAPI handle);
361 |
362 | /**
363 | * Returns the available languages in the vector of STRINGs.
364 | */
365 | public static native PointerByReference TessBaseAPIGetAvailableLanguagesAsVector(TessAPI1.TessBaseAPI handle);
366 |
367 | /**
368 | * Init only the lang model component of Tesseract. The only functions that
369 | * work after this init are SetVariable
and IsValidWord
. WARNING: temporary!
370 | * This function will be removed from here and placed in a separate API at
371 | * some future time.
372 | */
373 | public static native int TessBaseAPIInitLangMod(TessAPI1.TessBaseAPI handle, String datapath, String language);
374 |
375 | /**
376 | * Init only for page layout analysis. Use only for calls to SetImage
and
377 | * AnalysePage
. Calls that attempt recognition will generate an error.
378 | */
379 | public static native void TessBaseAPIInitForAnalysePage(TessAPI1.TessBaseAPI handle);
380 |
381 | /**
382 | * Read a "config" file containing a set of param, value pairs. Searches the
383 | * standard places: tessdata/configs
, tessdata/tessconfigs
and also accepts
384 | * a relative or absolute path name. Note: only non-init params will be set
385 | * (init params are set by Init()
).
386 | */
387 | public static native void TessBaseAPIReadConfigFile(TessAPI1.TessBaseAPI handle, String filename, int init_only);
388 |
389 | /**
390 | * Set the current page segmentation mode. Defaults to PSM_SINGLE_BLOCK. The
391 | * mode is stored as an IntParam so it can also be modified by
392 | * ReadConfigFile
or SetVariable("tessedit_pageseg_mode", mode as string)
.
393 | */
394 | public static native void TessBaseAPISetPageSegMode(TessAPI1.TessBaseAPI handle, int mode);
395 |
396 | /**
397 | * Return the current page segmentation mode.
398 | */
399 | public static native int TessBaseAPIGetPageSegMode(TessAPI1.TessBaseAPI handle);
400 |
401 | /**
402 | * Recognize a rectangle from an image and return the result as a string.
403 | * May be called many times for a single Init
. Currently has no error
404 | * checking. Greyscale of 8 and color of 24 or 32 bits per pixel may be
405 | * given. Palette color images will not work properly and must be converted
406 | * to 24 bit. Binary images of 1 bit per pixel may also be given but they
407 | * must be byte packed with the MSB of the first byte being the first pixel,
408 | * and a 1 represents WHITE. For binary images set bytes_per_pixel=0. The
409 | * recognized text is returned as a char* which is coded as UTF8 and must be
410 | * freed with the delete [] operator.TesseractRect
is the simplified convenience interface. For
413 | * advanced uses, use SetImage
, (optionally) SetRectangle
, Recognize
, and
414 | * one or more of the Get*Text
functions below.
415 | */
416 | public static native Pointer TessBaseAPIRect(TessAPI1.TessBaseAPI handle, ByteBuffer imagedata, int bytes_per_pixel, int bytes_per_line, int left, int top, int width, int height);
417 |
418 | /**
419 | * Call between pages or documents etc to free up memory and forget adaptive
420 | * data.
421 | */
422 | public static native void TessBaseAPIClearAdaptiveClassifier(TessAPI1.TessBaseAPI handle);
423 |
424 | /**
425 | * Provide an image for Tesseract to recognize. Format is as TesseractRect
426 | * above. Does not copy the image buffer, or take ownership. The source
427 | * image may be destroyed after Recognize is called, either explicitly or
428 | * implicitly via one of the Get*Text
functions. SetImage
clears all
429 | * recognition results, and sets the rectangle to the full image, so it may
430 | * be followed immediately by a GetUTF8Text
, and it will automatically
431 | * perform recognition.
432 | */
433 | public static native void TessBaseAPISetImage(TessAPI1.TessBaseAPI handle, ByteBuffer imagedata, int width, int height, int bytes_per_pixel, int bytes_per_line);
434 |
435 | /**
436 | * Set the resolution of the source image in pixels per inch so font size
437 | * information can be calculated in results. Call this after SetImage().
438 | */
439 | public static native void TessBaseAPISetSourceResolution(TessAPI1.TessBaseAPI handle, int ppi);
440 |
441 | /**
442 | * Restrict recognition to a sub-rectangle of the image. Call after
443 | * SetImage
. Each SetRectangle
clears the recognition results so multiple
444 | * rectangles can be recognized with the same image.
445 | */
446 | public static native void TessBaseAPISetRectangle(TessAPI1.TessBaseAPI handle, int left, int top, int width, int height);
447 |
448 | /** Scale factor from original image. */
449 | public static native int TessBaseAPIGetThresholdedImageScaleFactor(TessAPI1.TessBaseAPI handle);
450 |
451 | /** Dump the internal binary image to a PGM file. */
452 | public static native void TessBaseAPIDumpPGM(TessAPI1.TessBaseAPI handle, String filename);
453 |
454 | /**
455 | * Runs page layout analysis in the mode set by SetPageSegMode. May
456 | * optionally be called prior to Recognize to get access to just the page
457 | * layout results. Returns an iterator to the results. Returns NULL on
458 | * error. The returned iterator must be deleted after use. WARNING! This
459 | * class points to data held within the TessBaseAPI class, and therefore can
460 | * only be used while the TessBaseAPI class still exists and has not been
461 | * subjected to a call of Init
, SetImage
, Recognize
, Clear
, End
, DetectOS, or
462 | * anything else that changes the internal PAGE_RES.
463 | */
464 | public static native TessAPI1.TessPageIterator TessBaseAPIAnalyseLayout(TessAPI1.TessBaseAPI handle);
465 |
466 | /**
467 | * Recognize the image from SetAndThresholdImage, generating Tesseract
468 | * internal structures. Returns 0 on success. Optional. The Get*Text
469 | * functions below will call Recognize
if needed. After Recognize, the
470 | * output is kept internally until the next SetImage
.
471 | */
472 | public static native int TessBaseAPIRecognize(TessAPI1.TessBaseAPI handle, TessAPI1.ETEXT_DESC monitor);
473 |
474 | /**
475 | * Variant on Recognize used for testing chopper.
476 | */
477 | public static native int TessBaseAPIRecognizeForChopTest(TessAPI1.TessBaseAPI handle, TessAPI1.ETEXT_DESC monitor);
478 |
479 | /**
480 | * Get a reading-order iterator to the results of LayoutAnalysis and/or
481 | * Recognize. The returned iterator must be deleted after use. WARNING! This
482 | * class points to data held within the TessBaseAPI class, and therefore can
483 | * only be used while the TessBaseAPI class still exists and has not been
484 | * subjected to a call of Init
, SetImage
, Recognize
, Clear
, End
, DetectOS, or
485 | * anything else that changes the internal PAGE_RES.
486 | */
487 | public static native TessAPI1.TessResultIterator TessBaseAPIGetIterator(TessAPI1.TessBaseAPI handle);
488 |
489 | /**
490 | * Get a mutable iterator to the results of LayoutAnalysis and/or Recognize.
491 | * The returned iterator must be deleted after use.
492 | * WARNING! This class points to data held within the TessBaseAPI class, and
493 | * therefore can only be used while the TessBaseAPI class still exists and
494 | * has not been subjected to a call of Init, SetImage, Recognize, Clear, End
495 | * DetectOS, or anything else that changes the internal PAGE_RES.
496 | */
497 | public static native TessAPI1.TessMutableIterator TessBaseAPIGetMutableIterator(TessAPI1.TessBaseAPI handle);
498 |
499 | /**
500 | * Recognizes all the pages in the named file, as a multi-page tiff or list
501 | * of filenames, or single image, and gets the appropriate kind of text
502 | * according to parameters: tessedit_create_boxfile
,
503 | * tessedit_make_boxes_from_boxes
, tessedit_write_unlv
,
504 | * tessedit_create_hocr
. Calls ProcessPage on each page in the input file,
505 | * which may be a multi-page tiff, single-page other file format, or a plain
506 | * text list of images to read. If tessedit_page_number is non-negative,
507 | * processing begins at that page of a multi-page tiff file, or filelist.
508 | * The text is returned in text_out. Returns false on error. If non-zero
509 | * timeout_millisec terminates processing after the timeout on a single
510 | * page. If non-NULL and non-empty, and some page fails for some reason, the
511 | * page is reprocessed with the retry_config config file. Useful for
512 | * interactively debugging a bad page.
513 | */
514 | public static native Pointer TessBaseAPIProcessPages(TessAPI1.TessBaseAPI handle, String filename, String retry_config, int timeout_millisec);
515 |
516 | /**
517 | * The recognized text is returned as a char* which is coded as UTF-8 and
518 | * must be freed with the delete [] operator.
519 | */
520 | public static native Pointer TessBaseAPIGetUTF8Text(TessAPI1.TessBaseAPI handle);
521 |
522 | /**
523 | * Make a HTML-formatted string with hOCR markup from the internal data
524 | * structures. page_number is 0-based but will appear in the output as
525 | * 1-based.
526 | */
527 | public static native Pointer TessBaseAPIGetHOCRText(TessAPI1.TessBaseAPI handle, int page_number);
528 |
529 | /**
530 | * The recognized text is returned as a char* which is coded in the same
531 | * format as a box file used in training. Returned string must be freed with
532 | * the delete [] operator. Constructs coordinates in the original image -
533 | * not just the rectangle. page_number is a 0-based page index that will
534 | * appear in the box file.
535 | */
536 | public static native Pointer TessBaseAPIGetBoxText(TessAPI1.TessBaseAPI handle, int page_number);
537 |
538 | /**
539 | * The recognized text is returned as a char* which is coded as UNLV format
540 | * Latin-1 with specific reject and suspect codes and must be freed with the
541 | * delete [] operator.
542 | */
543 | public static native Pointer TessBaseAPIGetUNLVText(TessAPI1.TessBaseAPI handle);
544 |
545 | /**
546 | * Returns the (average) confidence value between 0 and 100.
547 | */
548 | public static native int TessBaseAPIMeanTextConf(TessAPI1.TessBaseAPI handle);
549 |
550 | /**
551 | * Returns all word confidences (between 0 and 100) in an array, terminated
552 | * by -1. The calling function must delete [] after use. The number of
553 | * confidences should correspond to the number of space-delimited words in
554 | * GetUTF8Text.
555 | */
556 | public static native IntByReference TessBaseAPIAllWordConfidences(TessAPI1.TessBaseAPI handle);
557 |
558 | /**
559 | * Applies the given word to the adaptive classifier if possible. The word
560 | * must be SPACE-DELIMITED UTF-8 - l i k e t h i s , so it can tell the
561 | * boundaries of the graphemes. Assumes that SetImage/SetRectangle have been
562 | * used to set the image to the given word. The mode arg should be
563 | * PSM_SINGLE_WORD or PSM_CIRCLE_WORD, as that will be used to control
564 | * layout analysis. The currently set PageSegMode is preserved. Returns
565 | * false if adaption was not possible for some reason.
566 | */
567 | public static native int TessBaseAPIAdaptToWordStr(TessAPI1.TessBaseAPI handle, int mode, String wordstr);
568 |
569 | /**
570 | * Free up recognition results and any stored image data, without actually
571 | * freeing any recognition data that would be time-consuming to reload.
572 | * Afterwards, you must call SetImage
or TesseractRect
before doing any
573 | * Recognize
or Get*
operation.
574 | */
575 | public static native void TessBaseAPIClear(TessAPI1.TessBaseAPI handle);
576 |
577 | /**
578 | * Close down tesseract and free up all memory. End()
is equivalent to
579 | * destructing and reconstructing your TessBaseAPI. Once End()
has been
580 | * used, none of the other API functions may be used other than Init
and
581 | * anything declared above it in the class definition.
582 | */
583 | public static native void TessBaseAPIEnd(TessAPI1.TessBaseAPI handle);
584 |
585 | /**
586 | * Check whether a word is valid according to Tesseract's language model.
587 | *
588 | * @return 0 if the word is invalid, non-zero if valid. @warning temporary!
589 | * This function will be removed from here and placed in a separate API at
590 | * some future time.
591 | */
592 | public static native int TessBaseAPIIsValidWord(TessAPI1.TessBaseAPI handle, String word);
593 |
594 | public static native int TessBaseAPIGetTextDirection(TessAPI1.TessBaseAPI handle, IntBuffer out_offset, FloatBuffer out_slope);
595 |
596 | /**
597 | * This method returns the string form of the specified unichar.
598 | */
599 | public static native String TessBaseAPIGetUnichar(TessAPI1.TessBaseAPI handle, int unichar_id);
600 |
601 | /* Page iterator */
602 | public static native void TessPageIteratorDelete(TessAPI1.TessPageIterator handle);
603 |
604 | public static native TessAPI1.TessPageIterator TessPageIteratorCopy(TessAPI1.TessPageIterator handle);
605 |
606 | public static native void TessPageIteratorBegin(TessAPI1.TessPageIterator handle);
607 |
608 | public static native int TessPageIteratorNext(TessAPI1.TessPageIterator handle, int level);
609 |
610 | public static native int TessPageIteratorIsAtBeginningOf(TessAPI1.TessPageIterator handle, int level);
611 |
612 | public static native int TessPageIteratorIsAtFinalElement(TessAPI1.TessPageIterator handle, int level, int element);
613 |
614 | public static native int TessPageIteratorBoundingBox(TessAPI1.TessPageIterator handle, int level, IntBuffer left, IntBuffer top, IntBuffer right, IntBuffer bottom);
615 |
616 | public static native int TessPageIteratorBlockType(TessAPI1.TessPageIterator handle);
617 |
618 | public static native int TessPageIteratorBaseline(TessAPI1.TessPageIterator handle, int level, IntBuffer x1, IntBuffer y1, IntBuffer x2, IntBuffer y2);
619 |
620 | public static native void TessPageIteratorOrientation(TessAPI1.TessPageIterator handle, IntBuffer orientation, IntBuffer writing_direction, IntBuffer textline_order, FloatBuffer deskew_angle);
621 |
622 | /* Result iterator */
623 | public static native void TessResultIteratorDelete(TessAPI1.TessResultIterator handle);
624 |
625 | public static native TessAPI1.TessResultIterator TessResultIteratorCopy(TessAPI1.TessResultIterator handle);
626 |
627 | public static native TessAPI1.TessPageIterator TessResultIteratorGetPageIterator(TessAPI1.TessResultIterator handle);
628 |
629 | public static native TessAPI1.TessPageIterator TessResultIteratorGetPageIteratorConst(TessAPI1.TessResultIterator handle);
630 |
631 | public static native Pointer TessResultIteratorGetUTF8Text(TessAPI1.TessResultIterator handle, int level);
632 |
633 | public static native float TessResultIteratorConfidence(TessAPI1.TessResultIterator handle, int level);
634 |
635 | public static native String TessResultIteratorWordFontAttributes(TessAPI1.TessResultIterator handle, IntBuffer is_bold, IntBuffer is_italic, IntBuffer is_underlined, IntBuffer is_monospace, IntBuffer is_serif, IntBuffer is_smallcaps, IntBuffer pointsize, IntBuffer font_id);
636 |
637 | public static native int TessResultIteratorWordIsFromDictionary(TessAPI1.TessResultIterator handle);
638 |
639 | public static native int TessResultIteratorWordIsNumeric(TessAPI1.TessResultIterator handle);
640 |
641 | public static native int TessResultIteratorSymbolIsSuperscript(TessAPI1.TessResultIterator handle);
642 |
643 | public static native int TessResultIteratorSymbolIsSubscript(TessAPI1.TessResultIterator handle);
644 |
645 | public static native int TessResultIteratorSymbolIsDropcap(TessAPI1.TessResultIterator handle);
646 |
647 | public static class TessBaseAPI extends PointerType {
648 |
649 | public TessBaseAPI(Pointer address) {
650 | super(address);
651 | }
652 |
653 | public TessBaseAPI() {
654 | super();
655 | }
656 | };
657 |
658 | public static class ETEXT_DESC extends PointerType {
659 |
660 | public ETEXT_DESC(Pointer address) {
661 | super(address);
662 | }
663 |
664 | public ETEXT_DESC() {
665 | super();
666 | }
667 | };
668 |
669 | public static class TessPageIterator extends PointerType {
670 |
671 | public TessPageIterator(Pointer address) {
672 | super(address);
673 | }
674 |
675 | public TessPageIterator() {
676 | super();
677 | }
678 | };
679 |
680 | public static class TessMutableIterator extends PointerType {
681 |
682 | public TessMutableIterator(Pointer address) {
683 | super(address);
684 | }
685 |
686 | public TessMutableIterator() {
687 | super();
688 | }
689 | };
690 |
691 | public static class TessResultIterator extends PointerType {
692 |
693 | public TessResultIterator(Pointer address) {
694 | super(address);
695 | }
696 |
697 | public TessResultIterator() {
698 | super();
699 | }
700 | };
701 | }
702 |
--------------------------------------------------------------------------------
/src/net/sourceforge/tess4j/Tesseract.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright @ 2012 Quan Nguyen
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 | * use this file except in compliance with the License. You may obtain a copy of
6 | * the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 | * License for the specific language governing permissions and limitations under
14 | * the License.
15 | */
16 | package net.sourceforge.tess4j;
17 |
18 | import net.sourceforge.vietocr.ImageIOHelper;
19 | import com.sun.jna.Pointer;
20 | import java.awt.Rectangle;
21 | import java.awt.image.*;
22 | import java.io.*;
23 | import java.nio.ByteBuffer;
24 | import java.util.*;
25 | import java.util.logging.*;
26 | import javax.imageio.IIOImage;
27 |
28 | /**
29 | * An object layer on top of
30 | * TessAPI
, provides character recognition support for common image
31 | * formats, and multi-page TIFF images beyond the uncompressed, binary TIFF
32 | * format supported by Tesseract OCR engine. The extended capabilities are
33 | * provided by the
34 | * Java Advanced Imaging Image I/O Tools
. Ghost4J
, a
37 | * JNA
wrapper for
38 | * GPL Ghostscript
, which should be installed and included in
39 | * system path. .jar
files for
42 | * jna
,
43 | * jai-imageio
, and
44 | * ghost4j
) are in its compile and run-time
45 | * classpath
.
46 | */
47 | public class Tesseract implements ITesseract {
48 |
49 | private static Tesseract instance;
50 | //private String language = "eng"; //英文识别语言包
51 | private String language = "chi_sim";//中文识别语言包
52 | private String datapath = "./";
53 | private int psm = TessAPI.TessPageSegMode.PSM_AUTO;
54 | private boolean hocr;
55 | private int pageNum;
56 | private int ocrEngineMode = TessAPI.TessOcrEngineMode.OEM_DEFAULT;
57 | private final Properties prop = new Properties();
58 |
59 | private TessAPI api;
60 | private TessAPI.TessBaseAPI handle;
61 |
62 | private final static Logger logger = Logger.getLogger(Tesseract.class.getName());
63 |
64 | /**
65 | * Private constructor.
66 | */
67 | public Tesseract() {
68 | System.setProperty("jna.encoding", "UTF8");
69 | }
70 |
71 | /**
72 | * Gets an instance of the class library.
73 | *
74 | * @return instance
75 | */
76 | public static synchronized Tesseract getInstance() {
77 | if (instance == null) {
78 | instance = new Tesseract();
79 | }
80 |
81 | return instance;
82 | }
83 |
84 | /**
85 | * Sets tessdata path.
86 | *
87 | * @param datapath the tessdata path to set
88 | */
89 | public void setDatapath(String datapath) {
90 | this.datapath = datapath;
91 | }
92 |
93 | /**
94 | * Sets language for OCR.
95 | *
96 | * @param language the language code, which follows ISO 639-3 standard.
97 | */
98 | public void setLanguage(String language) {
99 | this.language = language;
100 | }
101 |
102 | /**
103 | * Sets OCR engine mode.
104 | *
105 | * @param ocrEngineMode the OcrEngineMode to set
106 | */
107 | public void setOcrEngineMode(int ocrEngineMode) {
108 | this.ocrEngineMode = ocrEngineMode;
109 | }
110 |
111 | /**
112 | * Sets page segmentation mode.
113 | *
114 | * @param mode the page segmentation mode to set
115 | */
116 | public void setPageSegMode(int mode) {
117 | this.psm = mode;
118 | }
119 |
120 | /**
121 | * Enables hocr output.
122 | *
123 | * @param hocr to enable or disable hocr output
124 | */
125 | public void setHocr(boolean hocr) {
126 | this.hocr = hocr;
127 | prop.setProperty("tessedit_create_hocr", hocr ? "1" : "0");
128 | }
129 |
130 | /**
131 | * Set the value of Tesseract's internal parameter.
132 | *
133 | * @param key variable name, e.g.,
134 | * tessedit_create_hocr
,
135 | * tessedit_char_whitelist
, etc.
136 | * @param value value for corresponding variable, e.g., "1", "0",
137 | * "0123456789", etc.
138 | */
139 | public void setTessVariable(String key, String value) {
140 | prop.setProperty(key, value);
141 | }
142 |
143 | /**
144 | * Performs OCR operation.
145 | *
146 | * @param imageFile an image file
147 | * @return the recognized text
148 | * @throws TesseractException
149 | */
150 | public String doOCR(File imageFile) throws TesseractException {
151 | return doOCR(imageFile, null);
152 | }
153 |
154 | /**
155 | * Performs OCR operation.
156 | *
157 | * @param imageFile an image file
158 | * @param rect the bounding rectangle defines the region of the image to be
159 | * recognized. A rectangle of zero dimension or
160 | * null
indicates the whole image.
161 | * @return the recognized text
162 | * @throws TesseractException
163 | */
164 | public String doOCR(File imageFile, Rectangle rect) throws TesseractException {
165 | try {
166 | return doOCR(ImageIOHelper.getIIOImageList(imageFile), rect);
167 | } catch (Exception e) {
168 | logger.log(Level.SEVERE, e.getMessage(), e);
169 | throw new TesseractException(e);
170 | }
171 | }
172 |
173 | /**
174 | * Performs OCR operation.
175 | *
176 | * @param bi a buffered image
177 | * @return the recognized text
178 | * @throws TesseractException
179 | */
180 | public String doOCR(BufferedImage bi) throws TesseractException {
181 | return doOCR(bi, null);
182 | }
183 |
184 | /**
185 | * Performs OCR operation.
186 | *
187 | * @param bi a buffered image
188 | * @param rect the bounding rectangle defines the region of the image to be
189 | * recognized. A rectangle of zero dimension or
190 | * null
indicates the whole image.
191 | * @return the recognized text
192 | * @throws TesseractException
193 | */
194 | public String doOCR(BufferedImage bi, Rectangle rect) throws TesseractException {
195 | try {
196 | return doOCR(ImageIOHelper.getIIOImageList(bi), rect);
197 | } catch (Exception e) {
198 | logger.log(Level.SEVERE, e.getMessage(), e);
199 | throw new TesseractException(e);
200 | }
201 | }
202 |
203 | /**
204 | * Performs OCR operation.
205 | *
206 | * @param imageList a list of
207 | * IIOImage
objects
208 | * @param rect the bounding rectangle defines the region of the image to be
209 | * recognized. A rectangle of zero dimension or
210 | * null
indicates the whole image.
211 | * @return the recognized text
212 | * @throws TesseractException
213 | */
214 | public String doOCR(ListSetImage
, (optionally)
245 | * SetRectangle
, and one or more of the
246 | * Get*Text
functions.
247 | *
248 | * @param xsize width of image
249 | * @param ysize height of image
250 | * @param buf pixel data
251 | * @param rect the bounding rectangle defines the region of the image to be
252 | * recognized. A rectangle of zero dimension or
253 | * null
indicates the whole image.
254 | * @param bpp bits per pixel, represents the bit depth of the image, with 1
255 | * for binary bitmap, 8 for gray, and 24 for color RGB.
256 | * @return the recognized text
257 | * @throws TesseractException
258 | */
259 | public String doOCR(int xsize, int ysize, ByteBuffer buf, Rectangle rect, int bpp) throws TesseractException {
260 | init();
261 | setTessVariables();
262 |
263 | try {
264 | setImage(xsize, ysize, buf, rect, bpp);
265 | return getOCRText();
266 | } catch (Exception e) {
267 | logger.log(Level.SEVERE, e.getMessage(), e);
268 | throw new TesseractException(e);
269 | } finally {
270 | dispose();
271 | }
272 | }
273 |
274 | /**
275 | * Initializes Tesseract engine.
276 | */
277 | private void init() {
278 | pageNum = 0;
279 | api = TessAPI.INSTANCE;
280 | handle = api.TessBaseAPICreate();
281 | api.TessBaseAPIInit2(handle, datapath, language, ocrEngineMode);
282 | api.TessBaseAPISetPageSegMode(handle, psm);
283 | }
284 |
285 | /**
286 | * Sets Tesseract's internal parameters.
287 | */
288 | private void setTessVariables() {
289 | Enumeration> em = prop.propertyNames();
290 | while (em.hasMoreElements()) {
291 | String key = (String) em.nextElement();
292 | api.TessBaseAPISetVariable(handle, key, prop.getProperty(key));
293 | }
294 | }
295 |
296 | /**
297 | * A wrapper for {@link #setImage(int, int, ByteBuffer, Rectangle, int)}.
298 | */
299 | private void setImage(RenderedImage image, Rectangle rect) throws IOException {
300 | setImage(image.getWidth(), image.getHeight(), ImageIOHelper.getImageByteBuffer(image), rect, image.getColorModel().getPixelSize());
301 | }
302 |
303 | /**
304 | * Sets image to be processed.
305 | *
306 | * @param xsize width of image
307 | * @param ysize height of image
308 | * @param buf pixel data
309 | * @param rect the bounding rectangle defines the region of the image to be
310 | * recognized. A rectangle of zero dimension or
311 | * null
indicates the whole image.
312 | * @param bpp bits per pixel, represents the bit depth of the image, with 1
313 | * for binary bitmap, 8 for gray, and 24 for color RGB.
314 | */
315 | private void setImage(int xsize, int ysize, ByteBuffer buf, Rectangle rect, int bpp) {
316 | int bytespp = bpp / 8;
317 | int bytespl = (int) Math.ceil(xsize * bpp / 8.0);
318 | api.TessBaseAPISetImage(handle, buf, xsize, ysize, bytespp, bytespl);
319 |
320 | if (rect != null && !rect.isEmpty()) {
321 | api.TessBaseAPISetRectangle(handle, rect.x, rect.y, rect.width, rect.height);
322 | }
323 | }
324 |
325 | /**
326 | * Gets recognized text.
327 | *
328 | * @return the recognized text
329 | */
330 | private String getOCRText() {
331 | Pointer utf8Text = hocr ? api.TessBaseAPIGetHOCRText(handle, pageNum - 1) : api.TessBaseAPIGetUTF8Text(handle);
332 | String str = utf8Text.getString(0);
333 | api.TessDeleteText(utf8Text);
334 | return str;
335 | }
336 |
337 | /**
338 | * Releases all of the native resources used by this instance.
339 | */
340 | private void dispose() {
341 | api.TessBaseAPIDelete(handle);
342 | }
343 | }
344 |
--------------------------------------------------------------------------------
/src/net/sourceforge/tess4j/Tesseract1.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright @ 2012 Quan Nguyen
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 | * use this file except in compliance with the License. You may obtain a copy of
6 | * the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 | * License for the specific language governing permissions and limitations under
14 | * the License.
15 | */
16 | package net.sourceforge.tess4j;
17 |
18 | import net.sourceforge.vietocr.ImageIOHelper;
19 | import com.sun.jna.Pointer;
20 | import java.awt.Rectangle;
21 | import java.awt.image.*;
22 | import java.io.*;
23 | import java.nio.ByteBuffer;
24 | import java.util.*;
25 | import java.util.logging.*;
26 | import javax.imageio.IIOImage;
27 |
28 | /**
29 | * An object layer on top of
30 | * TessAPI1
, provides character recognition support for common
31 | * image formats, and multi-page TIFF images beyond the uncompressed, binary
32 | * TIFF format supported by Tesseract OCR engine. The extended capabilities are
33 | * provided by the
34 | * Java Advanced Imaging Image I/O Tools
. Ghost4J
, a
37 | * JNA
wrapper for
38 | * GPL Ghostscript
, which should be installed and included in
39 | * system path. .jar
files for
42 | * jna
,
43 | * jai-imageio
, and
44 | * ghost4j
) are in its compile and run-time
45 | * classpath
.
46 | */
47 | public class Tesseract1 extends TessAPI1 implements ITesseract {
48 |
49 | private String language = "eng";
50 | private String datapath = "./";
51 | private int psm = TessAPI1.TessPageSegMode.PSM_AUTO;
52 | private boolean hocr;
53 | private int pageNum;
54 | private int ocrEngineMode = TessAPI1.TessOcrEngineMode.OEM_DEFAULT;
55 | private final Properties prop = new Properties();
56 |
57 | private TessBaseAPI handle;
58 |
59 | private final static Logger logger = Logger.getLogger(Tesseract1.class.getName());
60 |
61 | /**
62 | * Public constructor.
63 | */
64 | public Tesseract1() {
65 | System.setProperty("jna.encoding", "UTF8");
66 | }
67 |
68 | /**
69 | * @param datapath the tessdata path to set
70 | */
71 | public void setDatapath(String datapath) {
72 | this.datapath = datapath;
73 | }
74 |
75 | /**
76 | * Sets language for OCR.
77 | *
78 | * @param language the language code, which follows ISO 639-3 standard.
79 | */
80 | public void setLanguage(String language) {
81 | this.language = language;
82 | }
83 |
84 | /**
85 | * Sets OCR engine mode.
86 | *
87 | * @param ocrEngineMode the OcrEngineMode to set
88 | */
89 | public void setOcrEngineMode(int ocrEngineMode) {
90 | this.ocrEngineMode = ocrEngineMode;
91 | }
92 |
93 | /**
94 | * @param mode the page segmentation mode to set
95 | */
96 | public void setPageSegMode(int mode) {
97 | this.psm = mode;
98 | }
99 |
100 | /**
101 | * Enables hocr output.
102 | *
103 | * @param hocr to enable or disable hocr output
104 | */
105 | public void setHocr(boolean hocr) {
106 | this.hocr = hocr;
107 | prop.setProperty("tessedit_create_hocr", hocr ? "1" : "0");
108 | }
109 |
110 | /**
111 | * Set the value of Tesseract's internal parameter.
112 | *
113 | * @param key variable name, e.g.,
114 | * tessedit_create_hocr
,
115 | * tessedit_char_whitelist
, etc.
116 | * @param value value for corresponding variable, e.g., "1", "0",
117 | * "0123456789", etc.
118 | */
119 | public void setTessVariable(String key, String value) {
120 | prop.setProperty(key, value);
121 | }
122 |
123 | /**
124 | * Returns API handle.
125 | */
126 | protected TessBaseAPI getHandle() {
127 | return handle;
128 | }
129 |
130 | /**
131 | * Performs OCR operation.
132 | *
133 | * @param imageFile an image file
134 | * @return the recognized text
135 | * @throws TesseractException
136 | */
137 | public String doOCR(File imageFile) throws TesseractException {
138 | return doOCR(imageFile, null);
139 | }
140 |
141 | /**
142 | * Performs OCR operation.
143 | *
144 | * @param imageFile an image file
145 | * @param rect the bounding rectangle defines the region of the image to be
146 | * recognized. A rectangle of zero dimension or
147 | * null
indicates the whole image.
148 | * @return the recognized text
149 | * @throws TesseractException
150 | */
151 | public String doOCR(File imageFile, Rectangle rect) throws TesseractException {
152 | try {
153 | return doOCR(ImageIOHelper.getIIOImageList(imageFile), rect);
154 | } catch (Exception e) {
155 | logger.log(Level.SEVERE, e.getMessage(), e);
156 | throw new TesseractException(e);
157 | }
158 | }
159 |
160 | /**
161 | * Performs OCR operation.
162 | *
163 | * @param bi a buffered image
164 | * @return the recognized text
165 | * @throws TesseractException
166 | */
167 | public String doOCR(BufferedImage bi) throws TesseractException {
168 | return doOCR(bi, null);
169 | }
170 |
171 | /**
172 | * Performs OCR operation.
173 | *
174 | * @param bi a buffered image
175 | * @param rect the bounding rectangle defines the region of the image to be
176 | * recognized. A rectangle of zero dimension or
177 | * null
indicates the whole image.
178 | * @return the recognized text
179 | * @throws TesseractException
180 | */
181 | public String doOCR(BufferedImage bi, Rectangle rect) throws TesseractException {
182 | try {
183 | return doOCR(ImageIOHelper.getIIOImageList(bi), rect);
184 | } catch (Exception e) {
185 | logger.log(Level.SEVERE, e.getMessage(), e);
186 | throw new TesseractException(e);
187 | }
188 | }
189 |
190 | /**
191 | * Performs OCR operation.
192 | *
193 | * @param imageList a list of
194 | * IIOImage
objects
195 | * @param rect the bounding rectangle defines the region of the image to be
196 | * recognized. A rectangle of zero dimension or
197 | * null
indicates the whole image.
198 | * @return the recognized text
199 | * @throws TesseractException
200 | */
201 | public String doOCR(ListSetImage
, (optionally)
232 | * SetRectangle
, and one or more of the
233 | * Get*Text
functions.
234 | *
235 | * @param xsize width of image
236 | * @param ysize height of image
237 | * @param buf pixel data
238 | * @param rect the bounding rectangle defines the region of the image to be
239 | * recognized. A rectangle of zero dimension or
240 | * null
indicates the whole image.
241 | * @param bpp bits per pixel, represents the bit depth of the image, with 1
242 | * for binary bitmap, 8 for gray, and 24 for color RGB.
243 | * @return the recognized text
244 | * @throws TesseractException
245 | */
246 | public String doOCR(int xsize, int ysize, ByteBuffer buf, Rectangle rect, int bpp) throws TesseractException {
247 | init();
248 | setTessVariables();
249 |
250 | try {
251 | setImage(xsize, ysize, buf, rect, bpp);
252 | return getOCRText();
253 | } catch (Exception e) {
254 | logger.log(Level.SEVERE, e.getMessage(), e);
255 | throw new TesseractException(e);
256 | } finally {
257 | dispose();
258 | }
259 | }
260 |
261 | /**
262 | * Initializes Tesseract engine.
263 | */
264 | protected void init() {
265 | pageNum = 0;
266 | handle = TessBaseAPICreate();
267 | TessBaseAPIInit2(handle, datapath, language, ocrEngineMode);
268 | TessBaseAPISetPageSegMode(handle, psm);
269 | }
270 |
271 | /**
272 | * Sets Tesseract's internal parameters.
273 | */
274 | protected void setTessVariables() {
275 | Enumeration> em = prop.propertyNames();
276 | while (em.hasMoreElements()) {
277 | String key = (String) em.nextElement();
278 | TessBaseAPISetVariable(handle, key, prop.getProperty(key));
279 | }
280 | }
281 |
282 | /**
283 | * A wrapper for {@link #setImage(int, int, ByteBuffer, Rectangle, int)}.
284 | */
285 | protected void setImage(RenderedImage image, Rectangle rect) throws IOException {
286 | setImage(image.getWidth(), image.getHeight(), ImageIOHelper.getImageByteBuffer(image), rect, image.getColorModel().getPixelSize());
287 | }
288 |
289 | /**
290 | * Sets image to be processed.
291 | *
292 | * @param xsize width of image
293 | * @param ysize height of image
294 | * @param buf pixel data
295 | * @param rect the bounding rectangle defines the region of the image to be
296 | * recognized. A rectangle of zero dimension or
297 | * null
indicates the whole image.
298 | * @param bpp bits per pixel, represents the bit depth of the image, with 1
299 | * for binary bitmap, 8 for gray, and 24 for color RGB.
300 | */
301 | protected void setImage(int xsize, int ysize, ByteBuffer buf, Rectangle rect, int bpp) {
302 | int bytespp = bpp / 8;
303 | int bytespl = (int) Math.ceil(xsize * bpp / 8.0);
304 | TessBaseAPISetImage(handle, buf, xsize, ysize, bytespp, bytespl);
305 |
306 | if (rect != null && !rect.isEmpty()) {
307 | TessBaseAPISetRectangle(handle, rect.x, rect.y, rect.width, rect.height);
308 | }
309 | }
310 |
311 | /**
312 | * Gets recognized text.
313 | *
314 | * @return the recognized text
315 | */
316 | protected String getOCRText() {
317 | Pointer utf8Text = hocr ? TessBaseAPIGetHOCRText(handle, pageNum - 1) : TessBaseAPIGetUTF8Text(handle);
318 | String str = utf8Text.getString(0);
319 | TessDeleteText(utf8Text);
320 | return str;
321 | }
322 |
323 | /**
324 | * Releases all of the native resources used by this instance.
325 | */
326 | protected void dispose() {
327 | TessBaseAPIDelete(handle);
328 | }
329 | }
330 |
--------------------------------------------------------------------------------
/src/net/sourceforge/tess4j/TesseractException.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright @ 2010 Quan Nguyen
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package net.sourceforge.tess4j;
18 |
19 | public class TesseractException extends Exception {
20 |
21 | public TesseractException() {
22 | super();
23 | }
24 |
25 | public TesseractException(String message) {
26 | super(message);
27 | }
28 |
29 | public TesseractException(Throwable cause) {
30 | super(cause);
31 | }
32 |
33 | public TesseractException(String message, Throwable cause) {
34 | super(message, cause);
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/src/net/sourceforge/tess4j/util/Utils.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright @ 2013 Quan Nguyen
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 | * use this file except in compliance with the License. You may obtain a copy of
6 | * the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 | * License for the specific language governing permissions and limitations under
14 | * the License.
15 | */
16 | package net.sourceforge.tess4j.util;
17 |
18 | import java.lang.reflect.Field;
19 | import java.lang.reflect.Modifier;
20 |
21 | public class Utils {
22 |
23 | /**
24 | * Gets user-friendly name of the public static final constant defined in a class or an
25 | * interface for display purpose.
26 | *
27 | * @param value the constant value
28 | * @param c type of class or interface
29 | * @return name
30 | */
31 | public static String getConstantName(Object value, Class c) {
32 | for (Field f : c.getDeclaredFields()) {
33 | int mod = f.getModifiers();
34 | if (Modifier.isStatic(mod) && Modifier.isPublic(mod) && Modifier.isFinal(mod)) {
35 | try {
36 | if (f.get(null).equals(value)) {
37 | return f.getName();
38 | }
39 | } catch (IllegalAccessException e) {
40 | return String.valueOf(value);
41 | }
42 | }
43 | }
44 | return String.valueOf(value);
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/src/net/sourceforge/vietocr/ImageHelper.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright @ 2008 Quan Nguyen
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 | * use this file except in compliance with the License. You may obtain a copy of
6 | * the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 | * License for the specific language governing permissions and limitations under
14 | * the License.
15 | */
16 | package net.sourceforge.vietocr;
17 |
18 | import java.awt.Graphics2D;
19 | import java.awt.Image;
20 | import java.awt.RenderingHints;
21 | import java.awt.Toolkit;
22 | import java.awt.Transparency;
23 | import java.awt.datatransfer.Clipboard;
24 | import java.awt.datatransfer.DataFlavor;
25 | import java.awt.image.*;
26 |
27 | public class ImageHelper {
28 |
29 | /**
30 | * Convenience method that returns a scaled instance of the provided
31 | * {@code BufferedImage}.
32 | *
33 | * @param image the original image to be scaled
34 | * @param targetWidth the desired width of the scaled instance, in pixels
35 | * @param targetHeight the desired height of the scaled instance, in pixels
36 | * @return a scaled version of the original {@code BufferedImage}
37 | */
38 | public static BufferedImage getScaledInstance(BufferedImage image, int targetWidth, int targetHeight) {
39 | int type = (image.getTransparency() == Transparency.OPAQUE)
40 | ? BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB;
41 | BufferedImage tmp = new BufferedImage(targetWidth, targetHeight, type);
42 | Graphics2D g2 = tmp.createGraphics();
43 | g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
44 | g2.drawImage(image, 0, 0, targetWidth, targetHeight, null);
45 | g2.dispose();
46 | return tmp;
47 | }
48 |
49 | /**
50 | * A replacement for the standard
51 | * BufferedImage.getSubimage
method.
52 | *
53 | * @param image
54 | * @param x the X coordinate of the upper-left corner of the specified
55 | * rectangular region
56 | * @param y the Y coordinate of the upper-left corner of the specified
57 | * rectangular region
58 | * @param width the width of the specified rectangular region
59 | * @param height the height of the specified rectangular region
60 | * @return a BufferedImage that is the subimage of image
.
61 | */
62 | public static BufferedImage getSubImage(BufferedImage image, int x, int y, int width, int height) {
63 | int type = (image.getTransparency() == Transparency.OPAQUE)
64 | ? BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB;
65 | BufferedImage tmp = new BufferedImage(width, height, type);
66 | Graphics2D g2 = tmp.createGraphics();
67 | g2.drawImage(image.getSubimage(x, y, width, height), 0, 0, null);
68 | g2.dispose();
69 | return tmp;
70 | }
71 |
72 | /**
73 | * A simple method to convert an image to binary or B/W image.
74 | *
75 | * @param image input image
76 | * @return a monochrome image
77 | */
78 | public static BufferedImage convertImageToBinary(BufferedImage image) {
79 | BufferedImage tmp = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_BYTE_BINARY);
80 | Graphics2D g2 = tmp.createGraphics();
81 | g2.drawImage(image, 0, 0, null);
82 | g2.dispose();
83 | return tmp;
84 | }
85 |
86 | /**
87 | * A simple method to convert an image to binary or B/W image.
88 | *
89 | * @param image input image
90 | * @return a monochrome image
91 | * @deprecated As of release 1.1, renamed to {@link #convertImageToBinary(BufferedImage image)}
92 | */
93 | @Deprecated
94 | public static BufferedImage convertImage2Binary(BufferedImage image) {
95 | return convertImageToBinary(image);
96 | }
97 |
98 | /**
99 | * A simple method to convert an image to gray scale.
100 | *
101 | * @param image input image
102 | * @return a monochrome image
103 | */
104 | public static BufferedImage convertImageToGrayscale(BufferedImage image) {
105 | BufferedImage tmp = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
106 | Graphics2D g2 = tmp.createGraphics();
107 | g2.drawImage(image, 0, 0, null);
108 | g2.dispose();
109 | return tmp;
110 | }
111 |
112 | private static final short[] invertTable;
113 |
114 | static {
115 | invertTable = new short[256];
116 | for (int i = 0; i < 256; i++) {
117 | invertTable[i] = (short) (255 - i);
118 | }
119 | }
120 |
121 | /**
122 | * Inverts image color.
123 | *
124 | * @param image input image
125 | * @return an inverted-color image
126 | */
127 | public static BufferedImage invertImageColor(BufferedImage image) {
128 | BufferedImage tmp = new BufferedImage(image.getWidth(), image.getHeight(), image.getType());
129 | BufferedImageOp invertOp = new LookupOp(new ShortLookupTable(0, invertTable), null);
130 | return invertOp.filter(image, tmp);
131 | }
132 |
133 | /**
134 | * Rotates an image.
135 | *
136 | * @param image the original image
137 | * @param angle the degree of rotation
138 | * @return a rotated image
139 | */
140 | public static BufferedImage rotateImage(BufferedImage image, double angle) {
141 | double theta = Math.toRadians(angle);
142 | double sin = Math.abs(Math.sin(theta));
143 | double cos = Math.abs(Math.cos(theta));
144 | int w = image.getWidth();
145 | int h = image.getHeight();
146 | int newW = (int) Math.floor(w * cos + h * sin);
147 | int newH = (int) Math.floor(h * cos + w * sin);
148 |
149 | BufferedImage tmp = new BufferedImage(newW, newH, image.getType());
150 | Graphics2D g2d = tmp.createGraphics();
151 | g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
152 | RenderingHints.VALUE_INTERPOLATION_BICUBIC);
153 | g2d.translate((newW - w) / 2, (newH - h) / 2);
154 | g2d.rotate(theta, w / 2, h / 2);
155 | g2d.drawImage(image, 0, 0, null);
156 | g2d.dispose();
157 | return tmp;
158 | }
159 |
160 | /**
161 | * Gets an image from Clipboard.
162 | *
163 | * @return image
164 | */
165 | public static Image getClipboardImage() {
166 | Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
167 | try {
168 | return (Image) clipboard.getData(DataFlavor.imageFlavor);
169 | } catch (Exception e) {
170 | return null;
171 | }
172 | }
173 | }
174 |
--------------------------------------------------------------------------------
/src/net/sourceforge/vietocr/ImageIOHelper.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright @ 2008 Quan Nguyen
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 | * use this file except in compliance with the License. You may obtain a copy of
6 | * the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 | * License for the specific language governing permissions and limitations under
14 | * the License.
15 | */
16 | package net.sourceforge.vietocr;
17 |
18 | import java.io.*;
19 | import java.util.*;
20 | import javax.imageio.*;
21 | import javax.imageio.stream.*;
22 | import javax.imageio.metadata.*;
23 | import com.sun.media.imageio.plugins.tiff.*;
24 | import java.awt.Toolkit;
25 | import java.awt.image.*;
26 | import java.nio.ByteBuffer;
27 | import java.nio.ByteOrder;
28 | import org.w3c.dom.NodeList;
29 |
30 | public class ImageIOHelper {
31 |
32 | final static String OUTPUT_FILE_NAME = "Tesstmp";
33 | final static String TIFF_EXT = ".tif";
34 | final static String TIFF_FORMAT = "tiff";
35 | final static String JAI_IMAGE_WRITER_MESSAGE = "Need to install JAI Image I/O package.\nhttps://java.net/projects/jai-imageio/";
36 | final static String JAI_IMAGE_READER_MESSAGE = "Unsupported image format. May need to install JAI Image I/O package.\nhttps://java.net/projects/jai-imageio/";
37 |
38 | /**
39 | * Creates a list of TIFF image files from an image file. It basically
40 | * converts images of other formats to TIFF format, or a multi-page TIFF
41 | * image to multiple TIFF image files.
42 | *
43 | * @param imageFile input image file
44 | * @param index an index of the page; -1 means all pages, as in a multi-page
45 | * TIFF image
46 | * @return a list of TIFF image files
47 | * @throws Exception
48 | */
49 | public static ListIIOImage
objects.
109 | *
110 | * @param imageList a list of IIOImage
objects
111 | * @param index an index of the page; -1 means all pages
112 | * @return a list of TIFF image files
113 | * @throws Exception
114 | */
115 | public static ListIIOImage
object.
189 | *
190 | * @param image an IIOImage
object
191 | * @return a byte buffer of pixel data
192 | * @throws IOException
193 | */
194 | public static ByteBuffer getImageByteBuffer(IIOImage image) throws IOException {
195 | return getImageByteBuffer(image.getRenderedImage());
196 | }
197 |
198 | /**
199 | * Gets pixel data of an RenderedImage
object.
200 | * @param image an RenderedImage
object
201 | * @return a byte buffer of pixel data
202 | * @throws IOException
203 | */
204 | public static ByteBuffer getImageByteBuffer(RenderedImage image) throws IOException {
205 | //Set up the writeParam
206 | TIFFImageWriteParam tiffWriteParam = new TIFFImageWriteParam(Locale.US);
207 | tiffWriteParam.setCompressionMode(ImageWriteParam.MODE_DISABLED);
208 |
209 | //Get tif writer and set output to file
210 | IteratorBufferedImage
to ByteBuffer
.
235 | *
236 | * @param bi Input image
237 | * @return pixel data
238 | */
239 | public static ByteBuffer convertImageData(BufferedImage bi) {
240 | DataBuffer buff = bi.getRaster().getDataBuffer();
241 | // ClassCastException thrown if buff not instanceof DataBufferByte because raster data is not necessarily bytes.
242 | // Convert the original buffered image to grayscale.
243 | if (!(buff instanceof DataBufferByte)) {
244 | bi = ImageHelper.convertImageToGrayscale(bi);
245 | buff = bi.getRaster().getDataBuffer();
246 | }
247 | byte[] pixelData = ((DataBufferByte) buff).getData();
248 | // return ByteBuffer.wrap(pixelData);
249 | ByteBuffer buf = ByteBuffer.allocateDirect(pixelData.length);
250 | buf.order(ByteOrder.nativeOrder());
251 | buf.put(pixelData);
252 | buf.flip();
253 | return buf;
254 | }
255 |
256 | /**
257 | * Gets a list of IIOImage
objects for an image file.
258 | *
259 | * @param imageFile input image file. It can be any of the supported
260 | * formats, including TIFF, JPEG, GIF, PNG, BMP, JPEG, and PDF if GPL
261 | * Ghostscript is installed
262 | * @return a list of IIOImage
objects
263 | * @throws Exception
264 | */
265 | public static ListIIOImage
objects for a BufferedImage
.
325 | * @param bi input image
326 | * @return a list of IIOImage
objects
327 | * @throws IOException
328 | */
329 | public static List