iOS二维码扫描 原生API 源码Demo 2016最新版本 简单易用

来源:互联网 发布:淘宝图片热点链接工具 编辑:程序博客网 时间:2024/05/20 20:17


iOS中的二维码扫描,ZXing, ZBar库不更新了,本博主,所以今天写了一个二维码扫描库,是最新版本的,兼容iOS7.0及以后的系统,主要用Objective-C写的


现测试已完美通过Xcode 7.3的IDE和iOS 7.0及以后的系统,识别率相当高

以后准备在写一个Swift版本的

有兴趣的小伙伴一定要试试哦


先看效果图

   




Github上的名称 ZRQRCodeViewController


本库完全使用的是系统API的一个归类


可以使用Cocoapods集成到项目中


集成用法    podfile文件如下

source 'https://github.com/VictorZhang2014/ZRQRCodeViewController'platform :ios, '7.0'  pod 'ZRQRCodeViewController', '~>3.1.2'

接着运行,以下命令

$ pod install


封装的方法一共有六种类型

1.打开一个控制器,通过摄像头来扫描

2.从手机图片库选择一张含二维码的图片,进行识别

3.长按含二维码的图片进行识别

4.给定一个UIImage既可识别二维码

5.生成二维码图片,带logo或者不带logo的

6.识别后的播放的声音可以自定义


方法用法如下:

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

<span style="font-size:18px;">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];    }}];</span>


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

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];     }}];


3.通过摄像头扫描,扫描一次返回一个结果,会结束挡墙控制器,并且它的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];        }    }];


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

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];         }    }];}];

5.通过长按要被扫描的对象,它可以是这些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];        }    }];}];


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

//1.导入头文件#import "ZRQRCodeScanView.h"//2.调用超简单[[[ZRQRCodeScanView alloc] init] openQRCodeScan:self];


7.生成二维码

//指定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"];

8.生成二维码 带 中间icon

//指定UIImageView 的 rect 大小CGRect rect = CGRectMake(10, 10, [UIScreen mainScreen].bounds.size.width - 20, [UIScreen mainScreen].bounds.size.width - 20);//然后,返回一个QRCode图片,通过指定大小的rect和数据字符串,中间带一个iconUIImage *center = [UIImage imageNamed:@"centericon"];UIImageView *myImage = [[[ZRQRCodeViewController alloc] init] generateQuickResponseCodeWithFrame:rect dataString:@"https://www.baidu.com" centerImage:center];


9.生成二维码 带 中间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];




好了,就是这么简单,读者若有任何问题,请在github上提问,博主会在第一时间解答。安静一般人我不告诉他大笑



以下是自定义扫描样式








0 0