iOS使用AVFoundation實現二維碼掃描

來源:互聯網
上載者:User

標籤:

實現過程如下:

Step1:需要匯入:AVFoundation Framework 包含標頭檔:

#import <AVFoundation/AVFoundation.h>

Step2:設定捕獲會話

設定 AVCaptureSession 和 AVCaptureVideoPreviewLayer 成員

12345678910
    #import <AVFoundation/AVFoundation.h>    static const char *kScanQRCodeQueueName = "ScanQRCodeQueue";    @interface ViewController () <AVCaptureMetadataOutputObjectsDelegate>    .....    @property (nonatomic) AVCaptureSession *captureSession;    @property (nonatomic) AVCaptureVideoPreviewLayer *videoPreviewLayer;    @property (nonatomic) BOOL lastResult;    @end

Step3:建立會話,讀取輸入資料流

12345678910111213141516171819202122232425262728293031323334353637
    - (BOOL)startReading    {        // 擷取 AVCaptureDevice 執行個體        NSError * error;        AVCaptureDevice *captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];        // 初始化輸入資料流        AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:captureDevice error:&error];        if (!input) {            NSLog(@"%@", [error localizedDescription]);            return NO;        }        // 建立會話        _captureSession = [[AVCaptureSession alloc] init];        // 添加輸入資料流        [_captureSession addInput:input];        // 初始化輸出資料流        AVCaptureMetadataOutput *captureMetadataOutput = [[AVCaptureMetadataOutput alloc] init];        // 添加輸出資料流        [_captureSession addOutput:captureMetadataOutput];        // 建立dispatch queue.        dispatch_queue_t dispatchQueue;        dispatchQueue = dispatch_queue_create(kScanQRCodeQueueName, NULL);        [captureMetadataOutput setMetadataObjectsDelegate:self queue:dispatchQueue];        // 設定中繼資料類型 AVMetadataObjectTypeQRCode        [captureMetadataOutput setMetadataObjectTypes:[NSArray arrayWithObject:AVMetadataObjectTypeQRCode]];        // 建立輸出對象        _videoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:_captureSession];        [_videoPreviewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];        [_videoPreviewLayer setFrame:_sanFrameView.layer.bounds];        [_sanFrameView.layer addSublayer:_videoPreviewLayer];        // 開始會話        [_captureSession startRunning];        return YES;    }

Step4:停止讀取

1234567
    - (void)stopReading    {        // 停止會話        [_captureSession stopRunning];        _captureSession = nil;    }

Step5:擷取捕獲資料

1234567891011121314
-(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects  fromConnection:(AVCaptureConnection *)connection{    if (metadataObjects != nil && [metadataObjects count] > 0) {        AVMetadataMachineReadableCodeObject *metadataObj = [metadataObjects objectAtIndex:0];        NSString *result;        if ([[metadataObj type] isEqualToString:AVMetadataObjectTypeQRCode]) {            result = metadataObj.stringValue;        } else {            NSLog(@"不是二維碼");        }        [self performSelectorOnMainThread:@selector(reportScanResult:) withObject:result waitUntilDone:NO];    }}

Step6:處理結果

12345678910111213141516
  - (void)reportScanResult:(NSString *)result  {      [self stopReading];      if (!_lastResult) {          return;      }      _lastResut = NO;      UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"二維碼掃描"                                                      message:result                                                     delegate:nil                                            cancelButtonTitle:@"取消"                                            otherButtonTitles: nil];      [alert show];      // 以下處理了結果,繼續下次掃描      _lastResult = YES;  }

以上基本就是二維碼的擷取流程

iOS使用AVFoundation實現二維碼掃描

聯繫我們

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