IOS 二維碼掃描,ios掃描

來源:互聯網
上載者:User

IOS 二維碼掃描,ios掃描

這篇部落格是對上篇部落格的補充,內容較少。

由於二維碼掃描需要在真機上測試本人比較爛就不做了。

下面是代碼(代碼中已經寫了很清楚的注釋)

////  ViewController.m//  CX 二維碼掃描////  Created by ma c on 16/4/12.//  Copyright © 2016年 bjsxt. All rights reserved.//#import "ViewController.h"#import <AVFoundation/AVFoundation.h>@interface ViewController ()<AVCaptureMetadataOutputObjectsDelegate>@end@implementation ViewController- (void)viewDidLoad {        }- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{        //建立捕捉會話    AVCaptureSession * session = [[AVCaptureSession alloc]init];    //添加輸入裝置    AVCaptureDevice * device = [AVCaptureDevice defaultDeviceWithMediaType:@"AVMediaTypeVideo"];    AVCaptureDeviceInput * input = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil];    [session addInput:input];    //添加輸出資料    AVCaptureMetadataOutput * output = [[AVCaptureMetadataOutput alloc]init];    [output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];    [session addOutput:output];    //告訴中繼資料類型為二維碼類型    //注意該方法在add後 否則崩潰    //測試需要真機稍有麻煩 就不了    [output setMetadataObjectTypes:@[AVMetadataObjectTypeQRCode]];    //添加掃描圖層    AVCaptureVideoPreviewLayer *layer = [AVCaptureVideoPreviewLayer layerWithSession:session];    layer.frame = CGRectMake(10,20, self.view.frame.size.width, 400);    [self.view.layer addSublayer:layer];    //開始掃描    [session startRunning];        //下面的方法適當的時候操作    //停止掃描//    [session stopRunning];    //移除圖層//    [layer removeFromSuperlayer];}- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection{    //metadataObjects 為掃描的後的資料        AVMetadataMachineReadableCodeObject * objc = [metadataObjects lastObject];    //我們想要的結果    NSLog(@"%@",objc.stringValue);}@end

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.