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

摘要:
下载地址为:http://zbar.sourceforge.net/iphone/index.html2.创建新项目。

版权声明:本文为博主原创文章,未经博主允许不得转载。

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

2.新建一个项目。

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

4.在项目文件的target中加入 以下framework
使用ZBar来读取条形码和二维码的方法第1张

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

[cpp] view plain copy
 
  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.       
  9.       
  10.     // force view class to load so it may be referenced directly from NIB  
  11.     [ZBarReaderView class];  
  12.       
  13.       
  14.     return YES;  
  15. }  

注意此代码:

[cpp] view plain copy
 
  1. // force view class to load so it may be referenced directly from NIB  
  2.  [ZBarReaderView class];  
6.在.h文件中加入   ZBarReaderViewDelegate的实现,代码如下:
[cpp] view plain copy
 
  1. //  
  2. //  ViewController.h  
  3. //  FootSafety  
  4. //  
  5. //  Created by 泽宇 徐 on 12-6-12.  
  6. //  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.  
  7. //  
  8.   
  9. #import <UIKit/UIKit.h>  
  10. #import "ZBarSDK.h"  
  11.   
  12. @interface ViewController : UIViewController<ZBarReaderViewDelegate>  
  13. {  
  14.     IBOutlet UILabel  * label ;  
  15.     ZBarReaderView *readerView;  
  16.     ZBarCameraSimulator *cameraSim;  
  17. }  
  18.   
  19. @property(nonatomic,retain) UILabel * label ;  
  20. @property (nonatomic, retain) IBOutlet ZBarReaderView *readerView;  
  21.   
  22.   
  23. @end  

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

[html] view plain copy
 
  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] view plain copy
 
  1. -(void) viewDidAppear:(BOOL)animated  
  2. {  
  3.     // run the reader when the view is visible  
  4.     [readerView start];  
  5. }  

这个是在显示视图的时候,启动摄像头,开始扫描
[html] view plain copy
 
  1. - (void)viewDidLoad  
  2. {  
  3.     [super viewDidLoad];  
  4.     // Do any additional setup after loading the view, typically from a nib.  
  5.       
  6.     // the delegate receives decode results  
  7.     readerView.readerDelegate = self;  
  8.       
  9.     // you can use this to support the simulator  
  10.     if(TARGET_IPHONE_SIMULATOR) {  
  11.         cameraSim = [[ZBarCameraSimulator alloc]  
  12.                      initWithViewController: self];  
  13.         cameraSim.readerView = readerView;  
  14.     }  
  15.   
  16. }  

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

.m文件所有内容是:

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

在ViewController.xib文件中 增加一个view,并且修改view的类是ZBarReaderView ,并且指向 .h文件中定义的 
[cpp] view plain copy
 
  1. ZBarReaderView *readerView;  
版权声明:本文为博主原创文章,未经博主允许不得转载。

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


2.新建一个项目。

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

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


5.在appDelegate文件中加入 标记部分的代码
[cpp] view plain copy
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions  
{  
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];  
    // Override point for customization after application launch.  
    self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease];  
    self.window.rootViewController = self.viewController;  
    [self.window makeKeyAndVisible];  
      
      
    // force view class to load so it may be referenced directly from NIB  
    [ZBarReaderView class];  
      
      
    return YES;  
}  
注意此代码:

[cpp] view plain copy
// force view class to load so it may be referenced directly from NIB  
 [ZBarReaderView class];  

6.在.h文件中加入   ZBarReaderViewDelegate的实现,代码如下:
[cpp] view plain copy
//  
//  ViewController.h  
//  FootSafety  
//  
//  Created by 泽宇 徐 on 12-6-12.  
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.  
//  
  
#import <UIKit/UIKit.h>  
#import "ZBarSDK.h"  
  
@interface ViewController : UIViewController<ZBarReaderViewDelegate>  
{  
    IBOutlet UILabel  * label ;  
    ZBarReaderView *readerView;  
    ZBarCameraSimulator *cameraSim;  
}  
  
@property(nonatomic,retain) UILabel * label ;  
@property (nonatomic, retain) IBOutlet ZBarReaderView *readerView;  
  
  
@end  

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

[html] view plain copy
- (void) readerView: (ZBarReaderView*) view  
     didReadSymbols: (ZBarSymbolSet*) syms  
          fromImage: (UIImage*) img  
{  
    // do something useful with results  
    for(ZBarSymbol *sym in syms) {  
        self.label.text = sym.data;  
        break;  
    }  
}  

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

