此文均在 .m中
#import <AVFoundation/AVFoundation.h>
#import "QRCodeReaderViewController.h"
@interface ViewController ()<QRCodeReaderDelegate,AVCaptureMetadataOutputObjectsDelegate>//设置代理
{
}
@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;//扫描框
@end
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor = [UIColor yellowColor];
// [self scanBarCode];// 方法一:利用系统的 AVFoundation 库
[self QRCodeReader];//方法二:使用 QRCodeReaderViewController 第三方库
}
- (void)scanBarCode
// 取到摄像头设备
_device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
// 初始化输入源
_input = [AVCaptureDeviceInput deviceInputWithDevice:self.device error:nil];
//初始化输出源
_output = [[AVCaptureMetadataOutput alloc]init];
[_output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
// 初始化会话
_session = [[AVCaptureSession alloc]init];
//设置为高清摄像头
[_session setSessionPreset:AVCaptureSessionPresetHigh];
if ([_session canAddInput:self.input]){
[_session addInput:self.input];
}
if ([_session canAddOutput:self.output]){
[_session addOutput:self.output];
}
if ([[_output availableMetadataObjectTypes] containsObject:AVMetadataObjectTypeQRCode] || [[_output availableMetadataObjectTypes] containsObject:AVMetadataObjectTypeEAN13Code]){
[_output setMetadataObjectTypes:@[AVMetadataObjectTypeQRCode,AVMetadataObjectTypeEAN13Code]];
}
//设置输出扫描结果 二维码类型 AVMetadataObjectTypeQRCode,条码类型AVMetadataObjectTypeEAN13Code
// _output.metadataObjectTypes =@[AVMetadataObjectTypeQRCode,AVMetadataObjectTypeEAN13Code];
_preview = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.session];
[_preview setVideoGravity:AVLayerVideoGravityResizeAspectFill];
[_preview setFrame:self.view.layer.bounds];
[self.view.layer addSublayer:_preview];
// 扫码框
// _preview =[AVCaptureVideoPreviewLayer layerWithSession:self.session];
// 设置扫码框为缩放
// _preview.videoGravity = AVLayerVideoGravityResizeAspectFill;
//开始扫描
[_session startRunning];
}
//系统AVFoundation 扫码结果回调
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection
{
NSString *stringValue;
if ([metadataObjects count] >0)
{
AVMetadataMachineReadableCodeObject * metadataObject = [metadataObjects objectAtIndex:0];
stringValue = metadataObject.stringValue;
NSLog(@"扫码结果回调 =%@",stringValue);
}
//停止扫描
[_session stopRunning];
}
- (void)QRCodeReader
{
QRCodeReaderViewController *QR = [[QRCodeReaderViewController alloc]init];
[QR.codeReader startScanning];
QR.delegate = self;
[self.navigationController pushViewController:QR animated:YES];
// 扫码完成后回调 resultAsString
[QR setCompletionWithBlock:^(NSString *resultAsString) {
NSLog(@"resultAsString == %@",resultAsString);
}];
}
//扫码结果回调
- (void)reader:(QRCodeReaderViewController *)reader didScanResult:(NSString *)result
{
NSLog(@"------ 代理 扫码结果回调 == %@ ",result);
}