掃描二維碼

一垫释、掃描二維碼需要4個對象

// 1.輸入設備 
@property (nonatomic, strong) AVCaptureDeviceInput *deviceInput;
// 2.輸出設備   metadata 元數(shù)據(jù)
@property (nonatomic, strong) AVCaptureMetadataOutput *metadataOutput;
// 3.會話  連接輸入和輸出設備
@property (nonatomic, strong) AVCaptureSession *session;
// 4.預覽界面  預覽攝像頭采集到的信息   -->特殊的圖層
@property (nonatomic, strong) AVCaptureVideoPreviewLayer *previewLayer;

二、實例化 并配置 各對象


    // 1.輸入設備  -->鍵盤  攝像頭  麥克風
    // 默認是后置攝像頭
    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    self.deviceInput = [[AVCaptureDeviceInput alloc] initWithDevice:device error:nil];
    // 2.輸出設備  -->解析數(shù)據(jù)
    self.metadataOutput = [[AVCaptureMetadataOutput alloc] init];
    // 設置輸出設備代理 在掃描到信息時可執(zhí)行回調(diào)
    [self.metadataOutput setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
    // 3.會話 -->連接輸入和輸出設備
    self.session = [[AVCaptureSession alloc] init];
    if ([self.session canAddInput:self.deviceInput]) {
        [self.session addInput:self.deviceInput];
    }
    if ([self.session canAddOutput:self.metadataOutput]) {
        [self.session addOutput:self.metadataOutput];
    }
    // 設置需要解析的數(shù)據(jù)類型
    self.metadataOutput.metadataObjectTypes = @[AVMetadataObjectTypeQRCode];
    // 4.預覽界面  -->預覽攝像頭采集到的信息   -->特殊的圖層
    self.previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.session];
    self.previewLayer.frame = self.view.bounds;
    [self.view.layer addSublayer:self.previewLayer];
    // 5.開啟會話
    [self.session startRunning];

三、掃描到信息時的回調(diào),如果不主動取消,會一直調(diào)用.

#pragma mark - AVCaptureMetadataOutputObjectsDelegate
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection{
    
    //關(guān)閉會話
    [self.session stopRunning];
    [self.previewLayer removeFromSuperlayer];  
    [self dismissViewControllerAnimated:YES completion:nil];

    //使用Safari 控制器 加載頁面
    if ([[metadataObjects.firstObject stringValue] hasPrefix:@"http"]||[[metadataObjects.firstObject stringValue] hasPrefix:@"https"]) {
        SFSafariViewController *safarVC = [[SFSafariViewController alloc]initWithURL:[NSURL URLWithString:[metadataObjects.firstObject stringValue]]];
        [self presentViewController:safarVC animated:YES completion:nil];
        
    }
   
//使用 block 進行數(shù)據(jù)逆?zhèn)?        self.complete([metadataObjects.firstObject stringValue]);   
}

四缕碎、可以自定義一個 view 完成 UI 頁面設置
有兩個地方需要注意:

需要改變 view 的 layer -->AVCaptureVideoPreviewLayer
需要將會話 session 傳給 view

#import "NEQRPreview.h"

@interface NEQRPreview ()
@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UIImageView *lineImageView;
@property (nonatomic, strong) NSTimer *timer;

@end

@implementation NEQRPreview
//給 session 賦值
- (void)setSession:(AVCaptureSession *)session{
    _session = session;
    AVCaptureVideoPreviewLayer *layer = (AVCaptureVideoPreviewLayer *) self.layer;
    layer.session = session;

}
//改變 View layer
+(Class)layerClass{
    return [AVCaptureVideoPreviewLayer class];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initUiConfig];
    }
    return self;
}

//在一個視圖中設置二維碼UI的垃圾代碼
- (void)initUiConfig
{
    //設置背景圖片
    _imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pick_bg.png"]];
    //設置位置到界面的中間
    _imageView.frame = CGRectMake(self.bounds.size.width * 0.5 - 140, self.bounds.size.height * 0.5 - 140, 280, 280);
    
    NSLog(@"%@",NSStringFromCGRect( _imageView.frame));
    
    //添加到視圖上
    [self addSubview:_imageView];
    
    //初始化二維碼的掃描線的位置
    _lineImageView = [[UIImageView alloc] initWithFrame:CGRectMake(30, 10, 220, 2)];
    _lineImageView.image = [UIImage imageNamed:@"line.png"];
    [_imageView addSubview:_lineImageView];
    
    //開啟定時器
    _timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(animation) userInfo:nil repeats:YES];
}

