欢迎来到 IT实训基地-南通科迅教育
咨询电话:0513-81107100
iOS中 扫描二维码/生成二维码详解
2017/4/18
南通科迅教育
434
南通零基础IT培训班如何选择

最近大家总是问我有没有关于二维码的demo,为了满足大家的需求,特此研究了一番,希望能帮到大家!

指示根视图:

?
1
self.window.rootViewController = [[UINavigationController alloc]initWithRootViewController:[SecondViewControllernew]];

 


生成二维码:

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//  Created by 韩俊强 on 15/11/27.
//  Copyright (c) 2015年 韩俊强. All rights reserved.
//
 
#import"SecondViewController.h"
 
@interfaceSecondViewController ()
 
@property(nonatomic, strong) UITextField *tfCode;
@property(nonatomic, strong) UIButton *btnGenerate;
@property(nonatomic, strong) UIImageView *imageView;
@end
 
@implementationSecondViewController
 
- (void)viewDidLoad {
    [superviewDidLoad];
    CGSize windowSize = [UIScreen mainScreen].bounds.size;
     
    self.tfCode = [[UITextField alloc] initWithFrame:CGRectMake(10,64, windowSize.width-100,40)];
    [self.view addSubview:self.tfCode];
    self.tfCode.borderStyle = UITextBorderStyleRoundedRect;
     
    self.btnGenerate = [[UIButton alloc] initWithFrame:CGRectMake(windowSize.width-100,64,90,40)];
    [self.view addSubview:self.btnGenerate];
    [self.btnGenerate addTarget:self action:@selector(actionGenerate) forControlEvents:UIControlEventTouchUpInside];
    self.btnGenerate.backgroundColor = [UIColor lightGrayColor];
    [self.btnGenerate setTitle:@"生成"forState:UIControlStateNormal];
    [self.btnGenerate setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
     
    self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,300,300)];
    [self.view addSubview:self.imageView];
    self.imageView.center = CGPointMake(windowSize.width/2, windowSize.height/2);
     
    self.tfCode.text = @"http://www.baidu.com";
}
- (void)actionGenerate
{
    NSString *text = self.tfCode.text;
     
    NSData *stringData = [text dataUsingEncoding: NSUTF8StringEncoding];
     
    //生成
    CIFilter *qrFilter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
    [qrFilter setValue:stringData forKey:@"inputMessage"];
    [qrFilter setValue:@"M"forKey:@"inputCorrectionLevel"];
     
    UIColor *onColor = [UIColor blackColor];
    UIColor *offColor = [UIColor whiteColor];
     
    //上色
    CIFilter *colorFilter = [CIFilter filterWithName:@"CIFalseColor"keysAndValues:@"inputImage",qrFilter.outputImage,@"inputColor0",[CIColor colorWithCGColor:onColor.CGColor],@"inputColor1",[CIColor colorWithCGColor:offColor.CGColor],nil];
     
    CIImage *qrImage = colorFilter.outputImage;
     
    //绘制
    CGSize size = CGSizeMake(300,300);
    CGImageRef cgImage = [[CIContext contextWithOptions:nil] createCGImage:qrImage fromRect:qrImage.extent];
    UIGraphicsBeginImageContext(size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetInterpolationQuality(context, kCGInterpolationNone);
    CGContextScaleCTM(context,1.0, -1.0);
    CGContextDrawImage(context, CGContextGetClipBoundingBox(context), cgImage);
    UIImage *codeImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
     
    CGImageRelease(cgImage);
     
    self.imageView.image = codeImage;
}

扫描二维码:

 

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
//  Created by 韩俊强 on 15/11/27.
//  Copyright (c) 2015年 韩俊强. All rights reserved.
//
 
#import"RootViewController.h"
#import
 
@interfaceRootViewController ()vcapturemetadataoutputobjectsdelegate,uialertviewdelegate>
 
@property(nonatomic, strong) UIView *scanRectView;
// 硬件设备
@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
 
@implementationRootViewController
 
- (void)viewDidLoad {
    [superviewDidLoad];
    CGSize windowSize = [UIScreen mainScreen].bounds.size;
     
    CGSize scanSize = CGSizeMake(windowSize.width*3/4, windowSize.width*3/4);
    CGRect scanRect = CGRectMake((windowSize.width-scanSize.width)/2, (windowSize.height-scanSize.height)/2, scanSize.width, scanSize.height);
     
    scanRect = CGRectMake(scanRect.origin.y/windowSize.height, scanRect.origin.x/windowSize.width, scanRect.size.height/windowSize.height,scanRect.size.width/windowSize.width);
     
    self.device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
     
    self.input = [AVCaptureDeviceInput deviceInputWithDevice:self.device error:nil];
     
    self.output = [[AVCaptureMetadataOutput alloc]init];
    [self.output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
     
    self.session = [[AVCaptureSession alloc]init];
    [self.session setSessionPreset:([UIScreen mainScreen].bounds.size.height<500)?AVCaptureSessionPreset640x480:AVCaptureSessionPresetHigh];
    [self.session addInput:self.input];
    [self.session addOutput:self.output];
    self.output.metadataObjectTypes=@[AVMetadataObjectTypeQRCode];
    self.output.rectOfInterest = scanRect;
     
    self.preview = [AVCaptureVideoPreviewLayer layerWithSession:self.session];
    self.preview.videoGravity = AVLayerVideoGravityResizeAspectFill;
    self.preview.frame = [UIScreen mainScreen].bounds;
    [self.view.layer insertSublayer:self.preview atIndex:0];
     
    self.scanRectView = [UIViewnew];
    [self.view addSubview:self.scanRectView];
    self.scanRectView.frame = CGRectMake(0,0, scanSize.width, scanSize.height);
    self.scanRectView.center = CGPointMake(CGRectGetMidX([UIScreen mainScreen].bounds), CGRectGetMidY([UIScreen mainScreen].bounds));
    self.scanRectView.layer.borderColor = [UIColor redColor].CGColor;
    self.scanRectView.layer.borderWidth =1;
     
     
    //开始捕获
    [self.session startRunning];
     
}
 
 
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection
{
    if( (metadataObjects.count==0) )
    {
        return;
    }
     
    if(metadataObjects.count>0) {
         
        [self.session stopRunning];
         
        AVMetadataMachineReadableCodeObject *metadataObject = metadataObjects.firstObject;
        //输出扫描字符串
         
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:metadataObject.stringValue message:@""delegate:self cancelButtonTitle:@"ok"otherButtonTitles: nil];
         
        [alert show];
    }
}
 
- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex
{
    [self.session startRunning];
}</a</avfoundation>

 

最终效果:(由于扫描二维码无法展示效果,所以自己动手真机测试吧!)

\
 

IOS7之前,开发者进行扫码编程时,一般会借助第三方库。常用的是ZBarSDK,IOS7之后,系统的AVMetadataObject类中,为我们提供了解析二维码的接口。经过测试,使用原生API扫描和处理的效率非常高,远远高于第三方库。

 

一、使用方法示例

 

官方提供的接口非常简单,代码如下:

77
关闭
先学习,后交费申请表
每期5位名额
在线咨询
免费电话
QQ联系
先学习,后交费
TOP
您好,您想咨询哪门课程呢?
关于我们
机构简介
官方资讯
地理位置
联系我们
0513-91107100
周一至周六     8:30-21:00
微信扫我送教程
手机端访问
南通科迅教育信息咨询有限公司     苏ICP备15009282号     联系地址:江苏省南通市人民中路23-6号新亚大厦三楼             法律顾问:江苏瑞慈律师事务所     Copyright 2008-