https://github.com/arciem/ZBarSDK 或者code4app 直接下载demo http://code4app.com/ios/%E5%88%A9%E7%94%A8ZBar%E5%AE%9E%E7%8E%B0%E4%BA%8C%E7%BB%B4%E7%A0%81%E6%89%AB%E6%8F%8F%E5%92%8C%E7%94%9F%E6%88%90/501a70f16803fa1c73000000
label什么的自己定义下
- (IBAction)button:(id)sender { /*扫描二维码部分: 导入ZBarSDK文件并引入一下框架 AVFoundation.framework CoreMedia.framework CoreVideo.framework QuartzCore.framework libiconv.dylib 引入头文件#import “ZBarSDK.h” 即可使用,注意引入ZBarReaderDelegate协议
当找到条形码时,会执行代理方法 - (void) imagePickerController: (UIImagePickerController*) reader didFinishPickingMediaWithInfo: (NSDictionary*) info 最后读取并显示了条形码的图片和内容。*/ ZBarReaderViewController *reader = [ZBarReaderViewController new]; [[reader.view.subviews objectAtIndex:1] removeFromSuperview]; for (UIView *view in reader.view.subviews) { // [view removeFromSuperview]; NSLog(@"view = %@",view); } UIView *vi = [[UIView alloc]initWithFrame:CGRectMake(300, 300, 500, 400)]; vi.backgroundColor = [UIColor redColor]; [reader.view addSubview:vi]; reader.readerDelegate = self; reader.supportedOrientationsMask = ZBarOrientationMaskAll; ZBarImageScanner *scanner = reader.scanner; [scanner setSymbology: ZBAR_I25 config: ZBAR_CFG_ENABLE to: 0]; NSLog(@"controller = %@",reader.childViewControllers); [self presentModalViewController: reader animated: YES]; [reader release]; }
- (IBAction)button2:(id)sender { /*字符转二维码 导入 libqrencode文件 引入头文件#import "QRCodeGenerator.h" 即可使用 */ imageview.image = [QRCodeGenerator qrImageForString:text.text imageSize:imageview.bounds.size.width]; } - (IBAction)Responder:(id)sender { //键盘释放 [text resignFirstResponder]; }
- (void) imagePickerController: (UIImagePickerController*) reader didFinishPickingMediaWithInfo: (NSDictionary*) info { id<NSFastEnumeration> results = [info objectForKey: ZBarReaderControllerResults]; ZBarSymbol *symbol = nil; for(symbol in results) break; imageview.image = [info objectForKey: UIImagePickerControllerOriginalImage]; [reader dismissModalViewControllerAnimated: YES]; //判断是否包含 头'http:' NSString *regex = @"http+:[^\\s]*"; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex]; //判断是否包含 头'ssid:' NSString *ssid = @"ssid+:[^\\s]*";; NSPredicate *ssidPre = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",ssid]; label.text = symbol.data ; if ([predicate evaluateWithObject:label.text]) { UIAlertView * alert = [[UIAlertView alloc]initWithTitle:nil message:@"It will use the browser to this URL。" delegate:nil cancelButtonTitle:@"Close" otherButtonTitles:@"Ok", nil]; alert.delegate = self; alert.tag=1; [alert show]; [alert release]; } else if([ssidPre evaluateWithObject:label.text]){ NSArray *arr = [label.text componentsSeparatedByString:@";"]; NSArray * arrInfoHead = [[arr objectAtIndex:0] componentsSeparatedByString:@":"]; NSArray * arrInfoFoot = [[arr objectAtIndex:1] componentsSeparatedByString:@":"]; label.text= [NSString stringWithFormat:@"ssid: %@ \n password:%@", [arrInfoHead objectAtIndex:1],[arrInfoFoot objectAtIndex:1]]; UIAlertView * alert = [[UIAlertView alloc]initWithTitle:label.text message:@"The password is copied to the clipboard , it will be redirected to the network settings interface" delegate:nil cancelButtonTitle:@"Close" otherButtonTitles:@"Ok", nil]; alert.delegate = self; alert.tag=2; [alert show]; [alert release]; UIPasteboard *pasteboard=[UIPasteboard generalPasteboard]; // 然后,可以使用如下代码来把一个字符串放置到剪贴板上: pasteboard.string = [arrInfoFoot objectAtIndex:1]; } }
在模拟器下正常运行的程序,在真机上出现下面的错误:
ld: file is universal (3 slices) but does not contain a(n) armv7s slice: /Users/david/Downloads/QR code/QR code/ZBarSDK/libzbar.a for architecture armv7s
上面的提示是使用的libzbar这个库不支持armv7s。
在Xcode里,点击相应的Target,然后点Build Settings,找到VALID_ARCHS,然后删除里面的arvm7s.
再次运行,如果成功就OK了,不成功往下看
再运行,出现新的错误:
将Build Active Architecture Only改为No
可以在真机上运行了!
- (void)QRscan { ZBarReaderViewController *reader = [ZBarReaderViewController new]; reader.readerDelegate = self; //非全屏 reader.wantsFullScreenLayout = NO; //隐藏底部控制按钮 reader.showsZBarControls = NO; //设置自己定义的界面 [self setOverlayPickerView:reader]; ZBarImageScanner *scanner = reader.scanner; [scanner setSymbology: ZBAR_I25 config: ZBAR_CFG_ENABLE to: 0]; [self presentModalViewController: reader animated: YES]; [reader release]; } - (void)setOverlayPickerView:(ZBarReaderViewController *)reader { //清除原有控件 for (UIView *temp in [reader.view subviews]) { for (UIButton *button in [temp subviews]) { if ([button isKindOfClass:[UIButton class]]) { [button removeFromSuperview]; } } for (UIToolbar *toolbar in [temp subviews]) { if ([toolbar isKindOfClass:[UIToolbar class]]) { [toolbar setHidden:YES]; [toolbar removeFromSuperview]; } } } //画中间的基准线 UIView* line = [[UIView alloc] initWithFrame:CGRectMake(40, 220, 240, 1)]; line.backgroundColor = [UIColor redColor]; [reader.view addSubview:line]; [line release]; //最上部view UIView* upView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 80)]; upView.alpha = 0.3; upView.backgroundColor = [UIColor blackColor]; [reader.view addSubview:upView]; //用于说明的label UILabel * labIntroudction= [[UILabel alloc] init]; labIntroudction.backgroundColor = [UIColor clearColor]; labIntroudction.frame=CGRectMake(15, 20, 290, 50); labIntroudction.numberOfLines=2; labIntroudction.textColor=[UIColor whiteColor]; labIntroudction.text=@"将二维码图像置于矩形方框内,离手机摄像头10CM左右,系统会自动识别。"; [upView addSubview:labIntroudction]; [labIntroudction release]; [upView release]; //左侧的view UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 80, 20, 280)]; leftView.alpha = 0.3; leftView.backgroundColor = [UIColor blackColor]; [reader.view addSubview:leftView]; [leftView release]; //右侧的view UIView *rightView = [[UIView alloc] initWithFrame:CGRectMake(300, 80, 20, 280)]; rightView.alpha = 0.3; rightView.backgroundColor = [UIColor blackColor]; [reader.view addSubview:rightView]; [rightView release]; //底部view UIView * downView = [[UIView alloc] initWithFrame:CGRectMake(0, 360, 320, 120)]; downView.alpha = 0.3; downView.backgroundColor = [UIColor blackColor]; [reader.view addSubview:downView]; [downView release]; //用于取消操作的button UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; cancelButton.alpha = 0.4; [cancelButton setFrame:CGRectMake(20, 390, 280, 40)]; [cancelButton setTitle:@"取消" forState:UIControlStateNormal]; [cancelButton.titleLabel setFont:[UIFont boldSystemFontOfSize:20]]; [cancelButton addTarget:self action:@selector(dismissOverlayView:)forControlEvents:UIControlEventTouchUpInside]; [reader.view addSubview:cancelButton]; } //取消button方法 - (void)dismissOverlayView:(id)sender{ [self dismissModalViewControllerAnimated: YES]; }