#import "QRCodeScanViewController.h"
#import <AVFoundation/AVFoundation.h>
@interface QRCodeScanViewController () <AVCaptureMetadataOutputObjectsDelegate>
// 使用掃描二維碼, 需要導(dǎo)入框架 <AVFoundation/AVFoundation.h>
// 手機(jī)震動(dòng), 需要導(dǎo)入框架 <AudioToolbox/AudioToolbox.h>
/**
* 輸入輸出流管道(控制輸入輸出流)
*/
@property (nonatomic, strong) AVCaptureSession *session;
/**
* 顯示捕獲到的相機(jī)輸出流
*/
@property (nonatomic, strong) AVCaptureVideoPreviewLayer *preLayer;
/**
* 獲取攝像設(shè)備
*/
@property (nonatomic, strong) AVCaptureDevice *device;
/**
* 創(chuàng)建輸入流
*/
@property (nonatomic, strong) AVCaptureDeviceInput *input;
/**
* 創(chuàng)建輸出流
*/
@property (nonatomic, strong) AVCaptureMetadataOutput *output;
@end
@implementation QRCodeScanViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
// 開始掃描二維碼
[self startScan];
// 點(diǎn)擊返回主界面
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget: self action: @selector(dismissMainView)];
[self.view addGestureRecognizer: tap];
}
- (void) dealloc {
// 視圖消失, 關(guān)閉通道
[self.session stopRunning];
}
- (void) dismissMainView {
[self dismissViewControllerAnimated: YES completion: nil];
}
// 開始掃描二維碼
- (void) startScan {
// 獲取攝像設(shè)備
self.device = [AVCaptureDevice defaultDeviceWithMediaType: AVMediaTypeVideo];
// 創(chuàng)建輸入流
NSError *error = nil;
self.input = [AVCaptureDeviceInput deviceInputWithDevice: self.device error: &error];
if (error) {
NSLog(@"input error: %@", error);
}
// 創(chuàng)建輸出流
self.output = [AVCaptureMetadataOutput new];
// 設(shè)置代理, 在主線程中刷新
[self.output setMetadataObjectsDelegate:self queue: dispatch_get_main_queue()];
// 初始化連接對(duì)象
self.session = [AVCaptureSession new];
// 設(shè)置高質(zhì)量采集率
[self.session setSessionPreset: AVCaptureSessionPresetHigh];
// 輸入輸出流加入到會(huì)話中
[self.session addInput: self.input];
[self.session addOutput: self.output];
// 設(shè)置掃描支持的編碼格式
self.output.metadataObjectTypes = @[AVMetadataObjectTypeQRCode];
// 設(shè)置掃描layer
self.preLayer = [AVCaptureVideoPreviewLayer layerWithSession: self.session];
self.preLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
CGSize windowSize = [UIScreen mainScreen].bounds.size;
self.preLayer.frame = CGRectMake((windowSize.width - 160)/4, windowSize.height/6 - 30, windowSize.width/2 + 80 , windowSize.height/2);
[self.view.layer insertSublayer: self.preLayer atIndex:0];
self.preLayer.cornerRadius = 10;
self.preLayer.borderColor = [UIColor orangeColor].CGColor; ;
self.preLayer.borderWidth = 3;
// 啟動(dòng)管道, 開始捕獲
[self.session startRunning];
}
#pragma mark - AVCaptureMetadataOutputObjectsDelegate
// 掃描二維碼
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection {
// 判斷是否存在數(shù)據(jù)
if (metadataObjects.count > 0) {
AVMetadataMachineReadableCodeObject *obj = [metadataObjects lastObject];
// 二維碼掃描信息處理
NSLog(@"obj --%@", obj);
NSLog(@"obj.corners = %@",obj.corners);
NSLog(@"obj.stringValue = %@",obj.stringValue);
NSString *string = [NSString stringWithFormat:@"%@",obj.stringValue];
NSLog(@"掃碼結(jié)果:%@",string);
}
}
iOS 掃描二維碼
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
推薦閱讀更多精彩內(nèi)容
- //首先介紹怎么生成二維碼 1,引入系統(tǒng)的頭文件 2, 代碼量很小 敲一遍也無妨 每段代碼的注釋都有 3,ok 這...
- 自iOS7以后,iOS掃描二維碼就并不需要借助第三方的框架,這次項(xiàng)目的需求,自己實(shí)現(xiàn)一個(gè)掃描的功能,蘋果在AVFo...
- 1.代碼如下: #import "ViewController.h" #import @interface Vie...
- //聯(lián)系人:石虎QQ: 1224614774昵稱:嗡嘛呢叭咪哄 一、用 ZBarSDK 做掃描二維碼 導(dǎo)入的庫(kù)文件...
- 文|年小暖 這個(gè)世界有沒有這種人,在很多人面前表現(xiàn)的無所畏懼,在很多人面前強(qiáng)大的好像沒有什么軟肋,卻在一個(gè)人面前溫...