- (void)animation
{
    [UIView animateWithDuration:2.8 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
        
        _lineImageView.frame = CGRectMake(30, 260, 220, 2);
        
    } completion:^(BOOL finished) {
        _lineImageView.frame = CGRectMake(30, 10, 220, 2);
    }];
}

@end

注意:

如出現(xiàn)以下提示

This app has crashed because it attempted to access privacy-sensitive data without a usage description. The app's Info.plist must contain an NSCameraUsageDescription key with a string value explaining to the user how the app uses this data.

解決方法 :

在 Info.plist中加入 NSCameraUsageDescription 字段即可.

DEMO:GitHub

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末善已,一起剝皮案震驚了整個濱河市灼捂,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌换团,老刑警劉巖悉稠,帶你破解...
    沈念sama閱讀 221,820評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異艘包,居然都是意外死亡的猛,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,648評論 3 399
  • 文/潘曉璐 我一進店門想虎,熙熙樓的掌柜王于貴愁眉苦臉地迎上來卦尊,“玉大人,你說我怎么就攤上這事磷醋∶担” “怎么了胡诗?”我有些...
    開封第一講書人閱讀 168,324評論 0 360
  • 文/不壞的土叔 我叫張陵邓线,是天一觀的道長。 經(jīng)常有香客問我煌恢,道長骇陈,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,714評論 1 297
  • 正文 為了忘掉前任瑰抵,我火速辦了婚禮你雌,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘二汛。我一直安慰自己婿崭,他們只是感情好,可當我...
    茶點故事閱讀 68,724評論 6 397
  • 文/花漫 我一把揭開白布肴颊。 她就那樣靜靜地躺著氓栈,像睡著了一般。 火紅的嫁衣襯著肌膚如雪婿着。 梳的紋絲不亂的頭發(fā)上授瘦,一...
    開封第一講書人閱讀 52,328評論 1 310
  • 那天,我揣著相機與錄音竟宋,去河邊找鬼提完。 笑死,一個胖子當著我的面吹牛丘侠,可吹牛的內(nèi)容都是我干的徒欣。 我是一名探鬼主播,決...
    沈念sama閱讀 40,897評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼蜗字,長吁一口氣:“原來是場噩夢啊……” “哼打肝!你這毒婦竟也來了官研?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,804評論 0 276
  • 序言:老撾萬榮一對情侶失蹤闯睹,失蹤者是張志新(化名)和其女友劉穎戏羽,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體楼吃,經(jīng)...
    沈念sama閱讀 46,345評論 1 318
  • 正文 獨居荒郊野嶺守林人離奇死亡始花,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,431評論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了孩锡。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片酷宵。...
    茶點故事閱讀 40,561評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖躬窜,靈堂內(nèi)的尸體忽然破棺而出浇垦,到底是詐尸還是另有隱情,我是刑警寧澤荣挨,帶...
    沈念sama閱讀 36,238評論 5 350
  • 正文 年R本政府宣布男韧,位于F島的核電站,受9級特大地震影響默垄,放射性物質(zhì)發(fā)生泄漏此虑。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,928評論 3 334
  • 文/蒙蒙 一口锭、第九天 我趴在偏房一處隱蔽的房頂上張望朦前。 院中可真熱鬧,春花似錦鹃操、人聲如沸韭寸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,417評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽恩伺。三九已至,卻和暖如春臭胜,著一層夾襖步出監(jiān)牢的瞬間莫其,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,528評論 1 272
  • 我被黑心中介騙來泰國打工耸三, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留乱陡,地道東北人。 一個月前我還...
    沈念sama閱讀 48,983評論 3 376
  • 正文 我出身青樓仪壮,卻偏偏與公主長得像憨颠,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 45,573評論 2 359

推薦閱讀更多精彩內(nèi)容