博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
浅谈iOS7 AVFoundation 二维码扫描
阅读量:4561 次
发布时间:2019-06-08

本文共 2677 字,大约阅读时间需要 8 分钟。

 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就是二维码的内容

转载于:https://www.cnblogs.com/WLL-Hero/p/3720331.html

你可能感兴趣的文章
Html5 舞动的雨伞
查看>>
快速构建Windows 8风格应用1-开发工具安装及模拟器使用
查看>>
Windows Phone 如果你把Pivot控件当成主页面,那么这篇文章你值得看。
查看>>
C#图像处理(各种旋转、改变大小、柔化、锐化、雾化、底片、浮雕、黑白、滤镜效果)...
查看>>
winform屏幕截图
查看>>
.NET(C#):XML序列化时派生类的处理
查看>>
sass学习笔记-输出方法
查看>>
Python之禅
查看>>
MongoDB分片集群配置实例
查看>>
mac 开启ntfs 权限
查看>>
laraval开发之QQ登录及QQ报错
查看>>
hibernate中的dialect解释
查看>>
基功太差,怨不得天,得下苦功——WAP面试,正式招聘号角响起
查看>>
resin4开启jmx
查看>>
Spring Boot+Mybatis+Pagehelper分页
查看>>
jsvc 启动java 在linux下的实现原理
查看>>
Ubuntu创建新用户并增加管理员权限
查看>>
MySQL库目录下db.opt文件的作用
查看>>
HTTP_referrer
查看>>
模拟表单方式上传文件
查看>>