繁体   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