[html] view plain copy
-(void) viewDidAppear:(BOOL)animated  
{  
    // run the reader when the view is visible  
    [readerView start];  
}  

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

[html] view plain copy
- (void)viewDidLoad  
{  
    [super viewDidLoad];  
    // Do any additional setup after loading the view, typically from a nib.  
      
    // the delegate receives decode results  
    readerView.readerDelegate = self;  
      
    // you can use this to support the simulator  
    if(TARGET_IPHONE_SIMULATOR) {  
        cameraSim = [[ZBarCameraSimulator alloc]  
                     initWithViewController: self];  
        cameraSim.readerView = readerView;  
    }  
  
}  

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

.m文件所有内容是:
[html] view plain copy
//  
//  ViewController.m  
//  FootSafety  
//  
//  Created by 泽宇 徐 on 12-6-12.  
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.  
//  
  
#import "ViewController.h"  
  
@interface ViewController ()  
  
@end  
  
@implementation ViewController  
@synthesize label;  
@synthesize readerView;  
  
- (void)viewDidLoad  
{  
    [super viewDidLoad];  
    // Do any additional setup after loading the view, typically from a nib.  
      
    // the delegate receives decode results  
    readerView.readerDelegate = self;  
      
    // you can use this to support the simulator  
    if(TARGET_IPHONE_SIMULATOR) {  
        cameraSim = [[ZBarCameraSimulator alloc]  
                     initWithViewController: self];  
        cameraSim.readerView = readerView;  
    }  
  
}  
  
-(void) viewDidAppear:(BOOL)animated  
{  
    // run the reader when the view is visible  
    [readerView start];  
}  
  
  
- (void) readerView: (ZBarReaderView*) view  
     didReadSymbols: (ZBarSymbolSet*) syms  
          fromImage: (UIImage*) img  
{  
    // do something useful with results  
    for(ZBarSymbol *sym in syms) {  
        self.label.text = sym.data;  
        break;  
    }  
}  
  
  
  
- (void)viewDidUnload  
{  
    [super viewDidUnload];  
    // Release any retained subviews of the main view.  
}  
  
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation  
{  
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);  
}  
  
-(void) dealloc  
{  
    [self.readerView release];  
    [self.label release];  
    [super dealloc];  
}  
  
@end  

在ViewController.xib文件中 增加一个view,并且修改view的类是ZBarReaderView ,并且指向 .h文件中定义的 
[cpp] view plain copy
ZBarReaderView *readerView;  

免责声明:文章转载自《使用ZBar来读取条形码和二维码的方法》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇SpringBoot与SpringCloud对应版本及官方查询方法photometric_stereo halcon光度立体法三维表面重建下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

zbar配置

1、下载安装zbar,选择full全部安装。也可以从这里下载。  2、由于官方给的是32位的,继续下载64位的zbar 替换文件: 将ZBarWin64-master\lib 文件夹下 libzbar64-0.lib 文件复制到ZBar安装路径下的 lib 文件夹; 将 ZBarWin64-master\lib 文件夹下 libzbar64-0.dl...

Qt---ToolBox自由伸展

参考:Qt实战12.可自由展开的ToolBox - Qt小罗 - 博客园 (cnblogs.com)   1 MainWindow::MainWindow(QWidget *parent) 2 : QMainWindow(parent) 3 , ui(new Ui::MainWindow) 4 { 5 ui->set...

uboot的relocation原理详细分析

转自:http://blog.csdn.net/skyflying2012/article/details/37660265 最近在一直在做uboot的移植工作,uboot中有很多值得学习的东西,之前总结过uboot的启动流程,但uboot一个非常核心的功能没有仔细研究,就是uboot的relocation功能。 这几天研究下uboot的reloca...

k8s全方位监控-prometheus-alertmanager部署-配置第一条告警邮件

1、alertmanager告警插件部署 [root@VM_0_48_centos prometheus]# cat alertmanager-pvc.yaml apiVersion: v1 kind: PersistentVolumeClaim metadata: name: alertmanager namespace: kube-syst...

WebRTC学习(三)WebRTC设备管理

一:WebRTC设备管理 (一)重要API,用于获取音视频设备 其中返回的ePromise结果,是一个Promise对象。 Promise对象的结构体:MediaDevicesInfo  deviceID:是设备唯一标识符ID label:是设备的名字(可读的) kind:设备的种类(音频输入/输出两种,视频输入/输出两类) groupID:同一个设备...

vue实现筛选功能,文字选中变色

1 <template> 2 <Poptip trigger="hover"title="Title"content="content"> 3 <i-button>Hover</i-button> 4 </Poptip> 5 <Poptip tri...