条形码、二维码扫描

在iOS7以前,还没有系统自带的二维码扫描的SDK,那时候搞个二维码扫描复杂的要死,用的那些第三方库,什么zxing,zbar,既麻烦,又不好用。不过现在不同了,iOS7以后,已经有了自带的条形码和二维码扫描。

下面我直接上代码,非常简单。

先是.h文件:

#import <UIKit/UIKit.h>
@import AVFoundation;

@interface ViewController : UIViewController<AVCaptureMetadataOutputObjectsDelegate>
{
    AVCaptureDevice *_device;
    AVCaptureDeviceInput *_input;
    AVCaptureMetadataOutput *_output;
    AVCaptureSession *_session;
    AVCaptureVideoPreviewLayer *_preview;
}

@end

.m文件:

#import "ViewController.h"

// 屏幕宽度
#define ScreenWidth  [UIScreen mainScreen].bounds.size.width
// 屏幕高度
#define ScreenHeight [UIScreen mainScreen].bounds.size.height

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    
    _input = [AVCaptureDeviceInput deviceInputWithDevice:_device error:nil];
    
    _output = [[AVCaptureMetadataOutput alloc] init];
    [_output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
//    _output.rectOfInterest = CGRectMake (( 124 )/ ScreenHeight ,(( ScreenWidth - 220 )/ 2 )/ ScreenWidth , 220 / ScreenHeight , 220 / ScreenWidth );
    
    _session = [[AVCaptureSession alloc] init];
    _session.sessionPreset = AVCaptureSessionPresetHigh;
    
    if ([_session canAddInput:_input]) {
        [_session addInput:_input];
    }
    
    if ([_session canAddOutput:_output]) {
        [_session addOutput:_output];
    }
    
    _output.metadataObjectTypes = [NSArray arrayWithObjects:AVMetadataObjectTypeUPCECode,
                                   AVMetadataObjectTypeCode39Code,
                                   AVMetadataObjectTypeCode39Mod43Code,
                                   AVMetadataObjectTypeEAN13Code,
                                   AVMetadataObjectTypeEAN8Code,
                                   AVMetadataObjectTypeCode93Code,
                                   AVMetadataObjectTypeCode128Code,
                                   AVMetadataObjectTypePDF417Code,
                                   AVMetadataObjectTypeQRCode,
                                   AVMetadataObjectTypeAztecCode,
                                   AVMetadataObjectTypeInterleaved2of5Code,
                                   AVMetadataObjectTypeITF14Code,
                                   AVMetadataObjectTypeDataMatrixCode, nil];
    
    _preview = [AVCaptureVideoPreviewLayer layerWithSession:_session];
    _preview.videoGravity = AVLayerVideoGravityResizeAspectFill;
    _preview.frame = self.view.layer.bounds;
    [self.view.layer insertSublayer:_preview atIndex:0];
    
    [_session startRunning];
    
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
    imageView.image = [UIImage imageNamed:@"more_qr"];
    [self.view addSubview:imageView];

    
}



#pragma mark - AVCaptureMetadataOutputObjectsDelegate

- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection {
    
    NSString *stringValue;
    
    if ([metadataObjects count] > 0) {
        
        [_session stopRunning];
        
        AVMetadataMachineReadableCodeObject *metadataCode = [metadataObjects objectAtIndex:0];
        
        NSLog(@"%@",metadataCode);
        
        stringValue = metadataCode.stringValue;
        
        NSLog(@"扫描得到的:%@",stringValue);
        
    }
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

再来解释一下上面的代码吧。

在这里面,我把所有的扫码类型都加入进去了,其中

AVMetadataObjectTypeUPCECode、AVMetadataObjectTypeCode39Code、AVMetadataObjectTypeCode39Mod43Code、AVMetadataObjectTypeEAN13Code、AVMetadataObjectTypeEAN8Code、AVMetadataObjectTypeCode93Code、AVMetadataObjectTypeCode128Code、AVMetadataObjectTypePDF417Code、AVMetadataObjectTypeInterleaved2of5Code、AVMetadataObjectTypeITF14Code这些都属于不同的条形码类型,而AVMetadataObjectTypeQRCode、AVMetadataObjectTypeAztecCode、AVMetadataObjectTypeDataMatrixCode这几种属于二维码类型的。

_output.rectOfInterest = CGRectMake (( 124 )/ ScreenHeight ,(( ScreenWidth - 220 )/ 2 )/ ScreenWidth , 220 / ScreenHeight , 220 / ScreenWidth );
这是设置扫码框的大小,默认是全屏的。






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值