7.0以后出来原生的二维码扫描之前一直使用ZBar和ZXing,而苹果又要求现在提交的应用必须支持64位编程,在网上搜了了一下ZBar的64位编程,虽然有解决64位编程办法但本人一直没有解决好,就打算放弃使用ZBar,开始使用原生的,查阅了一些相关文章,感觉有点乱,我就把自己写的代码整理了一下贴出来,希望对需要的人有所帮助
1.首先在工程里添加AVFoundation.framework框架
2.在.m文件中导入文件
#import <AVFoundation/AVFoundation.h>
3.添加两个属性,代理是后添加的
@interface ScanViewController ()<AVCaptureMetadataOutputObjectsDelegate>
@property (nonatomic,strong)AVCaptureSession *captureSession;
@property (nonatomic,strong)AVCaptureVideoPreviewLayer *preView;
@end
4.扫描代码
NSError *error;
//1.初始化捕捉设备(AVCaptureDevice),类型为AVMediaTypeVideo
AVCaptureDevice *caputreDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
//2.用captureDevice创建输入流
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:caputreDevice error:&error];
//3.创建媒体数据输出流
AVCaptureMetadataOutput *output = [[AVCaptureMetadataOutput alloc] init];
//3.1设置代理,然后添加代理协议
[output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
//4.实例化捕捉会话
self.captureSession = [[AVCaptureSession alloc] init];
//实现高质量的输出和摄像,默认值为AVCaptureSessionPresetHigh,可以不写
self.captureSession.sessionPreset = AVCaptureSessionPresetHigh;
//4.1将输入流添加到会话
if([self.captureSession canAddInput:input]){
[self.captureSession addInput:input];
}
//4.2将输出流添加到会话中
if([self.captureSession canAddOutput:output]){
[self.captureSession addOutput:output];
}
//5设置输出媒体数据类型为QRCode
output.metadataObjectTypes = @[AVMetadataObjectTypeQRCode];
//6.实例化预览图层
self.preView = [AVCaptureVideoPreviewLayer layerWithSession:self.captureSession];
//6.1设置填充方式
self.preView.videoGravity = AVLayerVideoGravityResizeAspectFill;
self.preView.frame = self.view.layer.bounds;
[self.view.layer insertSublayer:self.preView atIndex:0];
//7.添加扫描框
UIView *boxView = [[UIView alloc] initWithFrame:CGRectMake((320-200)/2.0,(568-64-200)/2.0+64,200,200)];
boxView.backgroundColor = [UIColor clearColor];
boxView.layer.borderColor = [UIColor greenColor].CGColor;
boxView.layer.borderWidth = 1.0f;
[self.view addSubview:boxView];
//8.设置扫描区域
output.rectOfInterest = CGRectMake(boxView.top/568.0, boxView.left/320.0, boxView.height/568.0, boxView.width/320.0);
//9.1设置扫描区域顶部透明图层
UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, 64, 320, boxView.top-64)];
topView.backgroundColor = [UIColor blackColor];
topView.alpha = 0.4;
[self.view addSubview:topView];
//9.2设置扫描区域底部透明图层
UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, boxView.bottom, 320, 568-boxView.bottom)];
bottomView.backgroundColor = [UIColor blackColor];
bottomView.alpha = 0.4;
[self.view addSubview:bottomView];
//9.3设置扫描区域左部透明图层
UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, boxView.top, boxView.left, boxView.height)];
leftView.backgroundColor = [UIColor blackColor];
leftView.alpha = 0.4;
[self.view addSubview:leftView];
//9.4设置扫描区域右部透明图层
UIView *rightView = [[UIView alloc] initWithFrame:CGRectMake(boxView.right, boxView.top, 320-boxView.right, boxView.height)];
rightView.backgroundColor = [UIColor blackColor];
rightView.alpha = 0.4;
[self.view addSubview:rightView];
//开始扫描
[self.captureSession startRunning];
5.代理方法内部处理
#pragma mark - AVCaptureMetadataOutputObjectsDelegate
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection {
if(metadataObjects.count > 0){
//停止扫描
[self.captureSession stopRunning];
AVMetadataMachineReadableCodeObject *metadataObject = metadataObjects[0];
//获取扫描结果
NSString *stringValue = metadataObject.stringValue;
}
}
到此就可以实现二维码扫描了,速度很快!