JavaCPP platform aggregator for Tesseract OCR native libraries providing cross-platform OCR capabilities in Java applications
—
Comprehensive configuration system with hundreds of parameters controlling OCR behavior, page segmentation, character recognition, and output formatting. Tesseract provides fine-grained control over recognition algorithms and processing options.
Set and retrieve configuration parameters that control Tesseract's behavior during initialization and recognition.
public class TessBaseAPI {
// Parameter setting
public boolean SetVariable(String name, String value);
public boolean SetDebugVariable(String name, String value);
// Parameter retrieval
public boolean GetIntVariable(String name, int[] value);
public boolean GetBoolVariable(String name, boolean[] value);
public boolean GetDoubleVariable(String name, double[] value);
public String GetStringVariable(String name);
// Generic parameter access
public native @Cast("bool") boolean GetVariableAsString(String name, @StdString @Cast({"char*", "std::string*"}) BytePointer val);
// Parameter introspection
public void PrintVariables(Pointer fp);
}Parameter Types:
TessBaseAPI api = new TessBaseAPI();
api.Init(null, "eng");
// Set character blacklist (ignore these characters)
api.SetVariable("tessedit_char_blacklist", "xyz@#$");
// Enable numeric-only mode
api.SetVariable("classify_bln_numeric_mode", "1");
// Set minimum word length
api.SetVariable("textord_min_linesize", "2.5");
// Enable debug output
api.SetDebugVariable("textord_debug_tabfind", "1");
// Check if parameter exists and get value
int[] min_chars = new int[1];
if (api.GetIntVariable("textord_min_chars_in_word", min_chars)) {
System.out.println("Minimum characters per word: " + min_chars[0]);
}
// Get string parameter
String lang = api.GetStringVariable("tessedit_init_config_only");
System.out.println("Config only mode: " + lang);Control which OCR engine and neural network models are used for recognition.
// OCR Engine Mode constants
public static final int OEM_TESSERACT_ONLY = 0; // Legacy Tesseract (deprecated)
public static final int OEM_LSTM_ONLY = 1; // LSTM neural network only
public static final int OEM_TESSERACT_LSTM_COMBINED = 2; // Combined legacy + LSTM (deprecated)
public static final int OEM_DEFAULT = 3; // Default (currently LSTM only)TessBaseAPI api = new TessBaseAPI();
// Use LSTM-only engine (recommended)
int result = api.Init(null, "eng", OEM_LSTM_ONLY);
// Or use default engine mode
int result2 = api.Init(null, "eng", OEM_DEFAULT);
// Check which engine is active
String engine_mode = api.GetStringVariable("tessedit_ocr_engine_mode");
System.out.println("Active OCR engine: " + engine_mode);Configure how Tesseract analyzes page layout and identifies text regions.
public class TessBaseAPI {
// Page segmentation mode control
public void SetPageSegMode(int mode);
public int GetPageSegMode();
}
// Page segmentation mode constants (detailed)
public static final int PSM_OSD_ONLY = 0; // Orientation/script detection only
public static final int PSM_AUTO_OSD = 1; // Automatic with OSD
public static final int PSM_AUTO_ONLY = 2; // Automatic without OSD
public static final int PSM_AUTO = 3; // Fully automatic (default)
public static final int PSM_SINGLE_COLUMN = 4; // Single column text
public static final int PSM_SINGLE_BLOCK_VERT_TEXT = 5; // Single vertical block
public static final int PSM_SINGLE_BLOCK = 6; // Single uniform block
public static final int PSM_SINGLE_LINE = 7; // Single text line
public static final int PSM_SINGLE_WORD = 8; // Single word
public static final int PSM_CIRCLE_WORD = 9; // Single word in circle
public static final int PSM_SINGLE_CHAR = 10; // Single character
public static final int PSM_SPARSE_TEXT = 11; // Sparse text find
public static final int PSM_SPARSE_TEXT_OSD = 12; // Sparse text with OSD
public static final int PSM_RAW_LINE = 13; // Raw line (bypass hacks)TessBaseAPI api = new TessBaseAPI();
api.Init(null, "eng");
// Configure for different document types
switch (documentType) {
case NEWSPAPER:
api.SetPageSegMode(PSM_AUTO); // Multi-column with auto detection
break;
case BUSINESS_CARD:
api.SetPageSegMode(PSM_SINGLE_BLOCK); // Simple layout
break;
case LICENSE_PLATE:
api.SetPageSegMode(PSM_SINGLE_LINE); // Single line of text
break;
case CAPTCHA:
api.SetPageSegMode(PSM_SINGLE_WORD); // Single word
break;
case RECEIPT:
api.SetPageSegMode(PSM_SINGLE_COLUMN); // Vertical list
break;
}
// Verify current mode
int currentMode = api.GetPageSegMode();
System.out.println("Page segmentation mode: " + currentMode);Fine-tune character recognition algorithms and thresholds.
// Character filtering and validation
api.SetVariable("tessedit_char_whitelist", "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ");
api.SetVariable("tessedit_char_blacklist", "!@#$%^&*()");
// Numeric mode for numbers-only recognition
api.SetVariable("classify_bln_numeric_mode", "1");
// Case sensitivity
api.SetVariable("unicharset_use_script_ider", "1");
// Character segmentation
api.SetVariable("chop_enable", "1"); // Enable character chopping
api.SetVariable("use_new_state_cost", "1"); // Use improved state cost
api.SetVariable("segment_segcost_rating", "1"); // Enable segmentation cost rating
// Word recognition
api.SetVariable("save_best_choices", "1"); // Save alternative choices
api.SetVariable("language_model_penalty_non_dict_word", "0.15");
api.SetVariable("language_model_penalty_non_freq_dict_word", "0.1");TessBaseAPI api = new TessBaseAPI();
api.Init(null, "eng");
// Configure for license plate recognition
api.SetPageSegMode(PSM_SINGLE_LINE);
api.SetVariable("tessedit_char_whitelist", "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
api.SetVariable("classify_bln_numeric_mode", "0"); // Allow letters
api.SetVariable("textord_min_chars_in_word", "3");
// Configure for invoice numbers
api.SetPageSegMode(PSM_SINGLE_WORD);
api.SetVariable("tessedit_char_whitelist", "0123456789-");
api.SetVariable("classify_bln_numeric_mode", "1");
// Configure for general document with high accuracy
api.SetPageSegMode(PSM_AUTO);
api.SetVariable("tessedit_pageseg_mode", "3");
api.SetVariable("classify_enable_learning", "1");
api.SetVariable("classify_enable_adaptive_matcher", "1");Control image preprocessing and enhancement algorithms.
// Image enhancement
api.SetVariable("textord_heavy_nr", "1"); // Enable heavy noise reduction
api.SetVariable("textord_noise_rejrows", "1"); // Reject noisy rows
api.SetVariable("textord_noise_rejwords", "1"); // Reject noisy words
// Skew detection and correction
api.SetVariable("textord_straight_baselines", "0"); // Allow curved baselines
api.SetVariable("textord_old_baselines", "1"); // Use old baseline fitting
api.SetVariable("textord_debug_baselines", "0"); // Debug baseline detection
// Text line finding
api.SetVariable("textord_really_old_xheight", "0"); // Use new x-height algorithm
api.SetVariable("textord_fix_xheight_bug", "1"); // Fix x-height bugs
api.SetVariable("textord_fix_makerow_bug", "1"); // Fix row-making bugs
// Edge detection
api.SetVariable("edges_use_new_outline_complexity", "1");
api.SetVariable("edges_max_children_per_outline", "10");TessBaseAPI api = new TessBaseAPI();
api.Init(null, "eng");
// Configure for poor quality scanned documents
api.SetVariable("textord_heavy_nr", "1");
api.SetVariable("textord_noise_rejrows", "1");
api.SetVariable("textord_noise_rejwords", "1");
api.SetVariable("edges_use_new_outline_complexity", "1");
// Configure for high-quality printed text
api.SetVariable("textord_really_old_xheight", "0");
api.SetVariable("textord_fix_xheight_bug", "1");
api.SetVariable("classify_enable_adaptive_matcher", "1");
// Configure for handwritten text
api.SetVariable("classify_enable_learning", "1");
api.SetVariable("classify_adapt_proto_threshold", "230");
api.SetVariable("classify_adapt_feature_threshold", "230");Enable detailed logging and debug output for troubleshooting and analysis.
// General debug output
api.SetDebugVariable("classify_debug_level", "2");
api.SetDebugVariable("textord_debug_tabfind", "1");
api.SetDebugVariable("textord_show_initial_words", "1");
// Image processing debug
api.SetDebugVariable("textord_debug_images", "1");
api.SetDebugVariable("textord_debug_to_screen", "1");
// Classification debug
api.SetDebugVariable("matcher_debug_level", "2");
api.SetDebugVariable("stopper_debug_level", "1");
// Layout analysis debug
api.SetDebugVariable("equationdetect_save_bi_image", "1");
api.SetDebugVariable("paragraph_debug_level", "1");TessBaseAPI api = new TessBaseAPI();
api.Init(null, "eng");
// Enable comprehensive debugging for development
if (debugMode) {
api.SetDebugVariable("classify_debug_level", "3");
api.SetDebugVariable("textord_debug_tabfind", "1");
api.SetDebugVariable("textord_debug_images", "1");
api.SetDebugVariable("matcher_debug_level", "2");
// Set debug output directory
api.SetOutputName("/tmp/tesseract_debug");
}
// Enable specific debugging for problem analysis
if (layoutProblems) {
api.SetDebugVariable("textord_show_initial_words", "1");
api.SetDebugVariable("paragraph_debug_level", "2");
}
if (characterRecognitionProblems) {
api.SetDebugVariable("classify_debug_level", "2");
api.SetDebugVariable("stopper_debug_level", "1");
}Control memory usage, processing speed, and computational resources.
// Processing timeouts
api.SetVariable("tessedit_timeout_millisecs", "30000"); // 30 second timeout
// Memory management
api.SetVariable("max_permuter_attempts", "10000"); // Limit word permutations
api.SetVariable("stopper_smallword_size", "2"); // Small word threshold
// Processing limits
api.SetVariable("language_model_penalty_increment", "0.01");
api.SetVariable("segment_penalty_dict_nonword", "1.25");
api.SetVariable("segment_penalty_garbage", "1.50");
// Quality vs speed trade-offs
api.SetVariable("tessedit_resegment_from_boxes", "1"); // Re-segment from boxes
api.SetVariable("tessedit_resegment_from_line_boxes", "1");
api.SetVariable("tessedit_train_from_boxes", "0"); // Disable training modeTessBaseAPI api = new TessBaseAPI();
api.Init(null, "eng");
// Configure for high-speed processing
api.SetVariable("tessedit_timeout_millisecs", "5000"); // 5 second limit
api.SetVariable("max_permuter_attempts", "5000"); // Reduce attempts
api.SetVariable("stopper_smallword_size", "3"); // Larger small word threshold
// Configure for high-accuracy processing
api.SetVariable("tessedit_timeout_millisecs", "60000"); // 60 second limit
api.SetVariable("max_permuter_attempts", "20000"); // More attempts
api.SetVariable("classify_enable_adaptive_matcher", "1");
api.SetVariable("classify_enable_learning", "1");
// Configure for batch processing
api.SetVariable("tessedit_do_invert", "1"); // Auto-invert if needed
api.SetVariable("tessedit_write_images", "0"); // Don't save debug images
api.SetVariable("tessedit_dump_pageseg_images", "0"); // Don't save segmentation imagesLoad configuration from files and manage configuration sets.
// Configuration file parameters
api.SetVariable("tessedit_init_config_only", "0"); // Load config files
api.SetVariable("user_defined_dpi", "300"); // Set DPI if not detected// Create configuration files for different use cases
// config_highaccuracy.txt
"""
classify_enable_adaptive_matcher 1
classify_enable_learning 1
tessedit_timeout_millisecs 60000
textord_heavy_nr 1
max_permuter_attempts 20000
"""
// config_speed.txt
"""
tessedit_timeout_millisecs 5000
max_permuter_attempts 5000
classify_enable_adaptive_matcher 0
classify_enable_learning 0
"""
// Use configuration files
TessBaseAPI api = new TessBaseAPI();
// Configuration files are loaded automatically from tessdata/configs/
// when specified in Init
api.Init("/usr/share/tessdata", "eng+config_highaccuracy");public class TesseractConfigurations {
public static void configureForNewspaper(TessBaseAPI api) {
api.SetPageSegMode(PSM_AUTO);
api.SetVariable("textord_tabfind_find_tables", "1");
api.SetVariable("textord_tablefind_good_width", "3");
}
public static void configureForBusinessCard(TessBaseAPI api) {
api.SetPageSegMode(PSM_SINGLE_BLOCK);
api.SetVariable("preserve_interword_spaces", "1");
api.SetVariable("tessedit_char_whitelist",
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789@.-+() ");
}
public static void configureForInvoice(TessBaseAPI api) {
api.SetPageSegMode(PSM_AUTO);
api.SetVariable("preserve_interword_spaces", "1");
api.SetVariable("textord_tabfind_find_tables", "1");
}
public static void configureForLicensePlate(TessBaseAPI api) {
api.SetPageSegMode(PSM_SINGLE_LINE);
api.SetVariable("tessedit_char_whitelist", "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
api.SetVariable("classify_bln_numeric_mode", "0");
api.SetVariable("textord_min_chars_in_word", "1");
}
}// Parameter value types
public class TessBaseAPI {
// Integer parameters (thresholds, counts, modes)
public boolean GetIntVariable(String name, int[] value);
// Boolean parameters (feature toggles)
public boolean GetBoolVariable(String name, boolean[] value);
// Double parameters (floating-point thresholds)
public boolean GetDoubleVariable(String name, double[] value);
// String parameters (paths, character sets, languages)
public String GetStringVariable(String name);
}public static final int OEM_TESSERACT_ONLY = 0; // Legacy engine
public static final int OEM_LSTM_ONLY = 1; // LSTM neural network
public static final int OEM_TESSERACT_LSTM_COMBINED = 2; // Combined (deprecated)
public static final int OEM_DEFAULT = 3; // Default modepublic static final int PSM_OSD_ONLY = 0;
public static final int PSM_AUTO_OSD = 1;
public static final int PSM_AUTO_ONLY = 2;
public static final int PSM_AUTO = 3;
public static final int PSM_SINGLE_COLUMN = 4;
public static final int PSM_SINGLE_BLOCK_VERT_TEXT = 5;
public static final int PSM_SINGLE_BLOCK = 6;
public static final int PSM_SINGLE_LINE = 7;
public static final int PSM_SINGLE_WORD = 8;
public static final int PSM_CIRCLE_WORD = 9;
public static final int PSM_SINGLE_CHAR = 10;
public static final int PSM_SPARSE_TEXT = 11;
public static final int PSM_SPARSE_TEXT_OSD = 12;
public static final int PSM_RAW_LINE = 13;Install with Tessl CLI
npx tessl i tessl/maven-org-bytedeco--tesseract-platform