使用ZBar来读取条形码和二维码的方法
2013-07-17 23:52
387 查看
1.使用ZBar项目。下载地址是: http://zbar.sourceforge.net/iphone/index.html
2.新建一个项目。
3.导入 ZBar的sdk。把ZBar SDK的目录拉入项目,然后选中copy选项
4.在项目文件的target中加入 以下framework
5.在appDelegate文件中加入 标记部分的代码
[cpp] view
plaincopy
- (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
plaincopy
// force view class to load so it may be referenced directly from NIB
[ZBarReaderView class];
6.在.h文件中加入 ZBarReaderViewDelegate的实现,代码如下:
[cpp] view
plaincopy
//
// 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
plaincopy
- (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
plaincopy
-(void) viewDidAppear:(BOOL)animated
{
// run the reader when the view is visible
[readerView start];
}
这个是在显示视图的时候,启动摄像头,开始扫描
[html] view
plaincopy
- (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
plaincopy
//
// 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
2.新建一个项目。
3.导入 ZBar的sdk。把ZBar SDK的目录拉入项目,然后选中copy选项
4.在项目文件的target中加入 以下framework
5.在appDelegate文件中加入 标记部分的代码
[cpp] view
plaincopy
- (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
plaincopy
// force view class to load so it may be referenced directly from NIB
[ZBarReaderView class];
6.在.h文件中加入 ZBarReaderViewDelegate的实现,代码如下:
[cpp] view
plaincopy
//
// 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
plaincopy
- (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
plaincopy
-(void) viewDidAppear:(BOOL)animated
{
// run the reader when the view is visible
[readerView start];
}
这个是在显示视图的时候,启动摄像头,开始扫描
[html] view
plaincopy
- (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
plaincopy
//
// 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
相关文章推荐
- 使用ZBar来读取条形码和二维码的方法
- 使用ZBar来读取条形码和二维码的方法
- 使用ZBar来读取条形码和二维码的方法
- 使用ZBar来读取条形码和二维码的方法
- 使用ZBar来读取条形码和二维码的方法
- 使用ZBar来读取条形码和二维码的方法
- ios 使用ZBar读取条形码和二维码
- ios 使用ZBar读取条形码和二维码
- Qt和Zbar的结合使用识别二维码条形码
- ZBarSDK 来读取条形码和二维码的方法
- ZBar扫描二维码第三方的使用方法
- 在iOS中使用ZBar扫描二维码和条形码
- ios使用AVFoundation读取二维码的方法
- 在使用zbar这个开源库时从相册读取二维码
- 使用zbar 解码二维码 条形码
- 在iOS中使用ZBar扫描二维码和条形码
- iOS扫描二维码条形码ZBar的简单使用
- Android上使用ZXing识别条形码与二维码的方法
- Qt和Zbar的结合使用识别二维码条形码
- ios-使用ZBar来扫描二维码/条形码