iOS7 AVFoundation 二维码扫瞄

 iOS7,AVFoundation中现在已经内置支持一维和二维码的扫瞄,iOS6及之前的想要扫瞄二维码,还是需要添加第三方库ZXing和ZBar。

ZBar生成二维码:http://blog.csdn.net/cafei111/article/details/8924297

先添加AVFoundation.framework

#import <AVFoundation/AVFoundation.h>


@interface QRcodeViewController :UIViewController<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


- (void)setupCamera

{

    // Device

    self.device = [AVCaptureDevicedefaultDeviceWithMediaType:AVMediaTypeVideo];

    

    // Input

    self.input = [AVCaptureDeviceInputdeviceInputWithDevice:self.deviceerror:nil];

    

    // Output

    self.output = [[AVCaptureMetadataOutputalloc]init];

    [self.outputsetMetadataObjectsDelegate:selfqueue:dispatch_get_main_queue()];

    

    // Session

    self.session = [[AVCaptureSessionalloc]init];

   [self.sessionsetSessionPreset:AVCaptureSessionPresetHigh];

   if ([self.sessioncanAddInput:self.input])

    {

        [self.sessionaddInput:self.input];

    }

   if ([self.sessioncanAddOutput:self.output])

    {

        [self.sessionaddOutput:self.output];

    }

    

    // 条码类型

    self.output.metadataObjectTypes =@[AVMetadataObjectTypeQRCode];

    

    // Preview

    self.preview = [AVCaptureVideoPreviewLayerlayerWithSession:self.session];

    self.preview.videoGravity =AVLayerVideoGravityResizeAspectFill;

    self.preview.frame =CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height);

   [self.view.layeraddSublayer:self.preview];

    

    // Start

    [self.sessionstartRunning];

}


条码类型有如下几种:
1
2
3
4
5
6
7
8
9
10
AVMetadataObjectTypeUPCECode
AVMetadataObjectTypeCode39Code
AVMetadataObjectTypeCode39Mod43Code
AVMetadataObjectTypeEAN13Code
AVMetadataObjectTypeEAN8Code
AVMetadataObjectTypeCode93Code
AVMetadataObjectTypeCode128Code
AVMetadataObjectTypePDF417Code
AVMetadataObjectTypeQRCode
AVMetadataObjectTypeAztecCode

扫瞄到二维码之后,会调用delegate

#pragma mark AVCaptureMetadataOutputObjectsDelegate

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

{

   NSString *stringValue;

    

   if ([metadataObjectscount] >0) {

       AVMetadataMachineReadableCodeObject *metadataObject = [metadataObjectsobjectAtIndex:0];

        stringValue = metadataObject.stringValue;

    }

    

    [_sessionstopRunning];

    

    [selfdismissViewControllerAnimated:YEScompletion:^{

       UIAlertView *alert = [[UIAlertViewalloc]initWithTitle:nil

                                                       message:stringValue

                                                      delegate:nil

                                             cancelButtonTitle:@"OK"

                                             otherButtonTitles:nil,nil];

        [alertshow];

    }];

}


这个委托方法里面的字符串stringValue就是二维码的内容

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值