利用Zbar来实现iOS中扫描二维码

今天为大家分享iOS中扫描二维码,首先大家必须下载ZbarSDK,这个yinSDK我已经传到百度网盘了,这是下载链接:http://pan.baidu.com/s/1i3y2UbV

大家下载好了之后把它导入到自己的项目之中就好,首先实现扫描二维码必须得有真机才行,因为模拟器没有摄像头。。。所以测试的时候一定要真机测试哦!

首先得导入以下框架:

导入号框架之后开始写代码:

引入头文件:

#import "ZBarSDK.h"


签订代理

<ZBarReaderDelegate, UIAlertViewDelegate>


写属性:

// 显示图片

@property (nonatomic, retain) UIImageView *myImageView;

// 显示二维码与条形码

@property (nonatomic, retain) UILabel *myLabel;


@property (nonatomic, retain) UIButton *myButton;

@property (nonatomic, retain) UIAlertView *myAlertView;


这段代码在viewDidLoad中写入:

self.myImageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, 280, 300)];

    self.myImageView.backgroundColor = [UIColor redColor];

    [self.view addSubview:self.myImageView];

    [_myImageView release];

    

    

    self.myLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 340, 280, 60)];

    self.myLabel.numberOfLines = 0;

    self.myLabel.font = [UIFont systemFontOfSize:14];

    self.myLabel.backgroundColor = [UIColor whiteColor];

//    [self.view addSubview:self.myLabel];

    [_myLabel release];

    

    self.myButton = [UIButton buttonWithType:UIButtonTypeSystem];

    self.myButton.frame = CGRectMake(100, 420, 100, 40);

    [self.myButton setTitle:@"扫描" forState:UIControlStateNormal];

    [self.view addSubview:self.myButton];

    [self.myButton addTarget:self action:@selector(myButtonClicked:) forControlEvents:UIControlEventTouchUpInside];

    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];

    button.frame = CGRectMake(100, 460, 100, 40);

    [button setTitle:@"111" forState:UIControlStateNormal];

    [self.view addSubview:button];

    [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];

    


这是按钮方法:

- (void)buttonClicked:(UIButton *)button

{

    Two_dimension_code_WebViewController *webVC = [[Two_dimension_code_WebViewController alloc] init];

    // 把网址传过去

    webVC.web = self.myLabel.text;

    [self.navigationController pushViewController:webVC animated:YES];

    [webVC release];

    NSLog(@"11111");

}


- (void)myButtonClicked:(UIButton *)button

{

    // 弹出扫描页面

    ZBarReaderViewController * reader = [ZBarReaderViewController new];

    reader.readerDelegate = self;

    // 扫描图片

    ZBarImageScanner *scanner = reader.scanner;

    

    [scanner setSymbology:ZBAR_I25 config:ZBAR_CFG_ENABLE to:0];

    

    reader.showsZBarControls = YES;

    

    [self presentViewController:reader animated:YES completion:nil];

}


-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

{

    // 获取结果

    id<NSFastEnumeration> results = [info objectForKey:ZBarReaderControllerResults];

    ZBarSymbol * symbol;

    // 搜寻结果

    for(symbol in results)

        break;

    // 设置图片

    self.myImageView.image = [info objectForKey:UIImagePickerControllerOriginalImage];

    

    [picker dismissViewControllerAnimated:YES completion:nil];

    // 判断是二维码还是条形码

    if ([[symbol.data substringToIndex:4] isEqualToString:@"http"]) {

        

        self.myAlertView = [[UIAlertView alloc] initWithTitle:@"呵呵" message:symbol.data delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"前往", nil];

        [self.myAlertView show];

        [_myAlertView release];

    } else {

        

        self.myAlertView = [[UIAlertView alloc] initWithTitle:@"呵呵" message:symbol.data delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];

        [self.myAlertView show];

        [_myAlertView release];

    }

    

    self.myLabel.text = symbol.data;

}


- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

{

    if (buttonIndex == 0) {

//        [self.picker dismissViewControllerAnimated:YES completion:nil];

    } else {

        

        Two_dimension_code_WebViewController *webVC = [[Two_dimension_code_WebViewController alloc] init];

        webVC.web = self.myLabel.text;

        [self.navigationController pushViewController:webVC animated:YES];

        [webVC release];

    }

}


这些代码中不但有二维码的扫描,还有条形码的扫描,其中二维码的扫描,会有提醒窗口弹出,点前往按钮就会进入二维码的内容,跳转之后是一个webView来体现二维码的主要内容这里就是传入一个网址到下个界面,那些代码就不写了,这是主要的代码,都有注释,谢谢大家!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值