【转】使用ZBar来读取条形码和二维码的方法

1.使用ZBar项目。下载地址是: http://zbar.sourceforge.net/iphone/index.html

2.新建一个项目。

3.导入 ZBar的sdk。把ZBar SDK的目录拉入项目,然后选中copy选项

4.在项目文件的target中加入 以下framework

5.在appDelegate文件中加入 标记部分的代码

[cpp]
  1. – (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  2. {
  3.     self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
  4.     // Override point for customization after application launch.
  5.     self.viewController = [[[ViewController alloc] initWithNibName:@“ViewController” bundle:nil] autorelease];
  6.     self.window.rootViewController = self.viewController;
  7.     [self.window makeKeyAndVisible];
  8.     // force view class to load so it may be referenced directly from NIB
  9.     [ZBarReaderView class];
  10.     return YES;
  11. }

注意此代码:

[cpp]
  1. // force view class to load so it may be referenced directly from NIB
  2.  [ZBarReaderView class];

6.在.h文件中加入   ZBarReaderViewDelegate的实现,代码如下:

[cpp]
  1. //
  2. //  ViewController.h
  3. //  FootSafety
  4. //
  5. //  Created by 泽宇 徐 on 12-6-12.
  6. //  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
  7. //
  8. #import <UIKit/UIKit.h>
  9. #import “ZBarSDK.h”
  10. @interface ViewController : UIViewController<ZBarReaderViewDelegate>
  11. {
  12.     IBOutlet UILabel  * label ;
  13.     ZBarReaderView *readerView;
  14.     ZBarCameraSimulator *cameraSim;
  15. }
  16. @property(nonatomic,retain) UILabel * label ;
  17. @property (nonatomic, retain) IBOutlet ZBarReaderView *readerView;
  18. @end

7.在.m文件中要实现的主要方法是:

[html]
  1. – (void) readerView: (ZBarReaderView*) view
  2.      didReadSymbols: (ZBarSymbolSet*) syms
  3.           fromImage: (UIImage*) img
  4. {
  5.     // do something useful with results
  6.     for(ZBarSymbol *sym in syms) {
  7.         self.label.text = sym.data;
  8.         break;
  9.     }
  10. }

这里是功能是读取照片信息,把条码放如label显示

[html]
  1. -(void) viewDidAppear:(BOOL)animated
  2. {
  3.     // run the reader when the view is visible
  4.     [readerView start];
  5. }

这个是在显示视图的时候,启动摄像头,开始扫描

[html]
  1. – (void)viewDidLoad
  2. {
  3.     [super viewDidLoad];
  4.     // Do any additional setup after loading the view, typically from a nib.
  5.     // the delegate receives decode results
  6.     readerView.readerDelegate = self;
  7.     // you can use this to support the simulator
  8.     if(TARGET_IPHONE_SIMULATOR) {
  9.         cameraSim = [[ZBarCameraSimulator alloc]
  10.                      initWithViewController: self];
  11.         cameraSim.readerView = readerView;
  12.     }
  13. }

在初始化的时候,设置托管。

.m文件所有内容是:

[html]
  1. //
  2. //  ViewController.m
  3. //  FootSafety
  4. //
  5. //  Created by 泽宇 徐 on 12-6-12.
  6. //  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
  7. //
  8. #import “ViewController.h”
  9. @interface ViewController ()
  10. @end
  11. @implementation ViewController
  12. @synthesize label;
  13. @synthesize readerView;
  14. – (void)viewDidLoad
  15. {
  16.     [super viewDidLoad];
  17.     // Do any additional setup after loading the view, typically from a nib.
  18.     // the delegate receives decode results
  19.     readerView.readerDelegate = self;
  20.     // you can use this to support the simulator
  21.     if(TARGET_IPHONE_SIMULATOR) {
  22.         cameraSim = [[ZBarCameraSimulator alloc]
  23.                      initWithViewController: self];
  24.         cameraSim.readerView = readerView;
  25.     }
  26. }
  27. -(void) viewDidAppear:(BOOL)animated
  28. {
  29.     // run the reader when the view is visible
  30.     [readerView start];
  31. }
  32. – (void) readerView: (ZBarReaderView*) view
  33.      didReadSymbols: (ZBarSymbolSet*) syms
  34.           fromImage: (UIImage*) img
  35. {
  36.     // do something useful with results
  37.     for(ZBarSymbol *sym in syms) {
  38.         self.label.text = sym.data;
  39.         break;
  40.     }
  41. }
  42. – (void)viewDidUnload
  43. {
  44.     [super viewDidUnload];
  45.     // Release any retained subviews of the main view.
  46. }
  47. – (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
  48. {
  49.     return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
  50. }
  51. -(void) dealloc
  52. {
  53.     [self.readerView release];
  54.     [self.label release];
  55.     [super dealloc];
  56. }
  57. @end

在ViewController.xib文件中 增加一个view,并且修改view的类是ZBarReaderView ,并且指向 .h文件中定义的

[cpp]
  1. ZBarReaderView *readerView;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值