2014-07-22 52 views
0

我做這個教程http://rdcworld-iphone.blogspot.com.au/2013/03/how-to-use-barcode-scanner-br-and-qr-in.htmlQR和條碼掃描儀的代碼不起作用的Xcode 5.1

我使用的是IOS SDK 7.1時,Xcode 5.1.1,Mac OSX版10.9.4和目標C iPhone模擬器。什麼是應該,當我運行的程序是如下情況發生:

1)我點擊它第一次出現

2的窗口)我得到的選項,選擇圖像中的iPhone掃描上的掃描按鈕相冊

3)I選擇QR或條碼

4)掃描條形碼

5)確定的值,並顯示在屏幕上與所述掃描圖像的小版本一起。

究竟發生了什麼,它起牀到第4步罰款和掃描圖像運行完成(startScanning)的方法。然而,不要做第5步,它會在屏幕上顯示一個巨大的條形碼版本,並且從不會調用執行步驟5的方法(didFinishPickingMediaWithInfo)。

內容ViewController.h的

// 
// ViewController.h 
// BarCodeScannerDemo 
// 
// Created by RDC on 3/11/13. 
// Copyright (c) 2013 RDC World. All rights reserved. 
// 

#import <UIKit/UIKit.h> 
#import "ZBarSDK.h" 

@interface ViewController : UIViewController<ZBarReaderDelegate> 

@property (weak, nonatomic) IBOutlet UIImageView *resultImageView; 
@property (weak, nonatomic) IBOutlet UITextView *resultTextView; 
- (IBAction)startScanning:(id)sender; 

@end 

ViewController.m的內容

// 
// ViewController.m 
// BarCodeScannerDemo 
// 
// Created by RDC on 3/11/13. 
// Copyright (c) 2013 RDC World. All rights reserved. 
// 

#import "ViewController.h" 

@interface ViewController() 
@end 

@implementation ViewController 

@synthesize resultImageView; 
@synthesize resultTextView; 

#pragma mark - ViewController's LifeCycle methods 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    NSLog(@"View did load"); 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    NSLog(@"Did receive memory warning"); 
} 

#pragma mark - Button click method 


- (IBAction)startScanning:(id)sender 
{ 
    NSLog(@"Scanning.."); 
    resultTextView.text = @"Scanning.."; 

    //Create a reader 
    ZBarReaderViewController *codeReader = [ZBarReaderViewController new]; 
    //Setup a delegate to recieve the results 
    //The delegate implements the ZBarReaderDelegate protocol, which inherits from UIImagePickerControllerDelegate 
    codeReader.readerDelegate= self; 

    codeReader.supportedOrientationsMask = ZBarOrientationMaskAll; 

    ZBarImageScanner *scanner = codeReader.scanner; 
    [scanner setSymbology: ZBAR_I25 config: ZBAR_CFG_ENABLE to: 0]; 

    [self presentViewController:codeReader animated:YES completion:nil]; 
    NSLog(@"End Start Scanning method"); 

} 

#pragma mark - ZBar's Delegate method 
//Called when a barcode is successsfully decoded 
//reader is the reader controller instance that read the barcodes 
- (void) imagePickerController: (UIImagePickerController*) reader didFinishPickingMediaWithInfo: (NSDictionary*) info 
{ 
    NSLog(@"Decode results..."); 


    // get the decode results 
    id<NSFastEnumeration> results = [info objectForKey: ZBarReaderControllerResults]; 

    ZBarSymbol *symbol = nil; 
    for(symbol in results) 
     break;// just grab the first barcode 

    // showing the result on textview 
    resultTextView.text = symbol.data; 

    resultImageView.image = [info objectForKey: UIImagePickerControllerOriginalImage]; 

    // dismiss the controller 
    [reader dismissViewControllerAnimated:YES completion:nil]; 
} 


-(void) readerControllerDidFailToRead:(ZBarReaderController *)reader withRetry:(BOOL)retry 
{ 
    NSLog(@"readerControllerDidFailToRead"); 
    //If retry parameter is NO controller must be dismissed. 
    if(retry==NO) 
     reader=nil; 
} 

@end 

AppDelegate.h內容

// 
// AppDelegate.h 
// BarCodeScannerDemo 
// 
// Created by RDC on 3/11/13. 
// Copyright (c) 2013 RDC World. All rights reserved. 
// 

#import <UIKit/UIKit.h> 

@class ViewController; 

@interface AppDelegate : UIResponder <UIApplicationDelegate> 

@property (strong, nonatomic) UIWindow *window; 

@property (strong, nonatomic) ViewController *viewController; 

@end 

AppDelegate.m

