具体的能够参考这篇文章:http://www.appcoda.com/qr-code-ios-programming-tutorial/ios
最近因为公司项目须要,我负责二维码的生成和扫描部分。因为苹果规定自2015/02/01后上架的做品必须支持64位,因此通过综合比较后,二维码的扫描部分我决定采用苹果自带的AVFoundation框架来实现扫描,网上有不少关于ZBar和Zxing的例子,有兴趣的朋友能够去研究下。session
1.使用前先加入框架头文件和代理信息,以下:app
#import <AVFoundation/AVFoundation.h> @interface QRCodeViewController : UIViewController<AVCaptureMetadataOutputObjectsDelegate>
2.用到以下几个类框架
@property (strong,nonatomic) AVCaptureDevice *device; @property (strong,nonatomic) AVCaptureDeviceInput *input; @property (strong,nonatomic) AVCaptureMetadataOutput *output; @property (strong,nonatomic) AVCaptureSession *session; @property (strong,nonatomic) AVCaptureVideoPreviewLayer *preview;
3.具体用法以下ide
//Device device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; //Input input = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil]; //判断是否有输入 if (!input) { NSLog(@"error info:%@",[error localizedDescription]); return NO; } //Session session = [[AVCaptureSession alloc] init]; [session setSessionPreset:AVCaptureSessionPresetHigh]; [session addInput:input]; //Output output = [[AVCaptureMetadataOutput alloc] init]; [session addOutput:output]; [output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()]; //条码类型 [output setMetadataObjectTypes:@[AVMetadataObjectTypeQRCode]]; //preview,扫描区域 preview = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session]; [preview setVideoGravity:AVLayerVideoGravityResizeAspectFill]; //设置扫描区域 [preview setFrame:frameView.layer.bounds]; //将扫描view放在self.frameView上 [self.frameView.layer addSublayer:preview]; //Start [session startRunning];
4.扫描到二维码后的操做,扫描到二维码以后就会调用以下方法atom
-(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection { NSString *stringValue; if (metadataObjects !=nil && [metadataObjects count] >0) { AVMetadataMachineReadableCodeObject *metadataObject = [metadataObjects objectAtIndex:0]; //判断取到的对象类型 if ([[metadataObject type] isEqualToString:AVMetadataObjectTypeQRCode]) { [scanResult performSelectorOnMainThread:@selector(setText:) withObject:[metadataObject stringValue] waitUntilDone:NO]; stringValue = [metadataObject stringValue]; NSLog(@"扫描结果:%@",stringValue); // //直接跳往处理二维码controller // ProcessRQViewController *processRQViewController = [[ProcessRQViewController alloc] init]; // //将取到的二维码的值传过去 // processRQViewController.RQResult = stringValue; // //[self.navigationController pushViewController:processRQViewController animated:true]; // [self presentViewController:processRQViewController animated:YES completion:NULL]; // //直接跳往下载图片的页面 // ProcessImageViewController *processImageViewController = [[ProcessImageViewController alloc] init]; // //将取到的二维码的值传过去 // processImageViewController.ImageResult = stringValue; // //跳转 // [self presentViewController:processImageViewController animated:YES completion:NULL]; }
5.至此结束spa
本人亲测,好多二维码均可以扫描出来。可是在2月13号这天,项目经理让我用这个app扫描本公司营业执照二维码的时候,只能扫出营业执照前面的数字(本公司的营业执照:数字(一连串)+空一格+汉字(好几个)+空一格+一个链接地址)。找出bug后来不及修改,就直接过年了,现在过年来了,要及时堵住这个bug了。代理