您的位置:首页 > 移动开发 > IOS开发

ios实现条形码扫描功能

2012-04-05 14:47 477 查看
免费的条形码是zbar,其使用地址是:http://zbar.sourceforge.net/iphone/sdkdoc/install.html

下面是实现的效果:







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

具体的实现过程如下:

首先下载zbr的sdk。

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

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

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




-(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];

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: