### Install Dependencies with CocoaPods Source: https://github.com/gali8/tesseract-ocr-ios/wiki/Contributing Install necessary testing libraries using CocoaPods before running unit tests. ```bash pod install ``` -------------------------------- ### Basic Tesseract OCR Integration in Swift Source: https://github.com/gali8/tesseract-ocr-ios/wiki/Using-Tesseract-OCR-iOS This snippet demonstrates the basic setup for Tesseract OCR in a Swift `ViewController`. It initializes Tesseract with specific languages, sets a delegate, defines a character whitelist, loads an image, and performs recognition. Ensure you have configured an Objective-C bridging header. ```Swift import UIKit import TesseractOCR class ViewController: UIViewController, G8TesseractDelegate { override func viewDidLoad() { super.viewDidLoad() var tesseract:G8Tesseract = G8Tesseract(language:"eng+ita") //tesseract.language = "eng+ita" tesseract.delegate = self tesseract.charWhitelist = "01234567890" tesseract.image = UIImage(named: "image_sample.jpg") tesseract.recognize() print(tesseract.recognizedText) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } func shouldCancelImageRecognitionForTesseract(tesseract: G8Tesseract!) -> Bool { return false // return true if you need to interrupt tesseract before it finishes } } ``` -------------------------------- ### Install with Carthage Source: https://github.com/gali8/tesseract-ocr-ios/blob/master/README.md Add this line to your Cartfile to include the Tesseract OCR iOS framework in your project via Carthage. After adding, run 'carthage update'. ```bash github "gali8/Tesseract-OCR-iOS" ``` -------------------------------- ### Add Tesseract OCR via CocoaPods (Stable Version) Source: https://github.com/gali8/tesseract-ocr-ios/wiki/Installation Use this snippet to add the latest stable version of the Tesseract OCR iOS library to your project's Podfile. After adding the line, run 'pod install'. ```ruby pod 'TesseractOCRiOS', '4.0.0' ``` -------------------------------- ### Objective-C: Basic Tesseract OCR Usage Source: https://github.com/gali8/tesseract-ocr-ios/wiki/Using-Tesseract-OCR-iOS Demonstrates how to initialize G8Tesseract, set recognition parameters, and perform OCR on an image. Use this for direct, synchronous recognition tasks. ```Objective-C #import @interface MyViewController : UIViewController @end ``` ```Objective-C - (void)viewDidLoad { [super viewDidLoad]; // Languages are used for recognition (e.g. eng, ita, etc.). Tesseract engine // will search for the .traineddata language file in the tessdata directory. // For example, specifying "eng+ita" will search for "eng.traineddata" and // "ita.traineddata". Cube engine will search for "eng.cube.*" files. // See https://github.com/tesseract-ocr/tessdata. // Create your G8Tesseract object using the initWithLanguage method: G8Tesseract *tesseract = [[G8Tesseract alloc] initWithLanguage:@"eng+ita"]; // Optionaly: You could specify engine to recognize with. // G8OCREngineModeTesseractOnly by default. It provides more features and faster // than Cube engine. See G8Constants.h for more information. //tesseract.engineMode = G8OCREngineModeTesseractOnly; // Set up the delegate to receive Tesseract's callbacks. // self should respond to TesseractDelegate and implement a // "- (BOOL)shouldCancelImageRecognitionForTesseract:(G8Tesseract *)tesseract" // method to receive a callback to decide whether or not to interrupt // Tesseract before it finishes a recognition. tesseract.delegate = self; // Optional: Limit the character set Tesseract should try to recognize from tesseract.charWhitelist = @"0123456789"; // This is wrapper for common Tesseract variable kG8ParamTesseditCharWhitelist: // [tesseract setVariableValue:@"0123456789" forKey:kG8ParamTesseditCharBlacklist]; // See G8TesseractParameters.h for a complete list of Tesseract variables // Optional: Limit the character set Tesseract should not try to recognize from //tesseract.charBlacklist = @"OoZzBbSs"; // Specify the image Tesseract should recognize on tesseract.image = [[UIImage imageNamed:@"image_sample.jpg"] g8_blackAndWhite]; // Optional: Limit the area of the image Tesseract should recognize on to a rectangle tesseract.rect = CGRectMake(20, 20, 100, 100); // Optional: Limit recognition time with a few seconds tesseract.maximumRecognitionTime = 2.0; // Start the recognition [tesseract recognize]; // Retrieve the recognized text NSLog(@"%@", [tesseract recognizedText]); // You could retrieve more information about recognized text with that methods: NSArray *characterBoxes = [tesseract recognizedBlocksByIteratorLevel:G8PageIteratorLevelSymbol]; NSArray *paragraphs = [tesseract recognizedBlocksByIteratorLevel:G8PageIteratorLevelParagraph]; NSArray *characterChoices = tesseract.characterChoices; UIImage *imageWithBlocks = [tesseract imageWithBlocks:characterBoxes drawText:YES thresholded:NO]; } - (void)progressImageRecognitionForTesseract:(G8Tesseract *)tesseract { NSLog(@"progress: %lu", (unsigned long)tesseract.progress); } - (BOOL)shouldCancelImageRecognitionForTesseract:(G8Tesseract *)tesseract { return NO; // return YES, if you need to interrupt tesseract before it finishes } ``` -------------------------------- ### Specify Architectures for Build Source: https://github.com/gali8/tesseract-ocr-ios/blob/master/TesseractOCR/README_howto_compile_libaries.md Builds Tesseract OCR libraries for specific iOS architectures, useful for AppStore submissions. ```bash export ARCHS=armv7, armv7s, arm64 ``` -------------------------------- ### Initialize Tesseract with Configuration Files Source: https://github.com/gali8/tesseract-ocr-ios/wiki/Advanced-Tesseract-Configuration Initializes a G8Tesseract object using custom configuration files. This is useful for managing complex or reusable sets of parameters. ```Objective-C // Construct the paths to our config files NSString *resourcePath = [NSBundle bundleForClass:G8Tesseract.class].resourcePath; NSString *tessdataFolderName = @"tessdata"; NSString *tessdataFolderPathFromTheBundle = [[resourcePath stringByAppendingPathComponent:tessdataFolderName] stringByAppendingString:@"/"]; NSString *debugConfigFileName = @"debugConfig.txt"; NSString *recognitionConfigFileName = @"recognitionConfig.txt"; NSString *tessConfigsFolderName = @"tessconfigs"; NSString *debugConfigFilePath = [[tessdataFolderPathFromTheBundle stringByAppendingPathComponent:tessConfigsFolderName] stringByAppendingPathComponent:debugConfigsFileName]; NSString *recognitionConfigFilePath = [[tessdataFolderPathFromTheBundle stringByAppendingPathComponent:tessConfigsFolderName] stringByAppendingPathComponent:recognitionConfigsFileName]; // Initialize the `G8Tesseract` object using the config files G8Tesseract *tesseract = [[G8Tesseract alloc] initWithLanguage:kG8Languages configDictionary:nil configFileNames:@[debugConfigFilePath, recognitionConfigFilePath] cachesRelatedDataPath:nil engineMode:G8OCREngineModeTesseractOnly]; ``` -------------------------------- ### Objective-C: Asynchronous Tesseract OCR with NSOperationQueue Source: https://github.com/gali8/tesseract-ocr-ios/wiki/Using-Tesseract-OCR-iOS Shows how to perform Tesseract OCR asynchronously using NSOperationQueue for better performance in the UI thread. Ideal for long-running recognition tasks. ```Objective-C #import @interface MyViewController : UIViewController @end ``` ```Objective-C - (void)viewDidLoad { // Create RecognitionOperation G8RecognitionOperation *operation = [[G8RecognitionOperation alloc] initWithLanguage:@"eng+ita"]; // Configure inner G8Tesseract object as described before operation.tesseract.charWhitelist = @"01234567890"; operation.tesseract.image = [[UIImage imageNamed:@"image_sample.jpg"] g8_blackAndWhite]; // Setup the recognitionCompleteBlock to receive the Tesseract object // after text recognition. It will hold the recognized text. operation.recognitionCompleteBlock = ^(G8Tesseract *recognizedTesseract) { // Retrieve the recognized text upon completion NSLog(@"%@", [recognizedTesseract recognizedText]); }; // Add operation to queue NSOperationQueue *queue = [[NSOperationQueue alloc] init]; [queue addOperation:operation]; } ``` -------------------------------- ### Initialize Tesseract with Configuration Dictionary Source: https://github.com/gali8/tesseract-ocr-ios/wiki/Advanced-Tesseract-Configuration Initializes a G8Tesseract object with specified language and a configuration dictionary. This method allows setting multiple parameters during initialization. ```Objective-C // During initialization, set the whitelist to recognize only the numbers 0 through 9 // and disable word dictionaries G8Tesseract *tesseract = [[G8Tesseract alloc] initWithLanguage:@"eng" configDictionary:@{ kG8ParamTesseditCharWhitelist: @"0123456789", kG8ParamLoadSystemDawg : @"F", kG8ParamLoadFreqDawg : @"F", } configFileNames:nil cachesRelatedDataPath:nil engineMode:G8OCREngineModeTesseractOnly]; ``` -------------------------------- ### Link libz.dylib or libz.tbd in Xcode Source: https://github.com/gali8/tesseract-ocr-ios/wiki/Installation Manually link the libz library to your target in Xcode's 'Build Phases' if it's not automatically included. This is a necessary step for the Tesseract OCR framework. ```bash -lz ``` -------------------------------- ### Initialize G8Tesseract with Custom Tessdata Path Source: https://github.com/gali8/tesseract-ocr-ios/wiki/Advanced-Tesseract-Configuration Use this Objective-C snippet to initialize `G8Tesseract` with a custom path relative to the Caches directory for the 'tessdata' folder. This allows for runtime downloads of language and configuration files. ```Objective-C G8Tesseract *tesseract = [[G8Tesseract alloc] initWithLanguage:@"eng" configDictionary:nil configFileNames:nil cachesRelatedDataPath:@"foo/bar" engineMode:G8OCREngineModeTesseractOnly]; ``` -------------------------------- ### Build Tesseract OCR Libraries Source: https://github.com/gali8/tesseract-ocr-ios/blob/master/TesseractOCR/README_howto_compile_libaries.md Compiles dependent libraries and Tesseract for all iOS architectures (device and simulator). ```bash make ``` -------------------------------- ### Clone Tesseract-OCR-iOS Repository Source: https://github.com/gali8/tesseract-ocr-ios/wiki/Contributing Clone your forked repository from GitHub to your local machine to begin making changes. ```bash git clone git@github.com:username/Tesseract-OCR-iOS.git ``` -------------------------------- ### Using GPUImage Adaptive Threshold Filter for OCR Source: https://github.com/gali8/tesseract-ocr-ios/wiki/Tips for Improving OCR Results Preprocess an image using GPUImage's adaptive threshold filter before passing it to Tesseract. Adjust 'blurRadiusInPixels' to fine-tune the filter. ```Objective-C // Grab the image you want to preprocess UIImage *inputImage = [UIImage imageNamed:@"my_test_image.jpg"]; // Initialize our adaptive threshold filter GPUImageAdaptiveThresholdFilter *stillImageFilter = [[GPUImageAdaptiveThresholdFilter alloc] init]; stillImageFilter.blurRadiusInPixels = 4.0 // adjust this to tweak the blur radius of the filter, defaults to 4.0 // Retrieve the filtered image from the filter UIImage *filteredImage = [stillImageFilter imageByFilteringImage:inputImage]; // Give Tesseract the filtered image tesseract.image = filteredImage; ``` -------------------------------- ### Add Tesseract OCR via CocoaPods (Development Version) Source: https://github.com/gali8/tesseract-ocr-ios/wiki/Installation Integrate the development version of the Tesseract OCR iOS library from its GitHub repository. Ensure you only have one 'TesseractOCRiOS' line in your Podfile. This uses the master branch and is not based on a stable release. ```ruby pod 'TesseractOCRiOS', :git => 'https://github.com/gali8/Tesseract-OCR-iOS.git' ``` -------------------------------- ### Import TesseractOCR in Swift Class Source: https://github.com/gali8/tesseract-ocr-ios/wiki/Installation Remember to import the TesseractOCR module at the top of your Swift class file to use its functionalities. ```swift import TesseractOCR ``` -------------------------------- ### Bypassing Tesseract's Internal Thresholder with Delegate Source: https://github.com/gali8/tesseract-ocr-ios/wiki/Tips for Improving OCR Results Implement the Tesseract delegate method 'preprocessedImageForTesseract:sourceImage:' to bypass Tesseract's internal thresholding when custom preprocessing is applied. This method should return the preprocessed image. ```Objective-C // somewhere in the function of your class // set the delegate tesseract.delegate = self; // give the original, non-processed image to Tesseract tesseract.image = [UIImage imageNamed:@"my_test_image.jpg"]; // Tesseract delegate method inside of your class - (UIImage *)preprocessedImageForTesseract:(G8Tesseract *)tesseract sourceImage:(UIImage *)sourceImage { // sourceImage is the same image you sent to Tesseract above UIImage *inputImage = sourceImage; // Initialize our adaptive threshold filter GPUImageAdaptiveThresholdFilter *stillImageFilter = [[GPUImageAdaptiveThresholdFilter alloc] init]; stillImageFilter.blurRadiusInPixels = 4.0 // adjust this to tweak the blur radius of the filter, defaults to 4.0 // Retrieve the filtered image from the filter UIImage *filteredImage = [stillImageFilter imageByFilteringImage:inputImage]; // Give the filteredImage to Tesseract instead of the original one, // allowing us to bypass the internal thresholding step. // filteredImage will be sent immediately to the recognition step return filteredImage; } ``` -------------------------------- ### Set GENERATE_MASTER_OBJECT_FILE to YES Source: https://github.com/gali8/tesseract-ocr-ios/wiki/Installation Resolve runtime errors like 'unrecognized selector sent to instance' by setting 'GENERATE_MASTER_OBJECT_FILE' to 'YES' in the Tesseract OCR iOS.xcodeproj build settings. This may increase the executable file size. ```bash GENERATE_MASTER_OBJECT_FILE = YES ``` -------------------------------- ### Read Tesseract Parameter Value Source: https://github.com/gali8/tesseract-ocr-ios/wiki/Advanced-Tesseract-Configuration Reads the value of a specific Tesseract parameter using its key. Useful for inspecting current settings. ```Objective-C // Assuming "tesseract" is an already initialized `G8Tesseract` object NSString *whitelist = [tesseract variableValueForKey:kG8ParamTesseditCharWhitelist]; ``` -------------------------------- ### Add -lstdc++ to Other Linker Flags Source: https://github.com/gali8/tesseract-ocr-ios/wiki/Installation Include '-lstdc++' in your project's 'Other Linker Flags' to prevent a multitude of build errors during the linking phase. ```bash -lstdc++ ``` -------------------------------- ### Clearing Tesseract Instance Source: https://github.com/gali8/tesseract-ocr-ios/wiki/Release-Notes Deprecated method for clearing Tesseract instance. Use `tesseract = nil;` to free memory. ```Objective-C [tesseract clear]; //call Clear() end End() functions ``` -------------------------------- ### Set Tesseract Parameter Individually Source: https://github.com/gali8/tesseract-ocr-ios/wiki/Advanced-Tesseract-Configuration Sets a single Tesseract parameter value after the Tesseract object has been initialized. This method is straightforward for modifying one parameter at a time. ```Objective-C // Assuming "tesseract" is an already initialized `G8Tesseract` object // Set the whitelist to recognize only the numbers 0 through 9 [tesseract setVariableValue:@"0123456789" forKey:kG8ParamTesseditCharWhitelist]; ``` -------------------------------- ### Disable Bitcode for Swift (iOS 9 & 10) Source: https://github.com/gali8/tesseract-ocr-ios/wiki/Installation For Swift projects targeting iOS 9 and iOS 10, disable Bitcode in your target's build settings. This is also required for the Tesseract OCR POD's bitcode. ```bash ENABLE_BITCODE = NO ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.