iOS二維碼掃描

來源:互聯網
上載者:User

標籤:framework   tar   imp   type   技術   property   perl   代碼   code   

每一步:添加 AVFoundation.framework 庫

General > Linked Frameworks and Libraries

 

第二步:代碼整合

 

#import "ViewController.h"

#import <AVFoundation/AVFoundation.h>

 

//1. 有個素材

@interface ViewController ()<AVCaptureMetadataOutputObjectsDelegate>

 

/** 2.輸入設定 採集相機擷取到的資訊 */

@property (nonatomic, strong) AVCaptureDeviceInput *input;

 

/** 3.輸出裝置 解析輸入裝置採集到的資訊 */

@property (nonatomic, strong) AVCaptureMetadataOutput *output;

 

/** 4. layer (特殊圖層 能夠展示網路攝影機採集到的畫面) 展示輸入裝置採集到的資訊 */

@property (nonatomic, strong) AVCaptureVideoPreviewLayer *layer;

 

/** 5.關聯輸入裝置和輸出裝置 會話 */

@property (nonatomic, strong) AVCaptureSession *session;

@end

 

@implementation ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

 

}

 

// 點擊掃描二維碼

- (IBAction)click:(id)sender {

    

    // 1.建立輸入裝置

    //     AVMediaTypeVideo 網路攝影機

    //     AVMediaTypeAudio 麥克風

    //     AVCaptureDeviceInput  輸入裝置    default預設後置

    //    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

    NSArray *allDevice = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];

    

    self.input = [AVCaptureDeviceInput deviceInputWithDevice:[allDevice firstObject] error:nil];

 

    //2. 建立輸出裝置

    self.output = [[AVCaptureMetadataOutput alloc]init];

    //解析 返回的資料

    

    //設定輸出裝置的代理 返回解析後的資料

    [self.output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];

    

    //3.建立會話

    self.session = [[AVCaptureSession alloc]init];

    

    //4.關聯 會話和裝置

    if ([self.session canAddInput:self.input]) {

        [self.session addInput:self.input];

    }

    if ([self.session canAddOutput:self.output]) {

        [self.session addOutput:self.output];

    }

    //告訴資料類型  AVMetadataObjectTypeQRCode 二維碼

    self.output.metadataObjectTypes = @[AVMetadataObjectTypeQRCode];

    // 掃描框大小  整個螢幕

    [self.session setSessionPreset:AVCaptureSessionPreset640x480];

    

    //5.指定layer 的 frame  然後添加到 view上

    self.layer = [[AVCaptureVideoPreviewLayer alloc]initWithSession:self.session];

    self.layer.frame = self.view.bounds;

    [self.view.layer addSublayer:self.layer];

    

    //開啟會話

    [self.session startRunning];

}

 

 

//掃描出結果後就會調用的方法

- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection{

 

    AVMetadataMachineReadableCodeObject *objc = [metadataObjects firstObject];

    NSString *str = objc.stringValue;

    

    NSLog(@"%@",str);

 

    //停止掃描

    [self.session stopRunning];

    

    //移除 layer

    [self.layer removeFromSuperlayer];

}

 

@end

iOS二維碼掃描

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.