Created
August 30, 2013 15:20
-
-
Save philopon/6390959 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Tesseract.mm | |
// | |
// Created by philopon on 2013/08/22. | |
// Copyright (c) 2013年 philopon. All rights reserved. | |
// | |
#import "Tesseract.h" | |
#include <tesseract/baseapi.h> | |
#include <leptonica/allheaders.h> | |
#define defaultLanguage = @"eng" | |
@implementation LeptonicaPix{ | |
l_uint32* _pixels; | |
} | |
- (id)initWithNSImage:(NSImage*)image{ | |
self = [super init]; | |
if(self){ | |
CGImageRef cg = [image CGImageForProposedRect:NULL context:NULL hints:NULL]; | |
unsigned long width = CGImageGetWidth (cg), | |
height = CGImageGetHeight (cg); | |
_pixels = (l_uint32 *) malloc(width * height * sizeof(l_uint32)); | |
memset(_pixels, 0, width * height * sizeof(l_uint32)); | |
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); | |
CGContextRef context = | |
CGBitmapContextCreate(_pixels, width, height, 8, width * sizeof(uint32_t), colorSpace, | |
kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedLast); | |
CGContextDrawImage(context, CGRectMake(0, 0, width, height), cg); | |
CGContextRelease(context); | |
CGColorSpaceRelease(colorSpace); | |
self.pix = pixCreate(width, height,CGImageGetBitsPerPixel(cg)); | |
pixSetData(self.pix, _pixels); | |
} | |
return self; | |
} | |
- (id)initWithFilePath:(NSString*)path{ | |
self = [super init]; | |
if(self){ | |
self.pix = pixRead([path UTF8String]); | |
} | |
return self; | |
} | |
- (void)dealloc{ | |
free(_pixels); | |
} | |
@end | |
@implementation Tesseract{ | |
tesseract::TessBaseAPI* _tesseract; | |
LeptonicaPix* _pix; | |
} | |
- (id)initWithLanguage:(NSString*)lang{ | |
self = [super init]; | |
if(self){ | |
_tesseract = new tesseract::TessBaseAPI; | |
if(_tesseract->Init(NULL, [lang UTF8String])){ | |
[NSException raise:@"IntializeException" format:@"TessBaseAPI initialize failed."]; | |
} | |
} | |
return self; | |
} | |
- (NSString*)getUTF8Text{ | |
char* cstr = _tesseract->GetUTF8Text(); | |
NSString* str = [NSString stringWithCString: cstr encoding:NSUTF8StringEncoding]; | |
delete [] cstr; | |
return str; | |
} | |
- (void)setImage:(LeptonicaPix*)img{ | |
_tesseract->SetImage(img.pix); | |
_pix = img; | |
} | |
- (void)clear{ | |
_tesseract->Clear(); | |
_pix = nil; | |
} | |
- (BOOL)setVariable:(NSString*)value forName:(NSString*)name{ | |
return _tesseract->SetVariable([name UTF8String], [value UTF8String]); | |
} | |
- (void)dealloc{ | |
_pix = nil; | |
_tesseract->End(); | |
} | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Tesseract.h | |
// | |
// Created by philopon on 2013/08/22. | |
// Copyright (c) 2013年 philopon. All rights reserved. | |
// | |
#import <Cocoa/Cocoa.h> | |
#include <leptonica/environ.h> | |
#include <leptonica/pix.h> | |
@interface LeptonicaPix : NSObject | |
- (id)initWithNSImage:(NSImage*)image; | |
- (id)initWithFilePath:(NSString*)path; | |
@property PIX* pix; | |
@end | |
@interface Tesseract : NSObject | |
- (id)initWithLanguage:(NSString*)lang; | |
- (void)setImage:(LeptonicaPix*)img; | |
- (BOOL)setVariable:(NSString*)value forName:(NSString*)name; | |
- (void)clear; | |
- (NSString*)getUTF8Text; | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment