ios实现条形码扫描功能

免费的条形码是zbar,其使用地址是:http://zbar.sourceforge.net/iphone/sdkdoc/install.html

下面是实现的效果:

image image

利用条形码,可以简单的得到二维码的值.

具体的实现过程如下:

首先下载zbr的sdk。

然后,创建工程,iphone.scanbar。

第三,把下载的ZBarSDK添加到工程中。

第四,然后添加.framework到工程中。

image

-(void)buttonAction:(UIButton *)btn{
    ZBarReaderViewController *reader = [ZBarReaderViewController new];
    
    reader.readerDelegate = self;
    
    ZBarImageScanner *scanner = reader.scanner;
    
    
    
    [scanner setSymbology: ZBAR_I25
     
                   config: ZBAR_CFG_ENABLE
     
                       to: 0];
    
    
    
    [self presentModalViewController: reader  animated: YES];
    
    [reader release];
    
    
    
}


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

{
    
    
    
    NSLog(@"info=%@",info);
    NSLog(@"info is a -------------->%@",info);
    
    // 得到条形码结果
    
    id<NSFastEnumeration> results =
    
    [info objectForKey: ZBarReaderControllerResults];
    
    ZBarSymbol *symbol = nil;
    
    for(symbol in results)
        
        // EXAMPLE: just grab the first barcode
        
        break;
    
    
    
    // 将获得到条形码显示到我们的界面上
    
    NSString *str  = symbol.data;
    
    // 扫描时的图片显示到我们的界面上
    NSLog(@"str is a -------------->%@",str);
    if([str hasPrefix:@"http"]){
        NSURL *url = [NSURL URLWithString:str];
        [[UIApplication sharedApplication] openURL:url];
    }else if([str hasPrefix:@"localhost:"]){
        NSString * categoryId = [str substringFromIndex:9];
        NSDictionary *dic = [NSDictionary dictionaryWithObject:categoryId forKey:@"categoryId"];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"goIntoMgzViewNotification" object:self userInfo:dic];
    }else{
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"二维码错误,请更换二维码后重试" delegate:self cancelButtonTitle:@"我知道了" otherButtonTitles:nil, nil] ;
        [alert show];
        [alert  release];
        alert = nil;
    }

    // resultImage.image =
    
    [info objectForKey: UIImagePickerControllerOriginalImage];
    
    
    
    // 扫描界面退出
    
    [reader dismissModalViewControllerAnimated: YES];
    
    
        
}


  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值