ZRQRCodeViewController 3.1.5

ZRQRCodeViewController 3.1.5

测试已测试
语言语言 Obj-CObjective C
许可 MIT
发布最新发布2016 年 10 月

Victor Zhang 维护。



  • Victor Zhang

这是一个令人愉悦的 QR 码扫描框架,兼容 iOS 7.0 及更高版本。它具有高精度扫描 QR 码。它可以扫描 QR 码和条形码。

特效照片

ZRQRCodeViewController Effect Photo 1 ZRQRCodeViewController Effect Photo 2 ZRQRCodeViewController Effect Photo 3

滚动到最下面有中文说明

CSDN 博文

新增功能?

1. 扫描 QR 码 2. 扫描条形码 3. 从相册识别 4. 长按 QR 码图片识别 5. 通过 UIImage 识别 6. 生成带或不含标志的 QR 码 7. 扫描成功时自定义声音名称

如何开始使用


安装


CocoaPods 是 Objective-C 的依赖管理器,它自动简化了在项目中使用类似 ZRQRCodeViewController 的第三方库的过程。

$ gem install cocoapods

需要 Cocoapods 1.0.0+ 才能构建 ZRQRCodeViewController 3.1.5。

Podfile

要将 ZRQRCodeViewController 集成到您的 Xcode 项目中使用 Cocoapods,请在此指定它:

source 'https://github.com/VictorZhang2014/ZRQRCodeViewController'
platform :ios, '7.0'  

pod 'ZRQRCodeViewController', '~>3.1.5'

然后,运行以下命令:

$ pod install

用法


使用相机扫描 QR 码,一旦扫描出结果,将关闭当前控制器

ZRQRCodeViewController *qrCode = [[ZRQRCodeViewController alloc] initWithScanType:ZRQRCodeScanTypeReturn];
qrCode.qrCodeNavigationTitle = @"QR Code Scanning";
[qrCode QRCodeScanningWithViewController:self completion:^(NSString *strValue) {
   NSLog(@"strValue = %@ ", strValue);
   if([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:strValue]]){
       [[UIApplication sharedApplication] openURL:[NSURL URLWithString:strValue]];
   } else {
       UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Ooooops!" message:[NSString stringWithFormat:@"The result is %@", strValue] delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:nil, nil];
       [alertView show];
   }
}];

使用相机扫描 QR 码,一旦扫描出结果,不会关闭当前控制器

ZRQRCodeViewController *qrCode = [[ZRQRCodeViewController alloc] initWithScanType:ZRQRCodeScanTypeContinuation];
[qrCode QRCodeScanningWithViewController:self completion:^(NSString *strValue) {
   NSLog(@"strValue = %@ ", strValue);
   if([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:strValue]]){
       [[UIApplication sharedApplication] openURL:[NSURL URLWithString:strValue]];
   } else {
       UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Ooooops!" message:[NSString stringWithFormat:@"The result is %@", strValue] delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:nil, nil];
       [alertView show];
   }
}];

使用相机扫描 QR 码,一旦扫描出结果,将关闭当前控制器。其视图可以自定义

    UIColor *white = [UIColor whiteColor];
    ZRQRCodeViewController *qrCode = [[ZRQRCodeViewController alloc] initWithScanType:ZRQRCodeScanTypeReturn customView:self.view navigationBarTitle:@"QR Code"];
    qrCode.VCTintColor = white; 
    [qrCode QRCodeScanningWithViewController:self completion:^(NSString *strValue) {
        NSLog(@"strValue = %@ ", strValue);
        if([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:strValue]]){
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:strValue]];
        } else {
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Ooooops!" message:[NSString stringWithFormat:@"The result is %@", strValue] delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:nil, nil];
            [alertView show];
        }
    }];

从相册扫描 QR 码

ZRQRCodeViewController *qrCode = [[ZRQRCodeViewController alloc] initWithScanType:ZRQRCodeScanTypeReturn];
qrCode.textWhenNotRecognized = @"No any QR Code texture on the picture were found!";
[qrCode recognizationByPhotoLibraryViewController:self completion:^(NSString *strValue) {
   NSLog(@"strValue = %@ ", strValue);
   [[ZRAlertController defaultAlert] alertShow:self title:@"" message:[NSString stringWithFormat:@"Result: %@", strValue] okayButton:@"Ok" completion:^{
        if([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:strValue]]){
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:strValue]];
        } else {
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Ooooops!" message:[NSString stringWithFormat:@"The result is %@", strValue] delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:nil, nil];
            [alertView show];
        }
    }];
}];

长按对象进行二维码扫描,其中的一个之一,UIImageView, UIButton, UIWebView, WKWebView, UIView, UIViewController

注意:先绑定长按手势,然后再使用。

       Like this variable `self.imageViewExample` that 
       it needs the event of long-press. You can bind the gesture in the method `viewDidLoad()`.
ZRQRCodeViewController *qrCode = [[ZRQRCodeViewController alloc] initWithScanType:ZRQRCodeScanTypeReturn];
qrCode.cancelButton = @"Cancel";
qrCode.actionSheets = @[];
qrCode.extractQRCodeText = @"Extract QR Code";
NSString *savedImageText = @"Save Image";
qrCode.saveImaegText = savedImageText;
[qrCode extractQRCodeByLongPressViewController:self Object:self.imageViewExample actionSheetCompletion:^(int index, NSString * _Nonnull value) {
    if ([value isEqualToString:savedImageText]) {
       [[ZRAlertController defaultAlert] alertShow:self title:@"" message:@"Saved Image Successfully!" okayButton:@"Ok" completion:^{ }];
    }
} completion:^(NSString * _Nonnull strValue) {
    NSLog(@"strValue = %@ ", strValue);
    [[ZRAlertController defaultAlert] alertShow:self title:@"" message:[NSString stringWithFormat:@"Result: %@", strValue] okayButton:@"Ok" completion:^{
        if([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:strValue]]){
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:strValue]];
        } else {
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Ooooops!" message:[NSString stringWithFormat:@"The result is %@", strValue] delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:nil, nil];
            [alertView show];
        }
    }];
}];

通过自定义视图进行二维码扫描,其中有一个示例视图名为 ZRQRCodeScanView,下面是片段代码。在此项目中有一个完整的代码

//1.You just import this header file
#import "ZRQRCodeScanView.h"

//2.Easy to call
[[[ZRQRCodeScanView alloc] init] openQRCodeScan:self];

将二维码生成图片

//Indicate UIImageView rect
CGRect rect = CGRectMake(10, 10, [UIScreen mainScreen].bounds.size.width - 20, [UIScreen mainScreen].bounds.size.width - 20);

//And then returns a UIImageView which is QRCode picture with rect through indicating data string
UIImageView *myImage = [[[ZRQRCodeViewController alloc] init] generateQuickResponseCodeWithFrame:rect dataString:@"https://www.baidu.com"];

在中心图标中生成二维码图片

//Indicate UIImageView rect
CGRect rect = CGRectMake(10, 10, [UIScreen mainScreen].bounds.size.width - 20, [UIScreen mainScreen].bounds.size.width - 20);

//And then returns a UIImageView which is QRCode picture with rect through indicating data string and amidst a icon
UIImage *center = [UIImage imageNamed:@"centericon"];
UIImageView *myImage = [[[ZRQRCodeViewController alloc] init] generateQuickResponseCodeWithFrame:rect dataString:@"https://www.baidu.com" centerImage:center];

在中心图标中生成二维码图片并具有阴影效果

//Indicate UIImageView rect
CGRect rect = CGRectMake(10, 10, [UIScreen mainScreen].bounds.size.width - 20, [UIScreen mainScreen].bounds.size.width - 20);

//And then returns a UIImageView which is QRCode picture with rect through indicating data string and amidst a icon and shadow effect
UIImage *center = [UIImage imageNamed:@"centericon"];
UIImageView *myImage = [[[ZRQRCodeViewController alloc] init] generateQuickResponseCodeWithFrame:rect dataString:@"https://www.baidu.com" centerImage:center needShadow:YES];

ZRQRCodeViewController

ZRQRCodeViewController 是一个非常好用的二维码扫描框架,兼容 iOS 7.0 及以后的系统版本。它具有非常精确的扫描能力,可以识别二维码和条形码。

CSDN 博文

如何开始


安装


CocoaPods CocoaPods 是一个 Objective-C 的库文件依赖管理器,它会自动的、简单化的集成第三方库到你的项目,例如 ZRQRCodeViewController。

$ gem install cocoapods

需要 Cocoapods 1.0.0+ 才能构建 ZRQRCodeViewController 3.1.5。

Podfile

使用 CocoaPods 把 ZRQRCodeViewController 库集成到你的项目,Podfile 文件内容如下

source 'https://github.com/VictorZhang2014/ZRQRCodeViewController'
platform :ios, '7.0'  

pod 'ZRQRCodeViewController', '~>3.1.5'

接着,运行以下命令

$ pod install

使用方法


通过摄像头扫描二维码,扫描一次返回一个结果,并结束当前扫描控制器

ZRQRCodeViewController *qrCode = [[ZRQRCodeViewController alloc] initWithScanType:ZRQRCodeScanTypeReturn];
qrCode.qrCodeNavigationTitle = @"QR Code Scanning";
[qrCode QRCodeScanningWithViewController:self completion:^(NSString *strValue) {
    NSLog(@"strValue = %@ ", strValue);
    if([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:strValue]]){
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:strValue]];
    } else {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Ooooops!" message:[NSString stringWithFormat:@"The result is %@", strValue] delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:nil, nil];
        [alertView show];
    }
}];

通过摄像头扫描二维码,扫描一次返回一个结果,不结束当前扫描控制器

ZRQRCodeViewController *qrCode = [[ZRQRCodeViewController alloc] initWithScanType:ZRQRCodeScanTypeContinuation];
[qrCode QRCodeScanningWithViewController:self completion:^(NSString *strValue) {
     NSLog(@"strValue = %@ ", strValue);
     if([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:strValue]]){
          [[UIApplication sharedApplication] openURL:[NSURL URLWithString:strValue]];
     } else {
          UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Ooooops!" message:[NSString stringWithFormat:@"The result is %@", strValue] delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:nil, nil];
          [alertView show];
     }
}];

通过摄像头扫描,扫描一次返回一个结果,会结束遮挡控制器,并且它的 view 可以自定义

    UIColor *white = [UIColor whiteColor];
    ZRQRCodeViewController *qrCode = [[ZRQRCodeViewController alloc] initWithScanType:ZRQRCodeScanTypeReturn customView:self.view navigationBarTitle:@"QR Code"];
    qrCode.VCTintColor = white; 
    [qrCode QRCodeScanningWithViewController:self completion:^(NSString *strValue) {
        NSLog(@"strValue = %@ ", strValue);
        if([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:strValue]]){
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:strValue]];
        } else {
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Ooooops!" message:[NSString stringWithFormat:@"The result is %@", strValue] delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:nil, nil];
            [alertView show];
        }
    }];

从手机相册中选择一张图片进行扫描,

ZRQRCodeViewController *qrCode = [[ZRQRCodeViewController alloc] initWithScanType:ZRQRCodeScanTypeReturn];
qrCode.textWhenNotRecognized = @"No any QR Code texture on the picture were found!";
[qrCode recognizationByPhotoLibraryViewController:self completion:^(NSString *strValue) {
    NSLog(@"strValue = %@ ", strValue);
    [[ZRAlertController defaultAlert] alertShow:self title:@"" message:[NSString stringWithFormat:@"Result: %@", strValue] okayButton:@"Ok" completion:^{
         if([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:strValue]]){
             [[UIApplication sharedApplication] openURL:[NSURL URLWithString:strValue]];
         } else {
             UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Ooooops!" message:[NSString stringWithFormat:@"The result is %@", strValue] delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:nil, nil];
             [alertView show];
         }
    }];
}];

通过长按要被扫描的对象,它可以是这些 UIImageView, UIButton, UIWebView, WKWebView, UIView, UIViewController

注意:先绑定长按手势,再使用。

例如这个变量,self.imageViewExample 它需要长按事件。可以在 viewDidLoad()方法中绑定该手势

ZRQRCodeViewController *qrCode = [[ZRQRCodeViewController alloc] initWithScanType:ZRQRCodeScanTypeReturn];
qrCode.cancelButton = @"Cancel";
qrCode.actionSheets = @[];
qrCode.extractQRCodeText = @"Extract QR Code";
NSString *savedImageText = @"Save Image";
qrCode.saveImaegText = savedImageText;
[qrCode extractQRCodeByLongPressViewController:self Object:self.imageViewExample actionSheetCompletion:^(int index, NSString * _Nonnull value) {
    if ([value isEqualToString:savedImageText]) {
         [[ZRAlertController defaultAlert] alertShow:self title:@"" message:@"Saved Image Successfully!" okayButton:@"Ok" completion:^{ }];
    }
} completion:^(NSString * _Nonnull strValue) {
    NSLog(@"strValue = %@ ", strValue);
    [[ZRAlertController defaultAlert] alertShow:self title:@"" message:[NSString stringWithFormat:@"Result: %@", strValue] okayButton:@"Ok" completion:^{
        if([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:strValue]]){
             [[UIApplication sharedApplication] openURL:[NSURL URLWithString:strValue]];
        } else {
             UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Ooooops!" message:[NSString stringWithFormat:@"The result is %@", strValue] delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:nil, nil];
             [alertView show];
        }
    }];
}];

二维码扫描可以通过自定义View,这里有一个样例,文件名是ZRQRCodeScanView,以下是使用代码,完整的代码请试运行该项目

//1.导入头文件
#import "ZRQRCodeScanView.h"

//2.调用超简单
[[[ZRQRCodeScanView alloc] init] openQRCodeScan:self];

生成二维码

//指定UIImageView 的 rect 大小
CGRect rect = CGRectMake(10, 10, [UIScreen mainScreen].bounds.size.width - 20, [UIScreen mainScreen].bounds.size.width - 20);

//然后,返回一个QRCode图片,通过指定大小的rect和数据字符串
UIImageView *myImage = [[[ZRQRCodeViewController alloc] init] generateQuickResponseCodeWithFrame:rect dataString:@"https://www.baidu.com"];

生成二维码 带 中间icon

//指定UIImageView 的 rect 大小
CGRect rect = CGRectMake(10, 10, [UIScreen mainScreen].bounds.size.width - 20, [UIScreen mainScreen].bounds.size.width - 20);

//然后,返回一个QRCode图片,通过指定大小的rect和数据字符串,中间带一个icon
UIImage *center = [UIImage imageNamed:@"centericon"];
UIImageView *myImage = [[[ZRQRCodeViewController alloc] init] generateQuickResponseCodeWithFrame:rect dataString:@"https://www.baidu.com" centerImage:center];

生成二维码 带 中间icon ,并且带有阴影效果

//指定UIImageView 的 rect 大小
CGRect rect = CGRectMake(10, 10, [UIScreen mainScreen].bounds.size.width - 20, [UIScreen mainScreen].bounds.size.width - 20);

//然后,返回一个QRCode图片,通过指定大小的rect和数据字符串,中间带一个icon, 并且有阴影效果
UIImage *center = [UIImage imageNamed:@"centericon"];
UIImageView *myImage = [[[ZRQRCodeViewController alloc] init] generateQuickResponseCodeWithFrame:rect dataString:@"https://www.baidu.com" centerImage:center needShadow:YES];