From 482dd4459730b4317035e9b78a45600e73dec1d5 Mon Sep 17 00:00:00 2001 From: kingsic Date: Mon, 5 Jul 2021 21:34:05 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E7=89=88=E6=9C=AC=E9=87=8D=E6=9E=84?= =?UTF-8?q?=E5=8F=8A=E4=BF=AE=E5=A4=8D=20[#163]=20=E9=97=AE=E9=A2=98?= =?UTF-8?q?=E3=80=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 20 +- SGQRCode.podspec | 4 +- SGQRCode/SGAuthorization.h | 39 +++ SGQRCode/SGAuthorization.m | 126 ++++++++++ SGQRCode/SGCreateCode.h | 49 ++++ SGQRCode/SGCreateCode.m | 71 ++++++ ...RCodeScanEndSound.caf => scanEndSound.caf} | Bin ...{QRCodeScanLine@2x.png => scanLine@2x.png} | Bin ...{QRCodeScanLine@3x.png => scanLine@3x.png} | Bin ...canLineGrid@2x.png => scanLineGrid@2x.png} | Bin ...canLineGrid@3x.png => scanLineGrid@3x.png} | Bin SGQRCode/SGQRCode.h | 8 +- SGQRCode/SGQRCodeManager.h | 99 -------- SGQRCode/SGScanCode.h | 56 +++++ SGQRCode/{SGQRCodeManager.m => SGScanCode.m} | 223 ++++-------------- SGQRCode/{SGQRCodeScanView.h => SGScanView.h} | 36 +-- SGQRCode/{SGQRCodeScanView.m => SGScanView.m} | 43 ++-- SGQRCodeExample.xcodeproj/project.pbxproj | 36 ++- SGQRCodeExample/Controller/WBQRCodeVC.m | 51 ++-- SGQRCodeExample/Controller/WCQRCodeVC.m | 64 +++-- SGQRCodeExample/ViewController.m | 35 +-- 21 files changed, 547 insertions(+), 413 deletions(-) create mode 100644 SGQRCode/SGAuthorization.h create mode 100644 SGQRCode/SGAuthorization.m create mode 100644 SGQRCode/SGCreateCode.h create mode 100644 SGQRCode/SGCreateCode.m rename SGQRCode/SGQRCode.bundle/{QRCodeScanEndSound.caf => scanEndSound.caf} (100%) rename SGQRCode/SGQRCode.bundle/{QRCodeScanLine@2x.png => scanLine@2x.png} (100%) rename SGQRCode/SGQRCode.bundle/{QRCodeScanLine@3x.png => scanLine@3x.png} (100%) rename SGQRCode/SGQRCode.bundle/{QRCodeScanLineGrid@2x.png => scanLineGrid@2x.png} (100%) rename SGQRCode/SGQRCode.bundle/{QRCodeScanLineGrid@3x.png => scanLineGrid@3x.png} (100%) delete mode 100644 SGQRCode/SGQRCodeManager.h create mode 100644 SGQRCode/SGScanCode.h rename SGQRCode/{SGQRCodeManager.m => SGScanCode.m} (55%) rename SGQRCode/{SGQRCodeScanView.h => SGScanView.h} (54%) rename SGQRCode/{SGQRCodeScanView.m => SGScanView.m} (94%) diff --git a/README.md b/README.md index ce0b0ca..4af37ba 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ * `QQ群:825339547` -* `注意:3.5.0 版本重构:API 功能的拓展及扫码识别优化` +* `注意:3.5.1 版本重构:API 功能的拓展及扫码识别优化` ## 主要内容的介绍 @@ -24,7 +24,7 @@ ## SGQRCode 集成 -* 1、CocoaPods 导入 pod 'SGQRCode', '~> 3.5.0' +* 1、CocoaPods 导入 pod 'SGQRCode', '~> 3.5.1' * 2、下载、拖拽 “SGQRCode” 文件夹到工程中 @@ -41,27 +41,27 @@ ```Objective-C    /// 创建二维码扫描类 -   manager = [SGQRCodeManager QRCodeManager]; +   scanCode = [SGScanCode scanCode];    /// 二维码扫描回调方法 -    [manager scanWithController:self resultBlock:^(SGQRCodeManager *manager, NSString *result) { +    [scanCode scanWithController:self resultBlock:^(SGScanCode *scanCode, NSString *result) {     <#code#> }]; /// 开启二维码扫描回调方法: 需手动开启 - [manager startRunningWithBefore:^{ + [scanCode startRunningWithBefore:^{ // 在此可添加 HUD } completion:^{ // 在此可移除 HUD }]; /// 外界光线强弱值回调方法 - [manager scanWithBrightnessBlock:^(SGQRCodeManager *manager, CGFloat brightness) { + [scanCode scanWithBrightnessBlock:^(SGScanCode *scanCode, CGFloat brightness) { <#code#> }];    /// 从相册中读取二维码回调方法 -    [manager readWithResultBlock:^(SGQRCodeManager *manager, NSString *result) { +    [scanCode readWithResultBlock:^(SGScanCode *scanCode, NSString *result) {     <#code#> }]; ``` @@ -70,10 +70,10 @@ ```Objective-C /// 常规二维码 - _imageView.image = [SGQRCodeObtain generateQRCodeWithData:@"https://github.com/kingsic" size:size]; + _imageView.image = [SGCreateCode createQRCodeWithData:@"https://github.com/kingsic" size:size]; /// 带 logo 的二维码 - _imageView.image = [SGQRCodeObtain generateQRCodeWithData:@"https://github.com/kingsic" size:size logoImage:logoImage ratio:ratio]; + _imageView.image = [SGCreateCode createQRCodeWithData:@"https://github.com/kingsic" size:size logoImage:logoImage ratio:ratio]; ``` @@ -119,6 +119,8 @@ * 2021-05-30 :v3.5.0 版本重构:API 功能的拓展及扫码识别优化,更多内容请在 [releases](https://github.com/kingsic/SGQRCode/releases/tag/3.5.0) 中查看 +* 2021-05-30 :v3.5.1 版本重构:修复 [#163] 问题,更多内容请在 [releases](https://github.com/kingsic/SGQRCode/releases/tag/3.5.1) 中查看 + ## License SGQRCode is released under the Apache License 2.0. See [LICENSE](https://github.com/kingsic/SGQRCode/blob/master/LICENSE) for details. diff --git a/SGQRCode.podspec b/SGQRCode.podspec index 8fb229d..8889232 100644 --- a/SGQRCode.podspec +++ b/SGQRCode.podspec @@ -1,8 +1,8 @@ Pod::Spec.new do |s| s.name = 'SGQRCode' - s.version = '3.5.0' - s.summary = 'An easy way to use BarCode and QRCode scan library for iOS' + s.version = '3.5.1' + s.summary = 'The easy to use bar code and QR code scan library for iOS' s.homepage = 'https://github.com/kingsic/SGQRCode' s.license = 'Apache-2.0' s.authors = {'kingsic' => 'kingsic@126.com'} diff --git a/SGQRCode/SGAuthorization.h b/SGQRCode/SGAuthorization.h new file mode 100644 index 0000000..7d8c8df --- /dev/null +++ b/SGQRCode/SGAuthorization.h @@ -0,0 +1,39 @@ +// +// SGAuthorization.h +// SGQRCodeExample +// +// Created by kingsic on 2021/7/5. +// Copyright © 2021年 kingsic. All rights reserved. +// + +#import +@class SGAuthorization; + +NS_ASSUME_NONNULL_BEGIN + +typedef enum : NSUInteger { + /// 授权成功(第一次授权允许及已授权) + SGAuthorizationStatusSuccess, + /// 授权失败(已拒绝) + SGAuthorizationStatusFail, + /// 未知(受限制) + SGAuthorizationStatusUnknown, +} SGAuthorizationStatus; + +typedef void(^SGAVAuthorizationBlock)(SGAuthorization *authorization, SGAuthorizationStatus status); +typedef void(^SGPHAuthorizationBlock)(SGAuthorization *authorization, SGAuthorizationStatus status); + +@interface SGAuthorization : NSObject +/** 类方法创建 */ ++ (instancetype)authorization; +/** 打印信息,默认为:NO */ +@property (nonatomic, assign) BOOL openLog; + +/** 相机授权回调方法 */ +- (void)AVAuthorizationBlock:(SGAVAuthorizationBlock)block; +/** 相册授权回调方法 */ +- (void)PHAuthorizationBlock:(SGPHAuthorizationBlock)block; + +@end + +NS_ASSUME_NONNULL_END diff --git a/SGQRCode/SGAuthorization.m b/SGQRCode/SGAuthorization.m new file mode 100644 index 0000000..4b3df80 --- /dev/null +++ b/SGQRCode/SGAuthorization.m @@ -0,0 +1,126 @@ +// +// SGAuthorization.h +// SGQRCodeExample +// +// Created by kingsic on 2021/7/5. +// Copyright © 2021年 kingsic. All rights reserved. +// + +#import "SGAuthorization.h" +#import +#import + +@implementation SGAuthorization + +/** 类方法创建 */ ++ (instancetype)authorization { + return [[self alloc] init]; +} + +/** 相机授权回调方法 */ +- (void)AVAuthorizationBlock:(SGAVAuthorizationBlock)block { + AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo]; + switch (status) { + // 授权状态未确定 + case AVAuthorizationStatusNotDetermined: { + [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) { + if (granted) { + dispatch_async(dispatch_get_main_queue(), ^{ + if (block) { + block(self, SGAuthorizationStatusSuccess); + } + }); + if (self.openLog) { + NSLog(@"用户第一次允许访问相机权限"); + } + } else { + if (self.openLog) { + NSLog(@"用户第一次拒绝访问相机权限"); + } + } + }]; + break; + } + // 已授权 + case AVAuthorizationStatusAuthorized: { + if (block) { + block(self, SGAuthorizationStatusSuccess); + } + if (self.openLog) { + NSLog(@"用户已允许访问相机权限"); + } + break; + } + // 已拒绝 + case AVAuthorizationStatusDenied: { + if (block) { + block(self, SGAuthorizationStatusFail); + } + if (self.openLog) { + NSLog(@"用户已拒绝访问相机权限"); + } + break; + } + // 受限制 + case AVAuthorizationStatusRestricted: { + if (block) { + block(self, SGAuthorizationStatusUnknown); + } + if (self.openLog) { + NSLog(@"系统原因, 无法访问"); + } + break; + } + + default: + break; + } + return; +} + +/** 相册授权回调方法 */ +- (void)PHAuthorizationBlock:(SGPHAuthorizationBlock)block { + PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus]; + if (status == PHAuthorizationStatusNotDetermined) { + [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) { + if (status == PHAuthorizationStatusAuthorized) { + dispatch_async(dispatch_get_main_queue(), ^{ + if (block) { + block(self, SGAuthorizationStatusSuccess); + } + }); + if (self.openLog == YES) { + NSLog(@"用户第一次同意访问相册权限"); + } + } else { + if (self.openLog == YES) { + NSLog(@"用户第一次拒绝访问相册权限"); + } + } + }]; + } else if (status == PHAuthorizationStatusAuthorized) { + if (block) { + block(self, SGAuthorizationStatusSuccess); + } + if (self.openLog == YES) { + NSLog(@"用户已允许访问相册权限"); + } + } else if (status == PHAuthorizationStatusDenied) { + if (block) { + block(self, SGAuthorizationStatusFail); + } + if (self.openLog) { + NSLog(@"用户已拒绝访问相册权限"); + } + } else if (status == PHAuthorizationStatusRestricted) { + if (block) { + block(self, SGAuthorizationStatusUnknown); + } + if (self.openLog) { + NSLog(@"系统原因, 无法访问"); + } + } +} + + +@end diff --git a/SGQRCode/SGCreateCode.h b/SGQRCode/SGCreateCode.h new file mode 100644 index 0000000..ab5b0fb --- /dev/null +++ b/SGQRCode/SGCreateCode.h @@ -0,0 +1,49 @@ +// +// SGCreateCode.h +// SGQRCodeExample +// +// Created by kingsic on 2021/7/5. +// Copyright © 2021 Sorgle. All rights reserved. +// + +#import + +NS_ASSUME_NONNULL_BEGIN + +@interface SGCreateCode : NSObject +/** 生成二维码 */ ++ (UIImage *)createQRCodeWithData:(NSString *)data size:(CGFloat)size; +/** + * 生成二维码(自定义颜色) + * + * @param data 二维码数据 + * @param size 二维码大小 + * @param color 二维码颜色 + * @param backgroundColor 二维码背景颜色 + */ ++ (UIImage *)createQRCodeWithData:(NSString *)data size:(CGFloat)size color:(UIColor *)color backgroundColor:(UIColor *)backgroundColor; +/** + * 生成带 logo 的二维码(推荐使用) + * + * @param data 二维码数据 + * @param size 二维码大小 + * @param logoImage logo + * @param ratio logo 相对二维码的比例(取值范围 0.0 ~ 0.5f) + */ ++ (UIImage *)createQRCodeWithData:(NSString *)data size:(CGFloat)size logoImage:(UIImage *)logoImage ratio:(CGFloat)ratio; +/** + * 生成带 logo 的二维码(拓展) + * + * @param data 二维码数据 + * @param size 二维码大小 + * @param logoImage logo + * @param ratio logo 相对二维码的比例(取值范围 0.0 ~ 0.5f) + * @param logoImageCornerRadius logo 外边框圆角(取值范围 0.0 ~ 10.0f) + * @param logoImageBorderWidth logo 外边框宽度(取值范围 0.0 ~ 10.0f) + * @param logoImageBorderColor logo 外边框颜色 + */ ++ (UIImage *)createQRCodeWithData:(NSString *)data size:(CGFloat)size logoImage:(UIImage *)logoImage ratio:(CGFloat)ratio logoImageCornerRadius:(CGFloat)logoImageCornerRadius logoImageBorderWidth:(CGFloat)logoImageBorderWidth logoImageBorderColor:(UIColor *)logoImageBorderColor; + +@end + +NS_ASSUME_NONNULL_END diff --git a/SGQRCode/SGCreateCode.m b/SGQRCode/SGCreateCode.m new file mode 100644 index 0000000..95f092d --- /dev/null +++ b/SGQRCode/SGCreateCode.m @@ -0,0 +1,71 @@ +// +// SGCreateCode.m +// SGQRCodeExample +// +// Created by kingsic on 2021/7/5. +// Copyright © 2021 Sorgle. All rights reserved. +// + +#import "SGCreateCode.h" + +@implementation SGCreateCode + ++ (UIImage *)createQRCodeWithData:(NSString *)data size:(CGFloat)size { + return [self createQRCodeWithData:data size:size color:[UIColor blackColor] backgroundColor:[UIColor whiteColor]]; +} + ++ (UIImage *)createQRCodeWithData:(NSString *)data size:(CGFloat)size color:(UIColor *)color backgroundColor:(UIColor *)backgroundColor { + NSData *string_data = [data dataUsingEncoding:NSUTF8StringEncoding]; + // 1、二维码滤镜 + CIFilter *fileter = [CIFilter filterWithName:@"CIQRCodeGenerator"]; + [fileter setValue:string_data forKey:@"inputMessage"]; + [fileter setValue:@"H" forKey:@"inputCorrectionLevel"]; + CIImage *ciImage = fileter.outputImage; + // 2、颜色滤镜 + CIFilter *color_filter = [CIFilter filterWithName:@"CIFalseColor"]; + [color_filter setValue:ciImage forKey:@"inputImage"]; + [color_filter setValue:[CIColor colorWithCGColor:color.CGColor] forKey:@"inputColor0"]; + [color_filter setValue:[CIColor colorWithCGColor:backgroundColor.CGColor] forKey:@"inputColor1"]; + // 3、生成处理 + CIImage *outImage = color_filter.outputImage; + CGFloat scale = size / outImage.extent.size.width; + outImage = [outImage imageByApplyingTransform:CGAffineTransformMakeScale(scale, scale)]; + return [UIImage imageWithCIImage:outImage]; +} + ++ (UIImage *)createQRCodeWithData:(NSString *)data size:(CGFloat)size logoImage:(UIImage *)logoImage ratio:(CGFloat)ratio { + return [self createQRCodeWithData:data size:size logoImage:logoImage ratio:ratio logoImageCornerRadius:5 logoImageBorderWidth:5 logoImageBorderColor:[UIColor whiteColor]]; +} + ++ (UIImage *)createQRCodeWithData:(NSString *)data size:(CGFloat)size logoImage:(UIImage *)logoImage ratio:(CGFloat)ratio logoImageCornerRadius:(CGFloat)logoImageCornerRadius logoImageBorderWidth:(CGFloat)logoImageBorderWidth logoImageBorderColor:(UIColor *)logoImageBorderColor { + UIImage *image = [self createQRCodeWithData:data size:size color:[UIColor blackColor] backgroundColor:[UIColor whiteColor]]; + if (logoImage == nil) return image; + if (ratio < 0.0 || ratio > 0.5) { + ratio = 0.25; + } + CGFloat logoImageW = ratio * size; + CGFloat logoImageH = logoImageW; + CGFloat logoImageX = 0.5 * (image.size.width - logoImageW); + CGFloat logoImageY = 0.5 * (image.size.height - logoImageH); + CGRect logoImageRect = CGRectMake(logoImageX, logoImageY, logoImageW, logoImageH); + // 绘制logo + UIGraphicsBeginImageContextWithOptions(image.size, false, [UIScreen mainScreen].scale); + [image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)]; + if (logoImageCornerRadius < 0.0 || logoImageCornerRadius > 10) { + logoImageCornerRadius = 5; + } + UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:logoImageRect cornerRadius:logoImageCornerRadius]; + if (logoImageBorderWidth < 0.0 || logoImageBorderWidth > 10) { + logoImageBorderWidth = 5; + } + path.lineWidth = logoImageBorderWidth; + [logoImageBorderColor setStroke]; + [path stroke]; + [path addClip]; + [logoImage drawInRect:logoImageRect]; + UIImage *QRCodeImage = UIGraphicsGetImageFromCurrentImageContext(); + UIGraphicsEndImageContext(); + return QRCodeImage; +} + +@end diff --git a/SGQRCode/SGQRCode.bundle/QRCodeScanEndSound.caf b/SGQRCode/SGQRCode.bundle/scanEndSound.caf similarity index 100% rename from SGQRCode/SGQRCode.bundle/QRCodeScanEndSound.caf rename to SGQRCode/SGQRCode.bundle/scanEndSound.caf diff --git a/SGQRCode/SGQRCode.bundle/QRCodeScanLine@2x.png b/SGQRCode/SGQRCode.bundle/scanLine@2x.png similarity index 100% rename from SGQRCode/SGQRCode.bundle/QRCodeScanLine@2x.png rename to SGQRCode/SGQRCode.bundle/scanLine@2x.png diff --git a/SGQRCode/SGQRCode.bundle/QRCodeScanLine@3x.png b/SGQRCode/SGQRCode.bundle/scanLine@3x.png similarity index 100% rename from SGQRCode/SGQRCode.bundle/QRCodeScanLine@3x.png rename to SGQRCode/SGQRCode.bundle/scanLine@3x.png diff --git a/SGQRCode/SGQRCode.bundle/QRCodeScanLineGrid@2x.png b/SGQRCode/SGQRCode.bundle/scanLineGrid@2x.png similarity index 100% rename from SGQRCode/SGQRCode.bundle/QRCodeScanLineGrid@2x.png rename to SGQRCode/SGQRCode.bundle/scanLineGrid@2x.png diff --git a/SGQRCode/SGQRCode.bundle/QRCodeScanLineGrid@3x.png b/SGQRCode/SGQRCode.bundle/scanLineGrid@3x.png similarity index 100% rename from SGQRCode/SGQRCode.bundle/QRCodeScanLineGrid@3x.png rename to SGQRCode/SGQRCode.bundle/scanLineGrid@3x.png diff --git a/SGQRCode/SGQRCode.h b/SGQRCode/SGQRCode.h index 2f0b17c..cd89a8f 100755 --- a/SGQRCode/SGQRCode.h +++ b/SGQRCode/SGQRCode.h @@ -1,11 +1,13 @@ // // SGQRCode.h -// Version 3.5.0 +// Version 3.5.1 // https://github.com/kingsic/SGQRCode // // Created by kingsic on 2016/8/16. // Copyright © 2016年 kingsic. All rights reserved. // -#import "SGQRCodeManager.h" -#import "SGQRCodeScanView.h" +#import "SGScanCode.h" +#import "SGScanView.h" +#import "SGCreateCode.h" +#import "SGAuthorization.h" diff --git a/SGQRCode/SGQRCodeManager.h b/SGQRCode/SGQRCodeManager.h deleted file mode 100644 index 3b67316..0000000 --- a/SGQRCode/SGQRCodeManager.h +++ /dev/null @@ -1,99 +0,0 @@ -// -// SGQRCodeObtain.h -// SGQRCodeExample -// -// Created by kingsic on 2016/8/16. -// Copyright © 2016年 kingsic. All rights reserved. -// - -#import -@class SGQRCodeManager; - -typedef enum : NSUInteger { - SGAuthorizationStatusSuccess, - SGAuthorizationStatusFail, - SGAuthorizationStatusUnknown, -} SGAuthorizationStatus; - -typedef void(^SGQRCodeManagerScanResultBlock)(SGQRCodeManager *manager, NSString *result); -typedef void(^SGQRCodeManagerScanBrightnessBlock)(SGQRCodeManager *manager, CGFloat brightness); -typedef void(^SGQRCodeManagerReadResultBlock)(SGQRCodeManager *manager, NSString *result); -typedef void(^SGQRCodeManagerAlbumDidCancelBlock)(SGQRCodeManager *manager); -typedef void(^SGQRCodeManagerAuthorizationBlock)(SGQRCodeManager *manager, SGAuthorizationStatus authorizationStatus); - -@interface SGQRCodeManager : NSObject -/** 扫描区域,默认为整个视图,取值范围:0~1(以屏幕右上角为坐标原点)*/ -@property (nonatomic, assign) CGRect scanArea; -/** 捕获外界光线亮度,默认为:NO */ -@property (nonatomic, assign) BOOL brightness; -/** 判断相册访问权限是否授权 */ -@property (nonatomic, assign) BOOL albumAuthorization; -/** 打印信息,默认为:NO */ -@property (nonatomic, assign) BOOL openLog; - -/** 类方法创建 */ -+ (instancetype)QRCodeManager; - -/** 相机权限访问回调方法 */ -- (void)authorizationStatusBlock:(SGQRCodeManagerAuthorizationBlock)block; -/** 后置摄像头是否可用 */ -- (BOOL)isCameraDeviceRearAvailable; - -/** 扫描二维码回调方法 */ -- (void)scanWithController:(UIViewController *)controller resultBlock:(SGQRCodeManagerScanResultBlock)blcok; -/** 扫描二维码时,捕获外界光线强弱回调方法(brightness = YES 时,此回调方法才有效)*/ -- (void)scanWithBrightnessBlock:(SGQRCodeManagerScanBrightnessBlock)blcok; - -/** 从相册中读取二维码回调方法 */ -- (void)readWithResultBlock:(SGQRCodeManagerReadResultBlock)block; -/** 相册选择控制器取消按钮的点击回调方法 */ -- (void)albumDidCancelBlock:(SGQRCodeManagerAlbumDidCancelBlock)block; - -/** 开启扫描回调 */ -- (void)startRunningWithBefore:(void (^)(void))before completion:(void (^)(void))completion; -/** 停止扫描 */ -- (void)stopRunning; - -#pragma mark - - 生成二维码相关方法 -/** 生成二维码 */ -+ (UIImage *)generateQRCodeWithData:(NSString *)data size:(CGFloat)size; -/** - * 生成二维码(自定义颜色) - * - * @param data 二维码数据 - * @param size 二维码大小 - * @param color 二维码颜色 - * @param backgroundColor 二维码背景颜色 - */ -+ (UIImage *)generateQRCodeWithData:(NSString *)data size:(CGFloat)size color:(UIColor *)color backgroundColor:(UIColor *)backgroundColor; -/** - * 生成带 logo 的二维码(推荐使用) - * - * @param data 二维码数据 - * @param size 二维码大小 - * @param logoImage logo - * @param ratio logo 相对二维码的比例(取值范围 0.0 ~ 0.5f) - */ -+ (UIImage *)generateQRCodeWithData:(NSString *)data size:(CGFloat)size logoImage:(UIImage *)logoImage ratio:(CGFloat)ratio; -/** - * 生成带 logo 的二维码(拓展) - * - * @param data 二维码数据 - * @param size 二维码大小 - * @param logoImage logo - * @param ratio logo 相对二维码的比例(取值范围 0.0 ~ 0.5f) - * @param logoImageCornerRadius logo 外边框圆角(取值范围 0.0 ~ 10.0f) - * @param logoImageBorderWidth logo 外边框宽度(取值范围 0.0 ~ 10.0f) - * @param logoImageBorderColor logo 外边框颜色 - */ -+ (UIImage *)generateQRCodeWithData:(NSString *)data size:(CGFloat)size logoImage:(UIImage *)logoImage ratio:(CGFloat)ratio logoImageCornerRadius:(CGFloat)logoImageCornerRadius logoImageBorderWidth:(CGFloat)logoImageBorderWidth logoImageBorderColor:(UIColor *)logoImageBorderColor; - -/** 播放音效文件 */ -- (void)playSoundName:(NSString *)name; - -/** 打开手电筒 */ -- (void)turnOnFlashlight; -/** 关闭手电筒 */ -- (void)turnOffFlashlight; - -@end diff --git a/SGQRCode/SGScanCode.h b/SGQRCode/SGScanCode.h new file mode 100644 index 0000000..55d4e53 --- /dev/null +++ b/SGQRCode/SGScanCode.h @@ -0,0 +1,56 @@ +// +// SGScanCode.h +// SGQRCodeExample +// +// Created by kingsic on 2016/8/16. +// Copyright © 2016年 kingsic. All rights reserved. +// + +#import +@class SGScanCode; + +typedef void(^SGScanCodeScanResultBlock)(SGScanCode *scanCode, NSString *result); +typedef void(^SGScanCodeScanBrightnessBlock)(SGScanCode *scanCode, CGFloat brightness); +typedef void(^SGScanCodeReadResultBlock)(SGScanCode *scanCode, NSString *result); +typedef void(^SGScanCodeAlbumDidCancelBlock)(SGScanCode *scanCode); + +@interface SGScanCode : NSObject +/** 扫描区域,默认为整个视图,取值范围:0~1(以屏幕右上角为坐标原点)*/ +@property (nonatomic, assign) CGRect scanArea; +/** 捕获外界光线亮度,默认为:NO */ +@property (nonatomic, assign) BOOL brightness; +/** 判断相册访问权限是否授权 */ +@property (nonatomic, assign) BOOL albumAuthorization; +/** 打印信息,默认为:NO */ +@property (nonatomic, assign) BOOL openLog; + +/** 类方法创建 */ ++ (instancetype)scanCode; + +/** 后置摄像头是否可用 */ +- (BOOL)isCameraDeviceRearAvailable; + +/** 扫码回调方法 */ +- (void)scanWithController:(UIViewController *)controller resultBlock:(SGScanCodeScanResultBlock)blcok; +/** 扫码时,捕获外界光线强弱回调方法(brightness = YES 时,此回调方法才有效)*/ +- (void)scanWithBrightnessBlock:(SGScanCodeScanBrightnessBlock)blcok; + +/** 从相册中读码回调方法 */ +- (void)readWithResultBlock:(SGScanCodeReadResultBlock)block; +/** 相册选择控制器取消按钮的点击回调方法 */ +- (void)albumDidCancelBlock:(SGScanCodeAlbumDidCancelBlock)block; + +/** 开启扫描回调 */ +- (void)startRunningWithBefore:(void (^)(void))before completion:(void (^)(void))completion; +/** 停止扫描 */ +- (void)stopRunning; + +/** 播放音效文件 */ +- (void)playSoundName:(NSString *)name; + +/** 打开手电筒 */ +- (void)turnOnFlashlight; +/** 关闭手电筒 */ +- (void)turnOffFlashlight; + +@end diff --git a/SGQRCode/SGQRCodeManager.m b/SGQRCode/SGScanCode.m similarity index 55% rename from SGQRCode/SGQRCodeManager.m rename to SGQRCode/SGScanCode.m index a11c87c..82ecc77 100644 --- a/SGQRCode/SGQRCodeManager.m +++ b/SGQRCode/SGScanCode.m @@ -1,16 +1,16 @@ // -// SGQRCodeObtain.m +// SGScanCode.m // SGQRCodeExample // // Created by kingsic on 2016/8/16. // Copyright © 2016年 kingsic. All rights reserved. // -#import "SGQRCodeManager.h" +#import "SGScanCode.h" #import -#import +#import "SGAuthorization.h" -@interface SGQRCodeManager () +@interface SGScanCode () @property (nonatomic, weak) UIViewController *tempController; @property (nonatomic, strong) AVCaptureDevice *device; @property (nonatomic, strong) AVCaptureDeviceInput *deviceInput; @@ -19,22 +19,22 @@ @interface SGQRCodeManager () 0.5) { - ratio = 0.25; - } - CGFloat logoImageW = ratio * size; - CGFloat logoImageH = logoImageW; - CGFloat logoImageX = 0.5 * (image.size.width - logoImageW); - CGFloat logoImageY = 0.5 * (image.size.height - logoImageH); - CGRect logoImageRect = CGRectMake(logoImageX, logoImageY, logoImageW, logoImageH); - // 绘制logo - UIGraphicsBeginImageContextWithOptions(image.size, false, [UIScreen mainScreen].scale); - [image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)]; - if (logoImageCornerRadius < 0.0 || logoImageCornerRadius > 10) { - logoImageCornerRadius = 5; - } - UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:logoImageRect cornerRadius:logoImageCornerRadius]; - if (logoImageBorderWidth < 0.0 || logoImageBorderWidth > 10) { - logoImageBorderWidth = 5; - } - path.lineWidth = logoImageBorderWidth; - [logoImageBorderColor setStroke]; - [path stroke]; - [path addClip]; - [logoImage drawInRect:logoImageRect]; - UIImage *QRCodeImage = UIGraphicsGetImageFromCurrentImageContext(); - UIGraphicsEndImageContext(); - return QRCodeImage; -} - - (void)playSoundName:(NSString *)name { /// 静态库 path 的获取 NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil]; @@ -441,23 +304,29 @@ - (void)_enterImagePickerController { [_tempController presentViewController:imagePicker animated:YES completion:nil]; } - (NSString *)_canSetSessionPreset { - if ([self.session canSetSessionPreset:AVCaptureSessionPreset3840x2160]) { + if ([self.device supportsAVCaptureSessionPreset:AVCaptureSessionPreset3840x2160]) { return AVCaptureSessionPreset3840x2160; - } else if ([self.session canSetSessionPreset:AVCaptureSessionPreset1920x1080]) { + } + if ([self.device supportsAVCaptureSessionPreset:AVCaptureSessionPreset1920x1080]) { return AVCaptureSessionPreset1920x1080; - } else if ([self.session canSetSessionPreset:AVCaptureSessionPreset1280x720]) { + } + if ([self.device supportsAVCaptureSessionPreset:AVCaptureSessionPreset1280x720]) { return AVCaptureSessionPreset1280x720; - } else if ([self.session canSetSessionPreset:AVCaptureSessionPreset640x480]) { + } + if ([self.device supportsAVCaptureSessionPreset:AVCaptureSessionPreset640x480]) { return AVCaptureSessionPreset640x480; - } else if ([self.session canSetSessionPreset:AVCaptureSessionPreset352x288]) { + } + if ([self.device supportsAVCaptureSessionPreset:AVCaptureSessionPreset352x288]) { return AVCaptureSessionPreset352x288; - } else if ([self.session canSetSessionPreset:AVCaptureSessionPresetHigh]) { + } + if ([self.device supportsAVCaptureSessionPreset:AVCaptureSessionPresetHigh]) { return AVCaptureSessionPresetHigh; - } else if ([self.session canSetSessionPreset:AVCaptureSessionPresetMedium]) { + } + if ([self.device supportsAVCaptureSessionPreset:AVCaptureSessionPresetMedium]) { return AVCaptureSessionPresetMedium; - } else { - return AVCaptureSessionPresetLow; } + + return AVCaptureSessionPresetLow; } @end diff --git a/SGQRCode/SGQRCodeScanView.h b/SGQRCode/SGScanView.h similarity index 54% rename from SGQRCode/SGQRCodeScanView.h rename to SGQRCode/SGScanView.h index afb459f..9a724cc 100644 --- a/SGQRCode/SGQRCodeScanView.h +++ b/SGQRCode/SGScanView.h @@ -1,5 +1,5 @@ // -// SGQRCodeScanView.h +// SGScanView.h // SGQRCodeExample // // Created by kingsic on 2017/8/23. @@ -19,32 +19,32 @@ typedef enum : NSUInteger { typedef enum : NSUInteger { /// 单线扫描样式 - ScanAnimationStyleDefault, + ScanStyleDefault, /// 网格扫描样式 - ScanAnimationStyleGrid -} ScanAnimationStyle; + ScanStyleGrid +} ScanStyle; -@interface SGQRCodeScanView : UIView -/** 扫描样式,默认 ScanAnimationStyleDefault */ -@property (nonatomic, assign) ScanAnimationStyle scanAnimationStyle; +@interface SGScanView : UIView +/** 扫描样式,默认为:ScanStyleDefault */ +@property (nonatomic, assign) ScanStyle scanStyle; /** 扫描线名 */ -@property (nonatomic, copy) NSString *scanImageName; -/** 边框颜色,默认白色 */ +@property (nonatomic, copy) NSString *scanLineName; +/** 边框颜色,默认为:白色 */ @property (nonatomic, strong) UIColor *borderColor; -/** 边角位置,默认 CornerLoactionDefault */ +/** 边角位置,默认为:CornerLoactionDefault */ @property (nonatomic, assign) CornerLoaction cornerLocation; -/** 边角颜色,默认微信颜色 */ +/** 边角颜色,默认为:red:85/255.0 green:183/255.0 blue:55/255.0 alpha:1.0 */ @property (nonatomic, strong) UIColor *cornerColor; -/** 边角宽度,默认 2.f */ +/** 边角宽度,默认为:2.f */ @property (nonatomic, assign) CGFloat cornerWidth; -/** 扫描区周边颜色的 alpha 值,默认 0.2f */ +/** 扫描区周边颜色的 alpha 值,默认为:0.2f */ @property (nonatomic, assign) CGFloat backgroundAlpha; -/** 扫描线动画时间,默认 0.02s */ +/** 扫描线动画时间,默认为:0.02s */ @property (nonatomic, assign) NSTimeInterval animationTimeInterval; -/** 添加定时器 */ -- (void)addTimer; -/** 移除定时器 */ -- (void)removeTimer; +/** 扫描线开始扫描 */ +- (void)startScanning; +/** 扫描线停止扫描 */ +- (void)stopScanning; @end diff --git a/SGQRCode/SGQRCodeScanView.m b/SGQRCode/SGScanView.m similarity index 94% rename from SGQRCode/SGQRCodeScanView.m rename to SGQRCode/SGScanView.m index 95b8b7d..feacaba 100644 --- a/SGQRCode/SGQRCodeScanView.m +++ b/SGQRCode/SGScanView.m @@ -1,12 +1,12 @@ // -// SGQRCodeScanView.m +// SGScanView.m // SGQRCodeExample // // Created by kingsic on 2017/8/23. // Copyright © 2017年 kingsic All rights reserved. // -#import "SGQRCodeScanView.h" +#import "SGScanView.h" /** 扫描内容的 W 值 */ #define scanBorderW 0.7 * self.frame.size.width @@ -15,19 +15,20 @@ /** 扫描内容的 Y 值 */ #define scanBorderY 0.5 * (self.frame.size.height - scanBorderW) -@interface SGQRCodeScanView () +@interface SGScanView () @property (nonatomic, strong) UIView *contentView; @property (nonatomic, strong) NSTimer *timer; @property (nonatomic, strong) UIImageView *scanningline; @end -@implementation SGQRCodeScanView +@implementation SGScanView - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { self.backgroundColor = [UIColor clearColor]; [self initialization]; + [self addSubview:self.contentView]; } return self; } @@ -35,17 +36,18 @@ - (instancetype)initWithFrame:(CGRect)frame { - (void)awakeFromNib { [super awakeFromNib]; [self initialization]; + [self addSubview:self.contentView]; } - (void)initialization { - _scanAnimationStyle = ScanAnimationStyleDefault; + _scanStyle = ScanStyleDefault; _borderColor = [UIColor whiteColor]; _cornerLocation = CornerLoactionDefault; _cornerColor = [UIColor colorWithRed:85/255.0f green:183/255.0 blue:55/255.0 alpha:1.0]; _cornerWidth = 2.0; _backgroundAlpha = 0.5; _animationTimeInterval = 0.02; - _scanImageName = @"QRCodeScanLine"; + _scanLineName = @"scanLine"; } - (UIView *)contentView { @@ -177,20 +179,18 @@ - (void)drawRect:(CGRect)rect { } #pragma mark - - - 添加定时器 -- (void)addTimer { +- (void)startScanning { CGFloat scanninglineX = 0; CGFloat scanninglineY = 0; CGFloat scanninglineW = 0; CGFloat scanninglineH = 0; - if (self.scanAnimationStyle == ScanAnimationStyleGrid) { - [self addSubview:self.contentView]; + if (self.scanStyle == ScanStyleGrid) { [_contentView addSubview:self.scanningline]; scanninglineW = scanBorderW; scanninglineH = scanBorderW; scanninglineX = 0; scanninglineY = - scanBorderW; _scanningline.frame = CGRectMake(scanninglineX, scanninglineY, scanninglineW, scanninglineH); - } else { [self addSubview:self.scanningline]; scanninglineW = scanBorderW; @@ -199,15 +199,20 @@ - (void)addTimer { scanninglineY = scanBorderY; _scanningline.frame = CGRectMake(scanninglineX, scanninglineY, scanninglineW, scanninglineH); } + + [self removeTimer]; self.timer = [NSTimer timerWithTimeInterval:self.animationTimeInterval target:self selector:@selector(beginRefreshUI) userInfo:nil repeats:YES]; [[NSRunLoop mainRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes]; } +- (void)stopScanning { + [self removeTimer]; + [_scanningline removeFromSuperview]; + _scanningline = nil; +} #pragma mark - - - 移除定时器 - (void)removeTimer { [self.timer invalidate]; self.timer = nil; - [_scanningline removeFromSuperview]; - _scanningline = nil; } #pragma mark - - - 执行定时器方法 - (void)beginRefreshUI { @@ -216,7 +221,7 @@ - (void)beginRefreshUI { __weak typeof(self) weakSelf = self; - if (self.scanAnimationStyle == ScanAnimationStyleGrid) { + if (self.scanStyle == ScanStyleGrid) { if (flag) { frame.origin.y = - scanBorderW; flag = NO; @@ -283,9 +288,9 @@ - (UIImageView *)scanningline { } NSBundle *bundle = [NSBundle bundleWithURL:url]; - UIImage *image = [UIImage imageNamed:self.scanImageName inBundle:bundle compatibleWithTraitCollection:nil]; + UIImage *image = [UIImage imageNamed:self.scanLineName inBundle:bundle compatibleWithTraitCollection:nil]; if (!image) { - image = [UIImage imageNamed:self.scanImageName]; + image = [UIImage imageNamed:self.scanLineName]; } _scanningline.image = image; } @@ -293,12 +298,12 @@ - (UIImageView *)scanningline { } #pragma mark - - - set -- (void)setScanAnimationStyle:(ScanAnimationStyle)scanAnimationStyle { - _scanAnimationStyle = scanAnimationStyle; +- (void)setScanStyle:(ScanStyle)scanStyle { + _scanStyle = scanStyle; } -- (void)setScanImageName:(NSString *)scanImageName { - _scanImageName = scanImageName; +- (void)setScanLineName:(NSString *)scanLineName { + _scanLineName = scanLineName; } - (void)setBorderColor:(UIColor *)borderColor { diff --git a/SGQRCodeExample.xcodeproj/project.pbxproj b/SGQRCodeExample.xcodeproj/project.pbxproj index b710428..06e63f7 100755 --- a/SGQRCodeExample.xcodeproj/project.pbxproj +++ b/SGQRCodeExample.xcodeproj/project.pbxproj @@ -19,12 +19,14 @@ 18479F0B1E80F97700F7F225 /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 18479F071E80F97700F7F225 /* ViewController.m */; }; 18479F1A1E80F98F00F7F225 /* SGQRCode.bundle in Resources */ = {isa = PBXBuildFile; fileRef = 18479F101E80F98F00F7F225 /* SGQRCode.bundle */; }; 18479F1F1E80F9CA00F7F225 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 18479F1E1E80F9CA00F7F225 /* Main.storyboard */; }; - 8D19272A210DFEE7002AE63A /* SGQRCodeScanView.m in Sources */ = {isa = PBXBuildFile; fileRef = 8D192725210DFEE7002AE63A /* SGQRCodeScanView.m */; }; 8D19272E210DFF88002AE63A /* WBQRCodeVC.m in Sources */ = {isa = PBXBuildFile; fileRef = 8D19272D210DFF88002AE63A /* WBQRCodeVC.m */; }; 8D1E052F212442610093EA25 /* MBProgressHUD+SGQRCode.m in Sources */ = {isa = PBXBuildFile; fileRef = 8D1E052C212442610093EA25 /* MBProgressHUD+SGQRCode.m */; }; 8D1E0530212442610093EA25 /* MBProgressHUD.bundle in Resources */ = {isa = PBXBuildFile; fileRef = 8D1E052E212442610093EA25 /* MBProgressHUD.bundle */; }; 8D1E0536212443E00093EA25 /* MBProgressHUD.m in Sources */ = {isa = PBXBuildFile; fileRef = 8D1E0535212443E00093EA25 /* MBProgressHUD.m */; }; - 8D3DFEFE2662984800E02474 /* SGQRCodeManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 8D3DFEFD2662984800E02474 /* SGQRCodeManager.m */; }; + 8D3DFEFE2662984800E02474 /* SGScanCode.m in Sources */ = {isa = PBXBuildFile; fileRef = 8D3DFEFD2662984800E02474 /* SGScanCode.m */; }; + 8D97EF66269325F700F30558 /* SGAuthorization.m in Sources */ = {isa = PBXBuildFile; fileRef = 8D97EF63269325F700F30558 /* SGAuthorization.m */; }; + 8D97EF67269325F700F30558 /* SGScanView.m in Sources */ = {isa = PBXBuildFile; fileRef = 8D97EF65269325F700F30558 /* SGScanView.m */; }; + 8D97EF6A2693272A00F30558 /* SGCreateCode.m in Sources */ = {isa = PBXBuildFile; fileRef = 8D97EF692693272A00F30558 /* SGCreateCode.m */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -69,8 +71,6 @@ 18479F101E80F98F00F7F225 /* SGQRCode.bundle */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.plug-in"; path = SGQRCode.bundle; sourceTree = ""; }; 18479F111E80F98F00F7F225 /* SGQRCode.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SGQRCode.h; sourceTree = ""; }; 18479F1E1E80F9CA00F7F225 /* Main.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; path = Main.storyboard; sourceTree = ""; }; - 8D192725210DFEE7002AE63A /* SGQRCodeScanView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SGQRCodeScanView.m; sourceTree = ""; }; - 8D192726210DFEE7002AE63A /* SGQRCodeScanView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SGQRCodeScanView.h; sourceTree = ""; }; 8D19272C210DFF87002AE63A /* WBQRCodeVC.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WBQRCodeVC.h; sourceTree = ""; }; 8D19272D210DFF88002AE63A /* WBQRCodeVC.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = WBQRCodeVC.m; sourceTree = ""; }; 8D1E052C212442610093EA25 /* MBProgressHUD+SGQRCode.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "MBProgressHUD+SGQRCode.m"; sourceTree = ""; }; @@ -78,8 +78,14 @@ 8D1E052E212442610093EA25 /* MBProgressHUD.bundle */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.plug-in"; path = MBProgressHUD.bundle; sourceTree = ""; }; 8D1E0534212443E00093EA25 /* MBProgressHUD.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MBProgressHUD.h; sourceTree = ""; }; 8D1E0535212443E00093EA25 /* MBProgressHUD.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MBProgressHUD.m; sourceTree = ""; }; - 8D3DFEFC2662984800E02474 /* SGQRCodeManager.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SGQRCodeManager.h; sourceTree = ""; }; - 8D3DFEFD2662984800E02474 /* SGQRCodeManager.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SGQRCodeManager.m; sourceTree = ""; }; + 8D3DFEFC2662984800E02474 /* SGScanCode.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SGScanCode.h; sourceTree = ""; }; + 8D3DFEFD2662984800E02474 /* SGScanCode.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SGScanCode.m; sourceTree = ""; }; + 8D97EF62269325F700F30558 /* SGScanView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SGScanView.h; sourceTree = ""; }; + 8D97EF63269325F700F30558 /* SGAuthorization.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SGAuthorization.m; sourceTree = ""; }; + 8D97EF64269325F700F30558 /* SGAuthorization.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SGAuthorization.h; sourceTree = ""; }; + 8D97EF65269325F700F30558 /* SGScanView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SGScanView.m; sourceTree = ""; }; + 8D97EF682693272A00F30558 /* SGCreateCode.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SGCreateCode.h; sourceTree = ""; }; + 8D97EF692693272A00F30558 /* SGCreateCode.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SGCreateCode.m; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -199,10 +205,14 @@ isa = PBXGroup; children = ( 18479F111E80F98F00F7F225 /* SGQRCode.h */, - 8D3DFEFC2662984800E02474 /* SGQRCodeManager.h */, - 8D3DFEFD2662984800E02474 /* SGQRCodeManager.m */, - 8D192726210DFEE7002AE63A /* SGQRCodeScanView.h */, - 8D192725210DFEE7002AE63A /* SGQRCodeScanView.m */, + 8D3DFEFC2662984800E02474 /* SGScanCode.h */, + 8D3DFEFD2662984800E02474 /* SGScanCode.m */, + 8D97EF62269325F700F30558 /* SGScanView.h */, + 8D97EF65269325F700F30558 /* SGScanView.m */, + 8D97EF682693272A00F30558 /* SGCreateCode.h */, + 8D97EF692693272A00F30558 /* SGCreateCode.m */, + 8D97EF64269325F700F30558 /* SGAuthorization.h */, + 8D97EF63269325F700F30558 /* SGAuthorization.m */, 18479F101E80F98F00F7F225 /* SGQRCode.bundle */, ); path = SGQRCode; @@ -365,13 +375,15 @@ files = ( 8D19272E210DFF88002AE63A /* WBQRCodeVC.m in Sources */, 18479F0B1E80F97700F7F225 /* ViewController.m in Sources */, - 8D19272A210DFEE7002AE63A /* SGQRCodeScanView.m in Sources */, 180E360F1F01F33E00A53E7A /* ScanSuccessJumpVC.m in Sources */, 180E36101F01F33E00A53E7A /* WCQRCodeVC.m in Sources */, 8D1E052F212442610093EA25 /* MBProgressHUD+SGQRCode.m in Sources */, + 8D97EF66269325F700F30558 /* SGAuthorization.m in Sources */, + 8D97EF6A2693272A00F30558 /* SGCreateCode.m in Sources */, 8D1E0536212443E00093EA25 /* MBProgressHUD.m in Sources */, 18479ED21E80F92500F7F225 /* AppDelegate.m in Sources */, - 8D3DFEFE2662984800E02474 /* SGQRCodeManager.m in Sources */, + 8D3DFEFE2662984800E02474 /* SGScanCode.m in Sources */, + 8D97EF67269325F700F30558 /* SGScanView.m in Sources */, 180E36111F01F33E00A53E7A /* SGWebView.m in Sources */, 18479ECF1E80F92500F7F225 /* main.m in Sources */, ); diff --git a/SGQRCodeExample/Controller/WBQRCodeVC.m b/SGQRCodeExample/Controller/WBQRCodeVC.m index 72a2387..df65bcb 100644 --- a/SGQRCodeExample/Controller/WBQRCodeVC.m +++ b/SGQRCodeExample/Controller/WBQRCodeVC.m @@ -12,9 +12,9 @@ #import "MBProgressHUD+SGQRCode.h" @interface WBQRCodeVC () { - SGQRCodeManager *manager; + SGScanCode *scanCode; } -@property (nonatomic, strong) SGQRCodeScanView *scanView; +@property (nonatomic, strong) SGScanView *scanView; @property (nonatomic, strong) UILabel *promptLabel; @property (nonatomic, assign) BOOL stop; @end @@ -25,20 +25,10 @@ - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; if (_stop) { - [manager startRunningWithBefore:nil completion:nil]; + [scanCode startRunningWithBefore:nil completion:nil]; } } -- (void)viewDidAppear:(BOOL)animated { - [super viewDidAppear:animated]; - [self.scanView addTimer]; -} - -- (void)viewWillDisappear:(BOOL)animated { - [super viewWillDisappear:animated]; - [self.scanView removeTimer]; -} - - (void)dealloc { NSLog(@"WBQRCodeVC - dealloc"); [self removeScanningView]; @@ -49,30 +39,36 @@ - (void)viewDidLoad { // Do any additional setup after loading the view from its nib. self.view.backgroundColor = [UIColor blackColor]; - manager = [SGQRCodeManager QRCodeManager]; + scanCode = [SGScanCode scanCode]; [self setupQRCodeScan]; [self setupNavigationBar]; [self.view addSubview:self.scanView]; + [self.scanView startScanning]; [self.view addSubview:self.promptLabel]; } - (void)setupQRCodeScan { __weak typeof(self) weakSelf = self; - [manager scanWithController:self resultBlock:^(SGQRCodeManager *manager, NSString *result) { + [scanCode scanWithController:self resultBlock:^(SGScanCode *scanCode, NSString *result) { if (result) { - [manager stopRunning]; + [scanCode stopRunning]; weakSelf.stop = YES; - [manager playSoundName:@"SGQRCode.bundle/QRCodeScanEndSound.caf"]; - + [scanCode playSoundName:@"SGQRCode.bundle/scanEndSound.caf"]; ScanSuccessJumpVC *jumpVC = [[ScanSuccessJumpVC alloc] init]; - jumpVC.comeFromVC = ScanSuccessJumpComeFromWB; - jumpVC.jump_URL = result; [weakSelf.navigationController pushViewController:jumpVC animated:YES]; + + if ([result hasPrefix:@"http"]) { + jumpVC.comeFromVC = ScanSuccessJumpComeFromWB; + jumpVC.jump_URL = result; + } else { + jumpVC.comeFromVC = ScanSuccessJumpComeFromWB; + jumpVC.jump_bar_code = result; + } } }]; - [manager startRunningWithBefore:^{ + [scanCode startRunningWithBefore:^{ [MBProgressHUD SG_showMBProgressHUDWithModifyStyleMessage:@"正在加载..." toView:weakSelf.view]; } completion:^{ [MBProgressHUD SG_hideHUDForView:weakSelf.view]; @@ -86,7 +82,8 @@ - (void)setupNavigationBar { - (void)rightBarButtonItenAction { __weak typeof(self) weakSelf = self; - [manager readWithResultBlock:^(SGQRCodeManager *manager, NSString *result) { + + [scanCode readWithResultBlock:^(SGScanCode *scanCode, NSString *result) { if (result == nil) { NSLog(@"暂未识别出二维码"); } else { @@ -106,18 +103,18 @@ - (void)rightBarButtonItenAction { }]; } -- (SGQRCodeScanView *)scanView { +- (SGScanView *)scanView { if (!_scanView) { - _scanView = [[SGQRCodeScanView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; - _scanView.scanImageName = @"SGQRCode.bundle/QRCodeScanLineGrid"; - _scanView.scanAnimationStyle = ScanAnimationStyleGrid; + _scanView = [[SGScanView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; + _scanView.scanLineName = @"SGQRCode.bundle/scanLineGrid"; + _scanView.scanStyle = ScanStyleGrid; _scanView.cornerLocation = CornerLoactionOutside; _scanView.cornerColor = [UIColor orangeColor]; } return _scanView; } - (void)removeScanningView { - [self.scanView removeTimer]; + [self.scanView stopScanning]; [self.scanView removeFromSuperview]; self.scanView = nil; } diff --git a/SGQRCodeExample/Controller/WCQRCodeVC.m b/SGQRCodeExample/Controller/WCQRCodeVC.m index 6729d33..d0c40d7 100755 --- a/SGQRCodeExample/Controller/WCQRCodeVC.m +++ b/SGQRCodeExample/Controller/WCQRCodeVC.m @@ -12,9 +12,9 @@ #import "MBProgressHUD+SGQRCode.h" @interface WCQRCodeVC () { - SGQRCodeManager *manager; + SGScanCode *scanCode; } -@property (nonatomic, strong) SGQRCodeScanView *scanView; +@property (nonatomic, strong) SGScanView *scanView; @property (nonatomic, strong) UIButton *flashlightBtn; @property (nonatomic, strong) UILabel *promptLabel; @property (nonatomic, assign) BOOL isSelectedFlashlightBtn; @@ -26,19 +26,19 @@ @implementation WCQRCodeVC - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; /// 二维码开启方法 - [manager startRunningWithBefore:nil completion:nil]; + [scanCode startRunningWithBefore:nil completion:nil]; } - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; - [self.scanView addTimer]; + [self.scanView startScanning]; } - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; - [self.scanView removeTimer]; + [self.scanView stopScanning]; [self removeFlashlightBtn]; - [manager stopRunning]; + [scanCode stopRunning]; } - (void)dealloc { @@ -51,7 +51,7 @@ - (void)viewDidLoad { // Do any additional setup after loading the view from its nib. self.view.backgroundColor = [UIColor blackColor]; - manager = [SGQRCodeManager QRCodeManager]; + scanCode = [SGScanCode scanCode]; [self setupQRCodeScan]; [self setupNavigationBar]; @@ -62,34 +62,33 @@ - (void)viewDidLoad { } - (void)setupQRCodeScan { - BOOL isCameraDeviceRearAvailable = manager.isCameraDeviceRearAvailable; + BOOL isCameraDeviceRearAvailable = scanCode.isCameraDeviceRearAvailable; if (isCameraDeviceRearAvailable == NO) { return; } __weak typeof(self) weakSelf = self; - manager.openLog = YES; - manager.brightness = YES; + scanCode.openLog = YES; + scanCode.brightness = YES; - [manager scanWithController:self resultBlock:^(SGQRCodeManager *manager, NSString *result) { + [scanCode scanWithController:self resultBlock:^(SGScanCode *scanCode, NSString *result) { if (result) { - [manager stopRunning]; - [manager playSoundName:@"SGQRCode.bundle/QRCodeScanEndSound.caf"]; + [scanCode stopRunning]; + [scanCode playSoundName:@"SGQRCode.bundle/scanEndSound.caf"]; + ScanSuccessJumpVC *jumpVC = [[ScanSuccessJumpVC alloc] init]; + [weakSelf.navigationController pushViewController:jumpVC animated:YES]; + if ([result hasPrefix:@"http"]) { - ScanSuccessJumpVC *jumpVC = [[ScanSuccessJumpVC alloc] init]; - jumpVC.comeFromVC = ScanSuccessJumpComeFromWB; + jumpVC.comeFromVC = ScanSuccessJumpComeFromWC; jumpVC.jump_URL = result; - [weakSelf.navigationController pushViewController:jumpVC animated:YES]; - } else { - ScanSuccessJumpVC *jumpVC = [[ScanSuccessJumpVC alloc] init]; - jumpVC.comeFromVC = ScanSuccessJumpComeFromWB; - jumpVC.jump_bar_code = result; - [weakSelf.navigationController pushViewController:jumpVC animated:YES]; + jumpVC.comeFromVC = ScanSuccessJumpComeFromWC; + jumpVC.jump_URL = result; } } }]; - [manager scanWithBrightnessBlock:^(SGQRCodeManager *manager, CGFloat brightness) { + + [scanCode scanWithBrightnessBlock:^(SGScanCode *scanCode, CGFloat brightness) { if (brightness < - 1) { [weakSelf.view addSubview:weakSelf.flashlightBtn]; } else { @@ -100,14 +99,14 @@ - (void)setupQRCodeScan { }]; } -- (SGQRCodeScanView *)scanView { +- (SGScanView *)scanView { if (!_scanView) { - _scanView = [[SGQRCodeScanView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 0.9 * self.view.frame.size.height)]; + _scanView = [[SGScanView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 0.9 * self.view.frame.size.height)]; } return _scanView; } - (void)removeScanningView { - [self.scanView removeTimer]; + [self.scanView stopScanning]; [self.scanView removeFromSuperview]; self.scanView = nil; } @@ -156,7 +155,7 @@ - (UIButton *)flashlightBtn { - (void)flashlightBtn_action:(UIButton *)button { if (button.selected == NO) { - [manager turnOnFlashlight]; + [scanCode turnOnFlashlight]; self.isSelectedFlashlightBtn = YES; button.selected = YES; } else { @@ -166,7 +165,7 @@ - (void)flashlightBtn_action:(UIButton *)button { - (void)removeFlashlightBtn { dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ - [self->manager turnOffFlashlight]; + [self->scanCode turnOffFlashlight]; self.isSelectedFlashlightBtn = NO; self.flashlightBtn.selected = NO; [self.flashlightBtn removeFromSuperview]; @@ -180,8 +179,7 @@ - (void)setupNavigationBar { - (void)rightBarButtonItenAction { __weak typeof(self) weakSelf = self; - - [manager readWithResultBlock:^(SGQRCodeManager *manager, NSString *result) { + [scanCode readWithResultBlock:^(SGScanCode *scanCode, NSString *result) { [MBProgressHUD SG_showMBProgressHUDWithModifyStyleMessage:@"正在处理..." toView:weakSelf.view]; if (result == nil) { NSLog(@"暂未识别出二维码"); @@ -204,11 +202,11 @@ - (void)rightBarButtonItenAction { } }]; - if (manager.albumAuthorization == YES) { - [self.scanView removeTimer]; + if (scanCode.albumAuthorization == YES) { + [self.scanView stopScanning]; } - [manager albumDidCancelBlock:^(SGQRCodeManager *manager) { - [weakSelf.scanView addTimer]; + [scanCode albumDidCancelBlock:^(SGScanCode *scanCode) { + [weakSelf.scanView startScanning]; }]; } diff --git a/SGQRCodeExample/ViewController.m b/SGQRCodeExample/ViewController.m index fbd3e02..74c4363 100755 --- a/SGQRCodeExample/ViewController.m +++ b/SGQRCodeExample/ViewController.m @@ -45,19 +45,21 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.row == 0) { - SGQRCodeManager *manager = [SGQRCodeManager QRCodeManager]; - [manager authorizationStatusBlock:^(SGQRCodeManager *manager, SGAuthorizationStatus authorizationStatus) { - if (authorizationStatus == SGAuthorizationStatusSuccess) { + SGAuthorization *authorization = [[SGAuthorization alloc] init]; + authorization.openLog = YES; + [authorization AVAuthorizationBlock:^(SGAuthorization * _Nonnull authorization, SGAuthorizationStatus status) { + if (status == SGAuthorizationStatusSuccess) { WBQRCodeVC *WBVC = [[WBQRCodeVC alloc] init]; [self.navigationController pushViewController:WBVC animated:YES]; - } else if (authorizationStatus == SGAuthorizationStatusFail) { + } else if (status == SGAuthorizationStatusFail) { UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"请去-> [设置 - 隐私 - 相机 - SGQRCodeExample] 打开访问开关" preferredStyle:(UIAlertControllerStyleAlert)]; UIAlertAction *alertA = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) { - }]; [alertC addAction:alertA]; - [self presentViewController:alertC animated:YES completion:nil]; + dispatch_async(dispatch_get_main_queue(), ^{ + [self presentViewController:alertC animated:YES completion:nil]; + }); } else { UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"未检测到您的摄像头" preferredStyle:(UIAlertControllerStyleAlert)]; UIAlertAction *alertA = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) { @@ -65,26 +67,29 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath }]; [alertC addAction:alertA]; - [self presentViewController:alertC animated:YES completion:nil]; + dispatch_async(dispatch_get_main_queue(), ^{ + [self presentViewController:alertC animated:YES completion:nil]; + }); } }]; } if (indexPath.row == 1) { - SGQRCodeManager *manager = [SGQRCodeManager QRCodeManager]; - manager.openLog = YES; - [manager authorizationStatusBlock:^(SGQRCodeManager *manager, SGAuthorizationStatus authorizationStatus) { - if (authorizationStatus == SGAuthorizationStatusSuccess) { + SGAuthorization *authorization = [SGAuthorization authorization]; + [authorization AVAuthorizationBlock:^(SGAuthorization * _Nonnull authorization, SGAuthorizationStatus status) { + if (status == SGAuthorizationStatusSuccess) { WCQRCodeVC *WCVC = [[WCQRCodeVC alloc] init]; [self.navigationController pushViewController:WCVC animated:YES]; - } else if (authorizationStatus == SGAuthorizationStatusFail) { + } else if (status == SGAuthorizationStatusFail) { UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"请去-> [设置 - 隐私 - 相机 - SGQRCodeExample] 打开访问开关" preferredStyle:(UIAlertControllerStyleAlert)]; UIAlertAction *alertA = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) { }]; [alertC addAction:alertA]; - [self presentViewController:alertC animated:YES completion:nil]; + dispatch_async(dispatch_get_main_queue(), ^{ + [self presentViewController:alertC animated:YES completion:nil]; + }); } else { UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"未检测到您的摄像头" preferredStyle:(UIAlertControllerStyleAlert)]; UIAlertAction *alertA = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) { @@ -92,7 +97,9 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath }]; [alertC addAction:alertA]; - [self presentViewController:alertC animated:YES completion:nil]; + dispatch_async(dispatch_get_main_queue(), ^{ + [self presentViewController:alertC animated:YES completion:nil]; + }); } }]; }