-3

I am currently working on a project and using tesseract API .

The code is following :

UIImage *bwImage = [image g8_blackAndWhite];
[self.activityIndicator startAnimating];

// Display the preprocessed image to be recognized in the view
self.imageView.image = bwImage;

G8RecognitionOperation *operation = [[G8RecognitionOperation alloc] init];

operation.tesseract.engineMode = G8OCREngineModeTesseractOnly;

operation.tesseract.pageSegmentationMode = G8PageSegmentationModeAutoOnly;

operation.delegate = self;

operation.recognitionCompleteBlock = ^(G8Tesseract *tesseract) {
    // Fetch the recognized text
    NSString *recognizedText = tesseract.recognizedText;

    NSLog(@"%@", recognizedText);

    [self.activityIndicator stopAnimating];

    // Spawn an alert with the recognized text
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"OCR Result"
                                                    message:recognizedText
                                                   delegate:nil
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];
    [alert show];
};

//NSLog(@"%@",);
// Finally, add the recognition operation to the queue
[self.operationQueue addOperation:operation];

}

I want to pass recognizedText string to second View controller but it is not visible outside the block.

How can I achieve this, any advice?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Guri S
  • 1,083
  • 11
  • 12

1 Answers1

0

Declare recognizedText outside block with __block keyword to make it visible outside block.

Like below code:

......

__block NSString *recognizedText;//declared outside to make it visible outside block

operation.recognitionCompleteBlock = ^(G8Tesseract *tesseract) {
    // Fetch the recognized text
    recognizedText = tesseract.recognizedText;

    NSLog(@"%@", recognizedText);

    [self.activityIndicator stopAnimating];

    // Spawn an alert with the recognized text
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"OCR Result"
                                                    message:recognizedText
                                                   delegate:nil
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];
    [alert show];
};

......
Ronak Chaniyara
  • 5,335
  • 3
  • 24
  • 51
  • Thanks for the reply. i declared it outside the block but still when i am trying to print it , nothing is getting displayed NSLog(@"%@", recognizedText); – Guri S Oct 26 '16 at 05:15
  • Basically what i am trying to achieve is that message gets displayed as alert but i want to pass the string and display on another view controller using segue – Guri S Oct 26 '16 at 05:21
  • you will get result after block execute completely. – Ronak Oct 26 '16 at 05:23
  • Means you want to redirect next page when alert message popup? – Ronak Oct 26 '16 at 05:25
  • altert message pop is redirected to the new page but i want to pass this string : recognizedText to the textView on the next page so that it can be edited .... – Guri S Oct 26 '16 at 05:34
  • Using the following method to segue to the next screen but not able to pass recognizedText ..............................................................- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if ([segue.identifier isEqualToString:@"OcrSegue"]) { [self recognizeImageWithTesseract:self.imageView.image ]; } – Guri S Oct 26 '16 at 05:35