今天为大家分享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;
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];
}
}