iOS開發--二維碼的掃描

來源:互聯網
上載者:User

標籤:

一.需要包含標頭檔

#import <AVFoundation/AVFoundation.h>

二.通過設定<AVCaptureMetadataOutputObjectsDelegate>代理可以監聽掃描到的二維碼中的資訊

三.具體代碼

 1 #import "ViewController.h" 2 #import <AVFoundation/AVFoundation.h> 3  4 @interface ViewController () <AVCaptureMetadataOutputObjectsDelegate> 5  6 @end 7  8 @implementation ViewController 9 10 - (void)viewDidLoad {11     [super viewDidLoad];12 }13 14 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event15 {16     // 1.建立捕捉會話17     AVCaptureSession *session = [[AVCaptureSession alloc] init];18     19     // 2.設定輸入裝置20     AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];21     AVCaptureDeviceInput *inputDevice = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil];22     [session addInput:inputDevice];23     24     // 3.設定輸入方式25     AVCaptureMetadataOutput *output = [[AVCaptureMetadataOutput alloc] init];26     [output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];27     [session addOutput:output];28     [output setMetadataObjectTypes:@[AVMetadataObjectTypeQRCode]];29     30     // 4.添加一個顯示的layer31     AVCaptureVideoPreviewLayer *layer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];32     layer.frame = self.view.bounds;33     [self.view.layer addSublayer:layer];34     35     // 5.開始掃描36     [session startRunning];37 }38 39 #pragma mark - 擷取掃描結果40 - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection41 {42     if (metadataObjects.count > 0) {43         AVMetadataMachineReadableCodeObject *object = [metadataObjects lastObject];44         NSLog(@"%@", object.stringValue);45     }46 }47 48 @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.