【问题标题】:QR and Barcode scanner code doesn't work Xcode 5.1QR 和条形码扫描仪代码不起作用 Xcode 5.1
【发布时间】:2014-07-22 00:32:52
【问题描述】:

我已经完成了这个教程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) 我选择二维码或条形码

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

我花了很多时间试图找到解决方案,但我没有找到任何提供解决方案的东西。我见过的唯一一个遇到像我这样的问题的人是在教程页面 cmets 部分,但他们从未得到答复。

请帮忙!

编辑:

显示的图像旋转 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 文件。

我不知道是否应该对此做任何其他事情,因为我只重命名了一个对象并删除了一些导致错误的代码行 - 但程序确实按照我目前期望的方式运行。如果我以后有任何问题,我会发布一个新问题。

【问题讨论】:

    标签: ios iphone objective-c zbar-sdk


    【解决方案1】:

    试试这个代码。

    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;
    

    【讨论】:

    • 感谢您的快速回复! =) 我用该代码替换了 startScanning 的内容,然后尝试了选项代码(插入到 presentViewController 行之前),但它似乎有同样的问题。我应该补充一点,显示的图像旋转了 90 度并覆盖了屏幕。另外,我是 Objective c 的新手,所以我不确定仅用 codeReader.scanner 替换扫描仪对象的效果。我将更新原始帖子以反映所有这些。
    • 您确定从未调用过-(void) imagePickerController: (UIImagePickerController*) reader didFinishPickingMediaWithInfo: (NSDictionary*) info 吗?你有一行代码resultImageView.image = [info objectForKey: UIImagePickerControllerOriginalImage];。我想这会使您的屏幕上的条形码图像。
    • 我说它没有被调用,因为我放入了 NSlog 语句并且它们没有出现在消息日志中。不过,这可能是我缺乏知识。是否有任何显示消息/错误消息/异常的替代方法?
    • 可以使用断点。你怎么知道4) It scans the barcode 没问题?如果没问题,必须调用委托方法。 huge version of the barcode on the screen 不是 resultImageView 吗?
    • 我在 didFinishPickingMediaWithInfo 的开头插入了一个断点,但是程序在应该到达的时候没有停止。在对 resultImageView 的注释中,我还尝试注释掉 didFinishPickingMediaWithInfo 的全部内容,但仍然出现了巨大的屏幕填充图像。当大透明条码出现时,我仍然看到屏幕要求我按下 2 个手指来查看照片。
    猜你喜欢
    • 2012-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多