簡體   English   中英

目標C中未識別的功能

[英]function non recognized in objective-C

我是IOS開發的新手,但是我面臨的這個問題對我來說似乎並不合邏輯。 我在ViewController.h和ViewController.mm中都聲明了一個函數(由於我正在使用C ++而更改為.mm),但是當我在ViewController.mm中調用它時,我得到use of undeclared identifier錯誤。

這是頭文件中的聲明:

#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]];


}

您已經定義了一個實例方法而不是一個函數 與代碼中的其他方法調用一樣,您需要使用以下方法調用:

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

另外,如果您想要一個函數,則可以聲明為:

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

在這種情況下,該函數無法訪問任何實例變量。

高溫超導

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM