IOS7 二维码扫描


//


//  TCTosweepScan.m


//  TongChengSearch


//


//  Created by Fighting on 14-6-11.


//  Copyright (c) 2014 tcsos.com. All rights reserved.


//




#import "TCTosweepScan.h"


#import <AVFoundation/AVFoundation.h>




@interface TCTosweepScan()<AVCaptureMetadataOutputObjectsDelegate>




@property (strong, nonatomic)AVCaptureDevice *device;


@property (strong, nonatomic)AVCaptureDeviceInput *input;


@property (strong, nonatomic)AVCaptureMetadataOutput *output;


@property (strong, nonatomic)AVCaptureSession *session;


@property (strong, nonatomic)AVCaptureVideoPreviewLayer *preview;




@end








@implementation TCTosweepScan




- (instancetype)initConfigWith:(CGRect)frame previewFrame:(CGRect)previewFrame {


    if (self = [super init]) {


        self.frame = frame;


        self.autoresizingMask = YES;


        


        //初始化UI


        [self initUI:previewFrame];


    }


    


    return self;


}




- (void)dealloc {


    // 1. 如果扫描完成,停止会话


    [session stopRunning];


    


    // 2. 删除预览图层


    [preview removeFromSuperlayer];


    


    [output setMetadataObjectsDelegate:nil queue:nil];


}




//初始化UI


- (void)initUI:(CGRect)previewFrame {




    // Device


    device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];


    


    NSError *error = nil;


    


    // Input


    input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];


    


    if (error) {


        NSLog(@"你手机不支持二维码扫描!");


        return;


    }


    


    // Output


    output = [[AVCaptureMetadataOutput alloc] init];


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


    


    // Session


    session = [[AVCaptureSession alloc] init];


    [session setSessionPreset:AVCaptureSessionPresetHigh];


    


    if ([session canAddInput:input]) {


        [session addInput:input];


    }


    


    if ([session canAddOutput:output]) {


        [session addOutput:output];


    }


    


    // 条码类型


    output.metadataObjectTypes = @[AVMetadataObjectTypeQRCode];


    


    // Preview


    preview = [AVCaptureVideoPreviewLayer layerWithSession:session];


    preview.videoGravity = AVLayerVideoGravityResizeAspectFill;


    preview.frame = previewFrame;


    [self.layer addSublayer:preview];




}




//启动扫描


- (void)startScan {


    // Start


    [session startRunning];


}




// 此方法是在识别到QRCode,并且完成转换


// 如果QRCode的内容越大,转换需要的时间就越长


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




    // 1. 如果扫描完成,停止会话


    [session stopRunning];


    


    // 2. 删除预览图层


    [preview removeFromSuperlayer];


    


    NSString *val = nil;


    


    if (metadataObjects.count > 0) {


        AVMetadataMachineReadableCodeObject *obj = metadataObjects[0];


        val = obj.stringValue;


    }


    


    if (delegate && [delegate respondsToSelector:@selector(TCTosweepScanDidEnd:)]) {


        [delegate TCTosweepScanDidEnd:val];


    }


}




@synthesize delegate;


@synthesize device;


@synthesize input;


@synthesize output;


@synthesize session;


@synthesize preview;


@end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值