iOS系統方法掃描二維碼

來源:互聯網
上載者:User

標籤:






#import "RootViewController.h"
#import      AVFoundation/AVFoundation.h   //兩邊角括弧  顯示不出來

@interface RootViewController ()   AVCaptureMetadataOutputObjectsDelegate   //協議   道理同上 

@property (nonatomic, strong) UILabel *showLabel;
@property (nonatomic, strong) UIButton *button;
@property (nonatomic, strong) AVCaptureSession *session;
@property (nonatomic, strong) AVCaptureVideoPreviewLayer *preview;
@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.showLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 50, self.view.bounds.size.width-40, 30)];
    self.showLabel.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:self.showLabel];
    
    self.button = [UIButton buttonWithType:UIButtonTypeSystem];
    self.button.frame = CGRectMake(20, 300, self.view.bounds.size.width-40, 300);
    [self.button setTitle:@"開始" forState:UIControlStateNormal];
    [self.button setTitle:@"停止" forState:UIControlStateSelected];
    [self.button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.button];
}

- (void)buttonClick:(UIButton *)button {
    button.selected = !button.selected;
    if (button.selected) {
        [self startReadingMachineReadableCodeObjects:@[AVMetadataObjectTypeQRCode] inView:self.view];
    } else {
        [self stopReading];
    }
}

// 開始掃描
- (void)startReadingMachineReadableCodeObjects:(NSArray *)codeObjects inView:(UIView *)preview {
    // 網路攝影機裝置
    AVCaptureDevice *captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    NSError *error = nil;
    
    // 設定輸入口
    AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:captureDevice error:&error];
    if (error || !input) {
        NSLog(@"error: %@", [error description]);
        return;
    }
    
    // 會話session, 把輸入口加入會話
    self.session = [[AVCaptureSession alloc] init];
    [self.session addInput:input];   // 將輸入添加到session
    
    // 設定輸出口,加入session, 設定輸出口參數
    AVCaptureMetadataOutput *output = [[AVCaptureMetadataOutput alloc] init];
    [self.session addOutput:output];
    
    [output setMetadataObjectTypes:codeObjects];
    [output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()]; // 使用主線程隊列,相應比較同步,使用其他隊列,相應不同步,容易讓使用者產生不好的體驗
    
    // 設定展示層(預覽層)
    self.preview       = [AVCaptureVideoPreviewLayer layerWithSession:self.session];  // 設定預覽層資訊
    self.preview.frame = preview.bounds;
    [preview.layer insertSublayer:self.preview atIndex:0]; //  添加至視圖
    
    [self beginReading];
}

// 啟動session
- (void)beginReading
{
    [self.session startRunning];
}

// 關閉session
- (void)stopReading
{
    self.button.selected = NO;
    [self.session stopRunning];
    [self.preview removeFromSuperlayer];
}

// 識別到二維碼 並解析轉換為字串
#pragma mark -
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection
{
    [self stopReading];
    
    AVMetadataObject *metadata = [metadataObjects objectAtIndex:0];
    NSString *codeStr  = nil;
    if ([metadata respondsToSelector:@selector(stringValue)]) {
        codeStr = [(AVMetadataMachineReadableCodeObject *)metadata stringValue];
    }
    
    self.showLabel.text = codeStr;
}

@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.