廢話不說, 直接上代碼...
首先導(dǎo)入AVFoundation框架, 我們用的是蘋果自帶的二維碼掃描...
#import <AVFoundation/AVFoundation.h>
然后簽代理, 創(chuàng)建屬性
@interface ViewController ()<AVCaptureMetadataOutputObjectsDelegate> //用于處理采集信息的代理
@property (nonatomic, strong) AVCaptureSession *session;//輸入輸出的中間橋梁
@property (nonatomic, strong) AVCaptureDevice *device;//獲取攝像設(shè)備
@property (nonatomic, strong) AVCaptureDeviceInput *input;//創(chuàng)建輸入流
@property (nonatomic, strong) AVCaptureMetadataOutput *output;//創(chuàng)建輸出流
@property (nonatomic, strong) AVCaptureVideoPreviewLayer *layer;
@property (nonatomic, strong) UIView *scanRectView;
@end
在viewDidLoad里面實現(xiàn)UI效果圖
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.navigationItem.title = @"掃一掃";
//獲取攝像設(shè)備
self.device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
//創(chuàng)建輸入流
self.input = [AVCaptureDeviceInput deviceInputWithDevice:self.device error:nil];
//創(chuàng)建輸出流
self.output = [[AVCaptureMetadataOutput alloc] init];
//設(shè)置代理 在主線程里刷新
[self.output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
//初始化鏈接對象
self.session = [[AVCaptureSession alloc] init];
//高質(zhì)量采集率
[self.session setSessionPreset:AVCaptureSessionPresetHigh];
[self.session addInput:self.input];
[self.session addOutput:self.output];
//設(shè)置掃碼支持的編碼格式
self.output.metadataObjectTypes = @[AVMetadataObjectTypeQRCode];
AVCaptureVideoPreviewLayer *layer = [AVCaptureVideoPreviewLayer layerWithSession:self.session];
layer.videoGravity = AVLayerVideoGravityResizeAspectFill;
layer.frame = self.view.layer.bounds;
[self.view.layer insertSublayer:layer atIndex:0];
//掃描框
CGSize windowSize = [UIScreen mainScreen].bounds.size;
CGSize scanSize = CGSizeMake(windowSize.width * 5 / 8, windowSize.width * 5 / 8);
CGRect scanRect = CGRectMake((windowSize.width - scanSize.width) / 2, (windowSize.height - scanSize.height) / 2 - 100, scanSize.width, scanSize.height);
scanRect = CGRectMake(scanRect.origin.y / windowSize.height, scanRect.origin.x / windowSize.width, scanRect.size.height / windowSize.height,scanRect.size.width / windowSize.width);
self.output.rectOfInterest = scanRect;
scanRect = CGRectMake((windowSize.width - scanSize.width) / 2, (windowSize.height - scanSize.height) / 2 - 100, scanSize.width, scanSize.height);
self.scanRectView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"xiangkuang"]];
[self.view addSubview:self.scanRectView];
self.scanRectView.frame = scanRect;
self.scanRectView.layer.borderColor = [UIColor orangeColor].CGColor;
self.scanRectView.layer.borderWidth = 1;
//
UILabel *label = [UILabel new];
label.text = @"將二維碼/條碼放入框內(nèi),即可自動掃描";
label.frame = CGRectMake(0, self.scanRectView.frame.origin.y + self.scanRectView.frame.size.height + 10, self.view.frame.size.width, 30);
label.textAlignment = NSTextAlignmentCenter;
label.font = [UIFont systemFontOfSize:13];
label.textColor = [UIColor grayColor];
scanRect.origin.y = scanRect.origin.y + scanSize.height+5;
scanRect.size.height = 10;
[self.view addSubview:label];
//開始捕獲
[self.session startRunning];
}
#pragma mark - 實現(xiàn)代理方法, 完成二維碼掃描
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection
{
if (metadataObjects.count == 0) {
NSLog(@"%@", metadataObjects);
return;
}
if (metadataObjects.count > 0) {
//停止掃描
[self.session stopRunning];
AVMetadataMachineReadableCodeObject *metadataObject = metadataObjects.firstObject;
//掃描得到的文本 可以拿到掃描后的文本做其他操作哦
NSLog(@"%@", metadataObject.stringValue);
}
}
<a >github鏈接</a>
<a href="http://www.itdecent.cn/p/691a41303211">二維碼生成鏈接</a>
以上只是最基礎(chǔ)的實現(xiàn)方法, 更復(fù)雜的操作需要大家隨機(jī)應(yīng)變, 有問題可以問我哦~~