在实际开发中,最方便的还是直接使用第三方如SYQRCodeViewController 和QRCodeReaderViewController。
ZBar
ZBar和ZXing都可以实现扫描二维码,后者更可以生成二维码,但两者都已停止更新维护,并且iOS 8.0后系统也有了自己的二维码生成的扫描。所以第三方就略显尴尬,事实上原生API也是效率最高的。
#import <ZBarSDK.h>
@interface ViewController ()<ZBarReaderViewDelegate>
{
ZBarReaderView *readerView;
}
@end
//当然先要获得获得授权并判断相机是否可用
- (IBAction)gogoAction:(UIButton *)sender {
//初始化照相机窗口
readerView = [[ZBarReaderView alloc]init];
//设置代理
readerView.readerDelegate = self;
//自定义大小
//readerView.frame = CGRectMake(100, 100, 300, 300);
//设置扫描区域,标准坐标。
//如果要在readerView上添加控件最好再自定义一个透明view
readerView.frame=self.view.bounds;
self.readerView.scanCrop= CGRectMake(0.25,0.3,0.5,0.5)
readerView.center = self.view.center;
readerview.backgroundColor = [UIColor clearColor];
readerview.torchMode = NO;//关闭闪光灯
readerView.allowsPinchZoom = YES;//允许手势变焦
readerViewshowsFPS = YES;//显示帧率
//添加到父视图
[self.view addSubview:readerView];
//二维码/条形码识别设置
ZBarImageScanner *scanner = reader.scanner;
[scanner setSymbology:ZBAR_I25
config:ZBAR_CFG_ENABLE
to:0];
//必须启动
[ readerView start];
}
/*********ZBarReaderViewDelegate***********/
//扫描开始
-(void)readerViewDidStart:(ZBarReaderView *)readerView{
readerView.hidden = NO;
}
//扫描停止
- (void) readerView: (ZBarReaderView*) readerView didStopWithError: (NSError*) error{
readerView.hidden = YES;
}
//扫描到二维码
- (void) readerView: (ZBarReaderView*) readerView didReadSymbols: (ZBarSymbolSet*) symbols fromImage: (UIImage*) image{
//对扫描到的二维码进行解析
ZBarSymbol *symbol = nil;
for (symbol in symbols) {
break;
}
NSString *text = symbol.data;
NSLog(@"%@",text);
//不停止的话会连续扫描
[readerView stop];
}
AVFoundation
- 导入库文件
AVFoundatio.framework CoreImage.framework - 代码示例
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
#import <CoreImage/CoreImage.h>
@interface ViewController()<AVCaptureMetadataOutputObjectsDelegate>
//捕获会话
@property (nonatomic,strong) AVCaptureSession *session;
//预览图层,可以通过输出设备展示被捕获的数据流。
@property (nonatomic,strong) AVCaptureVideoPreviewLayer *previewLayer;
@end
@implementation ViewController
- (IBAction)scanAction:(UIButton *)sender {
//实例化拍摄设备
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
//设置输出设备
NSError *error = nil;
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
if (error) return;//没有照相机
//实例化拍摄元数据输出
AVCaptureMetadataOutput *output=[[AVCaptureMetadataOutput alloc]init];
//设置输出数据代理
[output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
//实例化拍摄会话
AVCaptureSession *session =[[AVCaptureSession alloc]init];
[session setSessionPreset:AVCaptureSessionPresetHigh];//预设输出质量
//添加会话输入
[session addInput:input];
//添加会话输出
[session addOutput:output];
//添加会话输出条码类型
[output setMetadataObjectTypes:@[AVMetadataObjectTypeQRCode]];
self.session = session;
//实例化预览图层
AVCaptureVideoPreviewLayer *preview = [AVCaptureVideoPreviewLayer layerWithSession:_session];
preview.videoGravity = AVLayerVideoGravityResizeAspectFill;
//preview.frame = CGRectMake(0, 0, 300, 300);
preview.frame = [UIScreen mainScreen].bounds;
//将图层插入当前视图
[self.view.layer insertSublayer:preview atIndex:100];
self.previewLayer = preview;
//启动会话
[_session startRunning];
}
//AVCaptureMetadataOutputObjectsDelegate
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection{
// 会频繁的扫描,调用代理方法
// 1. 如果扫描完成,停止会话
[self.session stopRunning];
// 2. 删除预览图层
[self.previewLayer removeFromSuperlayer];
// 3. 设置界面显示扫描结果
//判断是否有数据
NSString *str = nil;
if (metadataObjects.count > 0) {
AVMetadataMachineReadableCodeObject *obj = metadataObjects[0];
//如果需要对url或者名片等信息进行扫描,可以在此进行扩展
str = obj.stringValue;
NSLog(@"%@",str);
}
//结束扫描
[self dismissViewControllerAnimated:NO
completion:^{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:str preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
[alert addAction:action];
[self presentViewController:alert animated:YES completion:nil];
}];
}