簡體   English   中英

QR和條形碼掃描儀代碼不起作用Xcode 5.1

[英]QR and Barcode scanner code doesn't work Xcode 5.1

我已經完成了本教程http://rdcworld-iphone.blogspot.com.au/2013/03/how-to-use-barcode-scanner-br-and-qr-in.html

我在IOS SDK 7.1,Xcode 5.1.1,Mac OSX 10.9.4和目標c上使用iPhone模擬器。 運行該程序時,應該發生以下情況:

1)我點擊第一次出現的窗口上的掃描按鈕

2)我可以選擇要在iPhone相冊中掃描的圖像

3)我選擇一個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小時后我無法回答自己的問題,因此發現了以下內容:

在這里閱讀有關ZBarReaderViewController的ZBarSDK API參考: 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文件。

我不知道是否應該對此做任何其他事情,因為我只重命名了一個對象並刪除了一些會出錯的代碼行,但是程序確實按照我目前的預期運行。 如果以后有任何問題,我將發布一個新問題。

試試這個代碼。

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;

暫無
暫無

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

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