首页 > 代码库 > 实现扫描 一/二维码 -- ZBar开源SDK
实现扫描 一/二维码 -- ZBar开源SDK
一、下载ZBarSDK, 加入到工程中。
二、配置TARGETS和PROJECT
三、设置Link Binary With Libraries
四、 .m文件
#import "MQMainViewController.h"#import "ZBarSDK.h"@interface MQMainViewController ()@end@implementation MQMainViewController- (void)viewDidLoad{ [super viewDidLoad]; [self addCaptureButton]; }#pragma mark - add btn- (void)addCaptureButton{ CGRect frame = CGRectMake(0, 0, 100, 100); UIButton * btn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; btn.frame = frame; [btn setTitle:@"拍照" forState:UIControlStateNormal]; btn.backgroundColor = [UIColor clearColor]; btn.tag = 1111; [btn addTarget:self action:@selector(onClickCaptureButton:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btn];}-(IBAction)onClickCaptureButton:(id)sender{ [self takePicture];}- (void)takePicture{ ZBarReaderViewController * reader = [ZBarReaderViewController new]; reader.readerDelegate = self; reader.supportedOrientationsMask = ZBarOrientationMaskAll; reader.wantsFullScreenLayout = NO; //非全屏 reader.showsZBarControls = NO; //隐藏底部控制按钮 [self setOverlayPickerView:reader]; //设置自己定义的界面 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)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, 220)]; 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]; }- (void) imagePickerController:(UIImagePickerController*) reader didFinishPickingMediaWithInfo:(NSDictionary*) info{ id<NSFastEnumeration> results = [info objectForKey:ZBarReaderControllerResults]; ZBarSymbol * symbol; for (symbol in results) { break; } UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"解码内容" message:symbol.data delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil]; [alertView show]; }//取消button方法- (void)dismissOverlayView:(id)sender{ [self dismissModalViewControllerAnimated: YES];}@end
五、 总结
以上程序,在IPHONE4 (IOS7) IPHONE5(IOS6) 测试, 可以扫码一/二维码。
六、 记录
在网上浏览,ZXing也是可以扫码,二维码功能。
测试,扫码二维码的速度,视乎比ZBar要快!
改局部代码后,也可扫码一维码,但,识别出的信息,不够准确!
尝试,在XCODE上配置,也挺费劲~ 待续吧 ...
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。