2016-04-17 64 views
0

我是IOS開發新手,但是我面對的這個問題對我來說似乎不合邏輯。我在ViewController.h和ViewController.mm(更改爲.mm,因爲我使用C++)中聲明瞭一個函數,但是當我在ViewController.mm中調用它時,我得到了use of undeclared identifier錯誤。函數在目標中未被識別-C

下面是在頭文件中的聲明:

#import <UIKit/UIKit.h> 
#import <opencv2/highgui/cap_ios.h> 

using namespace cv; 

@interface ViewController : UIViewController<CvVideoCameraDelegate> 
{ 
    IBOutlet UIImageView* imageview; 
    CvVideoCamera* videoCamera; 
    IBOutlet UILabel *label; 

} 
- (UIImage *) CVMatToImage:(cv::Mat) matrice; 

@property (nonatomic, retain) CvVideoCamera* videoCamera; 

-(IBAction)cameraStart:(id)sender; 
-(IBAction)cameraStop:(id)sender; 

@end 

,並在.mm文件中的定義:

- (UIImage *) CVMatToImage:(cv::Mat) matrice 
{ 
    NSData *data = [NSData dataWithBytes:matrice.data length:matrice.elemSize()*matrice.total()]; 

    CGColorSpaceRef colorSpace; 
    CGBitmapInfo bitmapInfo; 

    if (matrice.elemSize() == 1) { 
     colorSpace = CGColorSpaceCreateDeviceGray(); 
     bitmapInfo = kCGImageAlphaNone | kCGBitmapByteOrderDefault; 
    } else { 
     colorSpace = CGColorSpaceCreateDeviceRGB(); 
     bitmapInfo = kCGBitmapByteOrder32Little | (
                matrice.elemSize() == 3? kCGImageAlphaNone : kCGImageAlphaNoneSkipFirst 
                ); 
    } 

    CGDataProviderRef provider = CGDataProviderCreateWithCFData((__bridge CFDataRef)data); 

    // Creating CGImage from cv::Mat 
    CGImageRef imageRef = CGImageCreate(
             matrice.cols,     //width 
             matrice.rows,     //height 
             8,       //bits per component 
             8 * matrice.elemSize(),  //bits per pixel 
             matrice.step[0],    //bytesPerRow 
             colorSpace,     //colorspace 
             bitmapInfo,     // bitmap info 
             provider,     //CGDataProviderRef 
             NULL,      //decode 
             false,      //should interpolate 
             kCGRenderingIntentDefault //intent 
             ); 

    // Getting UIImage from CGImage 
    UIImage *finalImage = [UIImage imageWithCGImage:imageRef]; 
    CGImageRelease(imageRef); 
    CGDataProviderRelease(provider); 
    CGColorSpaceRelease(colorSpace); 

    return finalImage; 


} 

但是,當我把它稱爲一個IBAction爲內我得到一個錯誤

-(IBAction)cameraStop:(id)sender 
{ 


    [self.videoCamera stop]; 

    ////////////////////// 

    // Don't forget to process image here 

    ////////////////////// 
    UIImage *finalImageToOCR = CVMatToImage(cvMatrice); 

    [imageview setImage:finalImageToOCR]; 
    G8Tesseract *tesseract = [[G8Tesseract alloc] initWithLanguage:@"fra"]; 
    [tesseract setImage:finalImageToOCR]; 
    [tesseract recognize]; 
    NSLog(@"%@", [tesseract recognizedText]); 
    label.text = [NSString stringWithFormat:[tesseract recognizedText]]; 


} 

回答

3

您已經定義了一個實例方法而不是函數。像其他的方法,在你的代碼調用,您需要使用的線沿線的一個方法調用:如果你想你將宣佈一個功能是

UIImage *finalImageToOCR = [<object instance> CVMatToImage:cvMatrice]; 

或者爲:

UIImage *CVMatToImage(cv::Mat matrice) { ... } 

在這種情況下,函數不能訪問任何實例變量。

HTH

+0

是的,我只是爲自己的問題寫了一個答案。我用一個ViewController的實例解決了這個問題。 現在我已經把它改成了一個你建議的功能,並且它完美地工作。非常感謝你! – user3491634