码迷,mamicode.com
首页 > 移动开发 > 详细

iOS之使用ZbarSDK实现扫描二维码以及条形码功能

时间:2016-04-04 14:32:08      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:

1、去下载ZbarSDK。

2、新建你的IOS Project。


3、导入ZbarSDK,直接拖动你下载的ZbarSDK到你的项目,并且选择copy选项。

4、添加如下framework

5、在appDelegate文件的以下方法中加入如下:

 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    //your code
    [ZBarReaderView class];
    return YES;
}

 

6、在你需要使用扫描功能的ViewController.h文件添加如下:

 

#import <UIKit/UIKit.h>
#import "ZBarSDK.h"
@interface ScanViewController : UIViewController<ZBarReaderViewDelegate>
{
    ZBarReaderView *readerView;
    ZBarCameraSimulator *cameraSim;
}
@property (retain, nonatomic) IBOutlet ZBarReaderView *readerView;
@end

 

7、在你需要使用扫描功能的ViewController.m文件添加如下:

 

#import "ScanViewController.h"
@interface ScanViewController ()
@end
@implementation ScanViewController
@synthesize readerView;
- (void)viewDidLoad
{
    [super viewDidLoad];    
    //your code    
    readerView.readerDelegate = self;
    [readerView setAllowsPinchZoom:YES];
    if (TARGET_IPHONE_SIMULATOR) {
        cameraSim = [[ZBarCameraSimulator alloc] initWithViewController:self];
        cameraSim.readerView = readerView;
    }
}
-(void)viewDidAppear:(BOOL)animated
{
    [readerView start];
}
-(void)viewDidDisappear:(BOOL)animated
{
    [readerView stop];
}

-(void) readerView:(ZBarReaderView *)readerView didReadSymbols:(ZBarSymbolSet *)symbols fromImage:(UIImage *)image
{
    NSString *codeData = [[NSString alloc] init];;
    for (ZBarSymbol *sym in symbols) {
        codeData = sym.data;
        break;
    }
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"掃描結果" message:codeData delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    [alert show];
}

-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    // 得到条形码结果
    id<NSFastEnumeration> results =
    [info objectForKey: ZBarReaderControllerResults];
    ZBarSymbol *symbol = nil;
    for(symbol in results)
        break;
    //获得到条形码
    //NSString *dataNum=symbol.data;
    //扫描界面退出
    [picker dismissModalViewControllerAnimated: YES];
}

 

iOS之使用ZbarSDK实现扫描二维码以及条形码功能

标签:

原文地址:http://www.cnblogs.com/chixuedong/p/5351867.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!