测试ocr for iphone但没有工作

时间:2010-09-25 02:13:00

标签: iphone

我正在使用ocr代码,但扫描时我没有得到任何价值 cc和text的值是无效意味着我无法看到任何输出我在这里做错了吗?

- (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);

 // this could take a while. maybe needs to happen asynchronously.
  text = tess->TesseractRect(imageData,(int)bytes_per_pixel,(int)bytes_per_line, 0, 0,(int) imageSize.height,(int) imageSize.width);

 // Do something useful with the text!
 NSLog(@"Converted text: %@",[NSString stringWithCString:text encoding:NSUTF8StringEncoding]);
 NSString *cc=[NSString stringWithCString:text ];// nothing
NSLog(@"secess %@",cc); // nothing





// const char *sqlStatement = [text UTF8String];//"SELECT * FROM wateat_tbl where name like '%love%' or desc like '%love%'";//limit 0,40
 //NSLog(@"query %c", sqlStatement);




 return [NSString stringWithCString:text encoding:NSUTF8StringEncoding];
}

1 个答案:

答案 0 :(得分:1)

- (NSString *) ocrImage: (UIImage *) uiImage
{
    // <MARCELO>

    CGContextRef    context = NULL;
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    void *          bitmapData;
    int             bitmapByteCount;
    int             bitmapBytesPerRow;
    int             bitsPerComponent = 8;
    int width;
    int height;

    CGImageRef image = uiImage.CGImage;

    int numberOfComponents = 4;

    width = CGImageGetWidth(image);
    height = CGImageGetHeight(image);
    CGRect imageRect = {{0,0},{width, height}};
    // Declare the number of bytes per row. Each pixel in the bitmap in this example is represented by 4 bytes; 8 bits each of red, green, blue, and  alpha.
    bitmapBytesPerRow   = (width * numberOfComponents);
    bitmapByteCount     = (bitmapBytesPerRow * height);

    // Allocate memory for image data. This is the destination in memory
    // where any drawing to the bitmap context will be rendered.
    bitmapData = malloc( bitmapByteCount );
    if (bitmapData == NULL) {
        CGColorSpaceRelease( colorSpace );
        return @"";
    }

    context = CGBitmapContextCreate (bitmapData, width, height, 
                                     bitsPerComponent, bitmapBytesPerRow, colorSpace,
                                     kCGImageAlphaPremultipliedFirst);//kCGImageAlphaNoneSkipFirst);//kCGImageAlphaNone);//
    if (context == NULL)  {
        free (bitmapData);
        CGColorSpaceRelease( colorSpace );
        return @"";
    }

    CGContextDrawImage(context, imageRect, image);
    CGColorSpaceRelease( colorSpace );
    void * buf = CGBitmapContextGetData (context);  

    NSDate *start = [NSDate date];

    char* text = tess->TesseractRect((unsigned char*)buf, 4, bitmapBytesPerRow, 0, 0, width, height);

    NSDate *end = [NSDate date];
    NSLog(@"%g", [end timeIntervalSinceDate:start]);

    free( buf );

    // Do something useful with the text!
    NSLog(@"Converted text: %@",[NSString stringWithCString:text encoding:NSUTF8StringEncoding]);

    return [NSString stringWithCString:text encoding:NSUTF8StringEncoding];
}