Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6549271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:04:23+00:00 2026-05-25T12:04:23+00:00

In Xcode, I am trying to pre process an image prior to sending it

  • 0

In Xcode, I am trying to pre process an image prior to sending it to OCR’ing. The OCR engine, Tesseract, handles images based on the Leptonica library.

As an example:
The Leptonica feature pixConvertTo8(“image.tif”)… is there a way to “transfer” the image raw data from UIImage -> PIX (see pix.h from the leptonica library) -> perform the pixConvertTo8() and back from PIX -> UImage – and this preferably without saving it to a file for transition – all in memory.

- (void) processImage:(UIImage *) uiImage
{
 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

// preprocess UIImage here with fx: pixConvertTo8();

CGSize imageSize = [uiImage size];
int bytes_per_line  = (int)CGImageGetBytesPerRow([uiImage CGImage]);
int bytes_per_pixel = (int)CGImageGetBitsPerPixel([uiImage CGImage]) / 8.0;

CFDataRef data = CGDataProviderCopyData(CGImageGetDataProvider([uiImage CGImage]));
const UInt8 *imageData = CFDataGetBytePtr(data);

// this could take a while.
char* text = tess->TesseractRect(imageData,
                                 bytes_per_pixel,
                                 bytes_per_line,
                                 0, 0,
                                 imageSize.width, imageSize.height);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T12:04:23+00:00Added an answer on May 25, 2026 at 12:04 pm

    these two functions will do the trick….

    - (void) startTesseract
    {
    //code from http://robertcarlsen.net/2009/12/06/ocr-on-iphone-demo-1043
    
    NSString *dataPath = 
        [[self applicationDocumentsDirectory]stringByAppendingPathComponent:@"tessdata"];
    /*
     Set up the data in the docs dir
     want to copy the data to the documents folder if it doesn't already exist
     */
    NSFileManager *fileManager = [NSFileManager defaultManager];
    // If the expected store doesn't exist, copy the default store.
    if (![fileManager fileExistsAtPath:dataPath]) {
        // get the path to the app bundle (with the tessdata dir)
        NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
        NSString *tessdataPath = [bundlePath stringByAppendingPathComponent:@"tessdata"];
        if (tessdataPath) {
            [fileManager copyItemAtPath:tessdataPath toPath:dataPath error:NULL];
        }
    }
    
    NSString *dataPathWithSlash = [[self applicationDocumentsDirectory] stringByAppendingString:@"/"];
    setenv("TESSDATA_PREFIX", [dataPathWithSlash UTF8String], 1);
    
    // init the tesseract engine.
    tess = new  tesseract::TessBaseAPI();
    tess->Init([dataPath cStringUsingEncoding:NSUTF8StringEncoding], "eng");
    
    }
    
    - (NSString *) ocrImage: (UIImage *) uiImage
    {
    
    //code from http://robertcarlsen.net/2009/12/06/ocr-on-iphone-demo-1043
        CGSize imageSize = [uiImage size];
    double bytes_per_line   = CGImageGetBytesPerRow([uiImage CGImage]);
    double bytes_per_pixel  = CGImageGetBitsPerPixel([uiImage CGImage]) / 8.0;
    
    CFDataRef data = CGDataProviderCopyData(CGImageGetDataProvider([uiImage CGImage]));
    const UInt8 *imageData = CFDataGetBytePtr(data);
    imageThresholder = new tesseract::ImageThresholder();       
    
    imageThresholder->SetImage(imageData,(int) imageSize.width,(int) imageSize.height,(int)bytes_per_pixel,(int)bytes_per_line);
    
    
    
    // this could take a while. maybe needs to happen asynchronously.
    tess->SetImage(imageThresholder->GetPixRect());
    
    char* text = tess->GetUTF8Text();
    // Do something useful with the text!
    NSLog(@"Converted text: %@",[NSString stringWithCString:text encoding:NSUTF8StringEncoding]);
    
    return [NSString stringWithCString:text encoding:NSUTF8StringEncoding]
    }
    

    You will have to declare both tess and imageThresholder in the .h file

    tesseract::TestBaseApi *tess;
    tesseract::ImageThresholder *imageThresholder;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to build for multiple targets in Xcode, to simplify the process of
I'm trying to build a PhoneGap-based application using Xcode 4. Since there's no PhoneGap
I have 2 errors in XCode and am trying to figure out what they
iam trying play animation with xcode,in specefic Time for example after 3 minutes play
I'm trying to emulate Xcode's ⌘-R keystroke in another editor (namely, Vim); I thought
I'm trying to setup my Xcode project to be instrumented with gcov so I
I'm trying to run Python scripts using Xcode's User Scripts menu. The issue I'm
I'm trying to compile some fortran files in Xcode, using a makefile made by
I'm trying to learn Objective-C for xcode. I was wondering if there was an
I have a plist file in my resources group in xcode. I am trying

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.