內容
// 
// AppDelegate.m 
// BarcodeScannerDemo 
// 
// Created by Airefrig Australia on 18/07/2014. 
// Copyright (c) 2014 RDCWorld. All rights reserved. 
// 

#import "AppDelegate.h" 
#import "ViewController.h" 

@implementation AppDelegate 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
    self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil]; 
    self.window.rootViewController = self.viewController; 
    [self.window makeKeyAndVisible]; 
    return YES; 
} 

- (void)applicationWillResignActive:(UIApplication *)application 
{ 
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. 
} 

- (void)applicationDidEnterBackground:(UIApplication *)application 
{ 
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 
} 

- (void)applicationWillEnterForeground:(UIApplication *)application 
{ 
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. 
} 

- (void)applicationDidBecomeActive:(UIApplication *)application 
{ 
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 
} 

- (void)applicationWillTerminate:(UIApplication *)application 
{ 
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 
} 

@end 

我花了很多小時試圖找到解決方案,我還沒有找到任何提供解決方案。我見過的唯一一個有我的問題的人在教程頁評論部分,但他們從來沒有得到答覆。

請幫忙!

編輯:

顯示的圖像旋轉90度。另外,我對客觀的C是新的,所以如果你提供代碼,請解釋爲什麼差異應該起作用。我想提高我的知識=)

編輯:解

既然不能要求後不到8小時回答我的問題,這裏是我發現:

在閱讀ZBarSDK API參考有關ZBarReaderViewController在這裏:http://zbar.sourceforge.net/iphone/sdkdoc/ZBarReaderViewController.html

它說:「這是控制器用於實時掃描從相機飼料自動捕獲。從圖像文件掃描或手動捕獲,請參閱ZBarReaderController。」

這意味着startScanning方法中的ZBarReaderViewController對象設置不是應該在那裏。那麼,不是爲了掃描靜態圖像。我必須用實際的設備而不是Mac來測試原始代碼,但看起來ZBarReaderController是我真正想要的。

新startScanning方法現在看起來是這樣的:

- (IBAction)startScanning:(id)sender 
{ 
    resultTextView.text = @"Scanning.."; 

    //Create a reader 
    ZBarReaderViewController *codeReader = [ZBarReaderController new]; 
    //Setup a delegate to recieve the results 
    //The delegate implements the ZBarReaderDelegate protocol, which inherits from UIImagePickerControllerDelegate 
    codeReader.readerDelegate= self; 

    [codeReader.scanner setSymbology: ZBAR_I25 config: ZBAR_CFG_ENABLE to: 0]; 

    [self presentViewController:codeReader animated:YES completion:nil]; 

} 

當測試我注意到,有透明的背景圖像(如提供的教程網站的QR碼和條形碼圖像)不工作 - 你會收到一條消息,說沒有找到代碼。您應該將它們另存爲帶有白色背景的jpg文件。

我不知道是否應該做其他事情,因爲我只重命名了一個對象並刪除了一些給出錯誤的代碼行 - 但程序確實按照我期望的方式運行。如果我有任何未來的問題,我會發佈一個新的問題。

回答

0

試試這個代碼。

ZBarReaderViewController *codeReader = [ZBarReaderViewController new]; 
codeReader.readerDelegate= self; 
codeReader.supportedOrientationsMask = ZBarOrientationMaskAll; 
[codeReader.scanner setSymbology:ZBAR_I25 config:ZBAR_CFG_ENABLE to:0]; 
[codeReader.readerView start]; 

[self presentViewController:codeReader animated:YES completion:nil]; 

下面是可選=)

[codeReader.readerView setZoom:2]; 
codeReader.view.frame = self.view.bounds; 
+0

謝謝你的快速回復! =)我用該代碼替換了startScanning的內容,然後嘗試了選項代碼(在presentViewController行之前插入),但它似乎具有相同的問題。 我應該補充說,顯示的圖像旋轉90度以及覆蓋屏幕。我也是新來的目標c,所以我不知道用codeReader.scanner替換scanner對象的效果。我會更新原文,以反映這一切。 – SOair

+0

你確定' - (void)imagePickerController:(UIImagePickerController *)reader didFinishPickingMediaWithInfo:(NSDictionary *)info'永遠不會被調用嗎?你有一行代碼'resultImageView.image = [info objectForKey:UIImagePickerControllerOriginalImage];'在裏面。我想這會讓你的屏幕上的條形碼圖像。 – Ryan

+0

我說它沒有被調用,因爲我放入NSlog語句並且它們沒有出現在消息日誌中。這可能是我缺乏知識說話。是否有任何其他方法顯示消息/錯誤消息/異常? – SOair