ios7新增api实现扫描二维码

本来用的ZBar开源库实现的扫描二维码,但是貌似不支持arm64了,也没有在更新。

现在不用适配ios7以下,而iOS新增系统API已支持扫码,参考老外的一篇博客做了个demo,需要的可以参考下

参考博客:http://www.appcoda.com/qr-code-ios-programming-tutorial/


#import <AVFoundation/AVFoundation.h>
@interface QRCodeReadController : BaseViewController<AVCaptureMetadataOutputObjectsDelegate>

@property (weak, nonatomic) IBOutlet UIView *viewPreview;
@end
在xib上加一个viewPreview,用来扫码时动态显示获取到的摄像头的内容

@interface QRCodeReadController ()
{
    NSInteger maxY;
    NSInteger minY;
    NSTimer *timer;
    
    UIImageView *line;
}
@property (nonatomic) BOOL isReading;

@property (nonatomic, strong) AVCaptureSession *captureSession;
@property (nonatomic, strong) AVCaptureVideoPreviewLayer *videoPreviewLayer;

-(BOOL)startReading;
-(void)stopReading;

@end

@implementation QRCodeReadController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    _isReading = NO;
    if ([self startReading]) {
        maxY = 280;
        minY = 2;
        line =[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 280, 10)];  // 0 -200
        [line setImage:[UIImage imageNamed:@"e0"]];
        [_viewPreview addSubview:line];
        
        timer = [NSTimer scheduledTimerWithTimeInterval:1.0/40 target:self selector:@selector(move) userInfo:nil repeats:YES];
    };
    

}

/*
 *
 *
 AVCaptureMetadataOutput object. This class in combination with the AVCaptureMetadataOutputObjectsDelegate protocol will manage to intercept any metadata found in the input device (meaning data in a QR code captured by our camera) and translate it to a human readable format.
 */
- (BOOL)startReading {
    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_t dispatchQueue;
    dispatchQueue = dispatch_queue_create("myQueue", NULL);
    [captureMetadataOutput setMetadataObjectsDelegate:self queue:dispatchQueue];
    [captureMetadataOutput setMetadataObjectTypes:[NSArray arrayWithObject:AVMetadataObjectTypeQRCode]];
    
    
    //show to user what the camera of the device sees  using a AVCaptureVideoPreviewLayer
    _videoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:_captureSession];
    [_videoPreviewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
    [_videoPreviewLayer setFrame:_viewPreview.layer.bounds];
    [_viewPreview.layer addSublayer:_videoPreviewLayer];
    
    [_captureSession startRunning];

    return YES;
}

-(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection{
    if (metadataObjects != nil && [metadataObjects count] > 0) {
        AVMetadataMachineReadableCodeObject *metadataObj = [metadataObjects objectAtIndex:0];
        if ([[metadataObj type] isEqualToString:AVMetadataObjectTypeQRCode]) {
            [self performSelectorOnMainThread:@selector(stopReading) withObject:nil waitUntilDone:NO];
            NSLog(@"metadataObj  string = %@",[metadataObj stringValue]);
            _isReading = NO;
        }
    }
}

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


// 扫描时,移动扫描线
-(void)move
{
    NSLog(@"+++");
    static BOOL flag = TRUE;  // true down  and false up
    if (flag) {
        if (line.frame.origin.y <maxY) {
            line.frame = CGRectMake(
                                    line.frame.origin.x, line.frame.origin.y +5,
                                    line.frame.size.width, line.frame.size.height
                                    );
        }else{
            flag = !flag;
            if (_isReading&&[timer isValid]) {
                [timer invalidate];
            }
        }
    }else
    {
        if (line.frame.origin.y >minY) {
            line.frame = CGRectMake(
                                    line.frame.origin.x, line.frame.origin.y -5,
                                    line.frame.size.width, line.frame.size.height
                                    );
        }else
        {
            flag = !flag;
        }
    }
    
    
}





郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。