苹果原生二维码扫描

首先引入库:AVFoundation.framework

话不多说,直接贴上代码

  1 #import "ViewController.h"
  2 
  3 #import <AVFoundation/AVFoundation.h>
  4 
  5 static const char *kScanQRCodeQueueName = "ScanQRCodeQueue";
  6 
  7 @interface ViewController () <AVCaptureMetadataOutputObjectsDelegate>
  8 
  9 @property (weak, nonatomic) IBOutlet UIView *sanFrameView;//二维码扫描框
 10 @property (weak, nonatomic) IBOutlet UIButton *button;//开始按钮
 11 @property (weak, nonatomic) IBOutlet UIButton *lightButton;//打开闪光灯
 12 
 13 @property (nonatomic) AVCaptureSession *captureSession;
 14 @property (nonatomic) AVCaptureVideoPreviewLayer *videoPreviewLayer;
 15 @property (nonatomic) BOOL lastResut;
 16 @end
 17 
 18 @implementation ViewController
 19 
 20 - (void)viewDidLoad
 21 {
 22     [super viewDidLoad];
 23     [_button setTitle:@"开始" forState:UIControlStateNormal];
 24     [_lightButton setTitle:@"打开照明" forState:UIControlStateNormal];
 25     _lastResut = YES;
 26 }
 27 
 28 - (void)dealloc
 29 {
 30     [self stopReading];
 31 }
 32 
 33 
 34 - (BOOL)startReading
 35 {
 36     [_button setTitle:@"停止" forState:UIControlStateNormal];
 37     // 获取 AVCaptureDevice 实例
 38     NSError * error;
 39     AVCaptureDevice *captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
 40     // 初始化输入流
 41     AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:captureDevice error:&error];
 42     if (!input) {
 43         NSLog(@"%@", [error localizedDescription]);
 44         return NO;
 45     }
 46     // 创建会话
 47     _captureSession = [[AVCaptureSession alloc] init];
 48     // 添加输入流
 49     [_captureSession addInput:input];
 50     // 初始化输出流
 51     AVCaptureMetadataOutput *captureMetadataOutput = [[AVCaptureMetadataOutput alloc] init];
 52     // 添加输出流
 53     [_captureSession addOutput:captureMetadataOutput];
 54     
 55     // 创建dispatch queue.
 56     dispatch_queue_t dispatchQueue;
 57     dispatchQueue = dispatch_queue_create(kScanQRCodeQueueName, NULL);
 58     [captureMetadataOutput setMetadataObjectsDelegate:self queue:dispatchQueue];
 59     // 设置元数据类型 AVMetadataObjectTypeQRCode
 60     [captureMetadataOutput setMetadataObjectTypes:[NSArray arrayWithObject:AVMetadataObjectTypeQRCode]];
 61     
 62     // 创建输出对象
 63     _videoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:_captureSession];
 64     [_videoPreviewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
 65     [_videoPreviewLayer setFrame:_sanFrameView.layer.bounds];
 66     [_sanFrameView.layer addSublayer:_videoPreviewLayer];
 67     // 开始会话
 68     [_captureSession startRunning];
 69     
 70     return YES;
 71 }
 72 
 73 - (void)stopReading
 74 {
 75     [_button setTitle:@"开始" forState:UIControlStateNormal];
 76     // 停止会话
 77     [_captureSession stopRunning];
 78     _captureSession = nil;
 79 }
 80 
 81 - (void)reportScanResult:(NSString *)result
 82 {
 83     [self stopReading];
 84     if (!_lastResut) {
 85         return;
 86     }
 87     _lastResut = NO;
 88     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"二维码扫描"
 89                                                     message:result
 90                                                    delegate:nil
 91                                           cancelButtonTitle:@"取消"
 92                                           otherButtonTitles: nil];
 93     [alert show];
 94     // 以及处理了结果,下次扫描
 95     _lastResut = YES;
 96 }
 97 
 98 - (void)systemLightSwitch:(BOOL)open
 99 {
100     if (open) {
101         [_lightButton setTitle:@"关闭照明" forState:UIControlStateNormal];
102     } else {
103         [_lightButton setTitle:@"打开照明" forState:UIControlStateNormal];
104     }
105     AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
106     if ([device hasTorch]) {
107         [device lockForConfiguration:nil];
108         if (open) {
109             [device setTorchMode:AVCaptureTorchModeOn];
110         } else {
111             [device setTorchMode:AVCaptureTorchModeOff];
112         }
113         [device unlockForConfiguration];
114     }
115 }
116 
117 #pragma AVCaptureMetadataOutputObjectsDelegate
118 
119 -(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects
120       fromConnection:(AVCaptureConnection *)connection
121 {
122     if (metadataObjects != nil && [metadataObjects count] > 0) {
123         AVMetadataMachineReadableCodeObject *metadataObj = [metadataObjects objectAtIndex:0];
124         NSString *result;
125         if ([[metadataObj type] isEqualToString:AVMetadataObjectTypeQRCode]) {
126             result = metadataObj.stringValue;
127         } else {
128             NSLog(@"不是二维码");
129         }
130         [self performSelectorOnMainThread:@selector(reportScanResult:) withObject:result waitUntilDone:NO];
131     }
132 }
  //开始按钮方法实现
133 - (IBAction)startScanner:(id)sender 134 { 135 UIButton *button = (UIButton *)sender; 136 if ([button.titleLabel.text isEqualToString:@"开始"]) { 137 [self startReading]; 138 } else { 139 [self stopReading]; 140 } 141 } 142 143 - (IBAction)openSystemLight:(id)sender//打开闪光灯 144 { 145 UIButton *button = (UIButton *)sender; 146 if ([button.titleLabel.text isEqualToString:@"打开照明"]) { 147 [self systemLightSwitch:YES]; 148 } else { 149 [self systemLightSwitch:NO]; 150 } 151 }
原文地址:https://www.cnblogs.com/h-tao/p/4971793.html