我已完成本教程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文件。
我不知道是否应该对此做任何其他事情,因为我只重命名了一个对象并删除了一些错误的代码行 - 但程序确实按照我预期的方式运行。如果我有任何未来的问题,我会发布一个新问题。
答案 0 :(得分: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;