iOS原生的AVFoundation掃描二維碼/條形碼


#import <AVFoundation/AVFoundation.h>

@interface ViewController ()<AVCaptureMetadataOutputObjectsDelegate>
//掃描條形碼
@property (strong, nonatomic) UIView *boxView;
@property (strong, nonatomic) UILabel *lblStatus;
@property (strong, nonatomic) CALayer *scanLayer;

//捕捉會話
@property (nonatomic, strong) AVCaptureSession *captureSession;
//展示layer
@property (nonatomic, strong) AVCaptureVideoPreviewLayer *videoPreviewLayer;
//掃描二維碼
- (void)clickSaoMiaoBtn
{
    [self startReading];
}
- (BOOL)startReading {
    NSError *error;
    //1.初始化捕捉設備(AVCaptureDevice),類型為AVMediaTypeVideo
    AVCaptureDevice *captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    //2.用captureDevice創建輸入流
    AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:captureDevice error:&error];
    if (!input) {
        NSLog(@"%@", [error localizedDescription]);
        return NO;
    }    
    //3.創建媒體數據輸出流
    AVCaptureMetadataOutput *captureMetadataOutput = [[AVCaptureMetadataOutput alloc] init];
    //4.實例化捕捉會話
    _captureSession = [[AVCaptureSession alloc] init];
    //4.1.將輸入流添加到會話
    [_captureSession addInput:input];
    //4.2.將媒體輸出流添加到會話中
    [_captureSession addOutput:captureMetadataOutput];
    //5.創建串行隊列,並加媒體輸出流添加到隊列當中
    dispatch_queue_t dispatchQueue;
    dispatchQueue = dispatch_queue_create("myQueue", NULL);
    //5.1.設置代理
    [captureMetadataOutput setMetadataObjectsDelegate:self queue:dispatchQueue];
    //5.2.設置輸出媒體數據類型為QRCode
    [captureMetadataOutput setMetadataObjectTypes:[NSArray arrayWithObjects:AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode128Code,nil]];
    //6.實例化預覽圖層
    _videoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:_captureSession];
    //7.設置預覽圖層填充方式
    [_videoPreviewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
    //8.設置圖層的frame
    [_videoPreviewLayer setFrame:self.view.layer.bounds];
    //9.將圖層添加到預覽view的圖層上
    [self.view.layer addSublayer:_videoPreviewLayer];
    //10.設置掃描范圍
    captureMetadataOutput.rectOfInterest = CGRectMake(0.2f, 0.2f, 0.6f, 0.6f);
    //10.1.掃描框
    _boxView = [[UIView alloc] initWithFrame:CGRectMake(WIDTH * 0.2f, HEIGHT * 0.2f, WIDTH - WIDTH * 0.4f, HEIGHT - HEIGHT * 0.4f)];
    _boxView.layer.borderColor = [UIColor orangeColor].CGColor;
    _boxView.layer.borderWidth = 2.0f;
    [self.view addSubview:_boxView];
    //10.2.掃描線
    _scanLayer = [[CALayer alloc] init];
    _scanLayer.frame = CGRectMake(0, 0, _boxView.bounds.size.width, 1);
    _scanLayer.backgroundColor = [UIColor redColor].CGColor;
    [_boxView.layer addSublayer:_scanLayer];
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.1f target:self selector:@selector(moveScanLayer:) userInfo:nil repeats:YES];
    [timer fire];
    //10.開始掃描
    [_captureSession startRunning];
    return YES;
}
#pragma mark - AVCaptureMetadataOutputObjectsDelegate
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection
{
    //判斷是否有數據
    if (metadataObjects != nil && [metadataObjects count] > 0) {
        AVMetadataMachineReadableCodeObject *metadataObj = [metadataObjects objectAtIndex:0];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            self.searchTF.text = [metadataObj stringValue];
            [self stopReading];
        });
        
    }
}

- (void)moveScanLayer:(NSTimer *)timer
{
    CGRect frame = _scanLayer.frame;
    if (_boxView.frame.size.height < _scanLayer.frame.origin.y +50*hl) {
        _scanLayer.frame = CGRectMake(0, 0, _boxView.bounds.size.width, 1);
    }else{
        frame.origin.y += 20*hl;
        [UIView animateWithDuration:0.01f animations:^{
            _scanLayer.frame = frame;
        }];
    }
}

-(void)stopReading{
    [_captureSession stopRunning];
    _captureSession = nil;
    [_scanLayer removeFromSuperlayer];
    [_videoPreviewLayer removeFromSuperlayer];
    [_boxView removeFromSuperview];
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM