iOS二維碼掃描 原生API 原始碼Demo 2016最新版本 簡單易用
阿新 • • 發佈:2019-01-09
iOS中的二維碼掃描,ZXing, ZBar庫不更新了,本博主,所以今天寫了一個二維碼掃描庫,是最新版本的,相容iOS7.0及以後的系統,主要用Objective-C寫的
現測試已完美通過Xcode 7.3的IDE和iOS 7.0及以後的系統,識別率相當高
以後準備在寫一個Swift版本的
有興趣的小夥伴一定要試試哦
先看效果圖
本庫完全使用的是系統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.通過攝像頭掃描二維碼,掃描一次返回一個結果,不結束當前掃描控制器
3.通過攝像頭掃描,掃描一次返回一個結果,會結束擋牆控制器,並且它的view可以自定義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]; } }];
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和資料字串,中間帶一個icon
UIImage *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上提問,博主會在第一時間解答。一般人我不告訴他
以下是自定義掃描樣式