效果預覽1.gif
效果預覽2.gif
功能描述:WSLNativeScanTool是在利用原生API的條件下封裝的二維碼掃描工具,支持二維碼的掃描、識別圖中二維碼、生成自定義顏色和中心圖標的二維碼、監測環境亮度、打開閃光燈這些功能;WSLScanView是參照微信封裝的一個掃一掃界面,支持線條顏色、大小、動畫圖片、矩形掃描框樣式的自定義;這個示例本身就是仿照微信的掃一掃功能實現的。
支持 pod 'WSLNativeScanTool', '~> 1.0'
- 詳細實現就不在此嘮叨了,直接去看代碼吧,注釋詳細是我的習慣??→WSLNativeScanTool
- 來看一下WSLNativeScanTool.h ,用法很明朗
@import UIKit;
@import AVFoundation;
#import <Foundation/Foundation.h>
/**
掃描完成的回調
@param scanString 掃描出的字符串
*/
typedef void(^WSLScanFinishedBlock)( NSString * _Nullable scanString);
/**
監聽環境光感的回調
@param brightness 亮度值
*/
typedef void(^WSLMonitorLightBlock)( float brightness);
@interface WSLNativeScanTool : NSObject
/**
掃描出結果后的回調 ,注意循環引用的問題
*/
@property (nonatomic, copy) WSLScanFinishedBlock _Nullable scanFinishedBlock;
/**
監聽環境光感的回調,如果 != nil 表示開啟監測環境亮度功能
*/
@property (nonatomic, copy) WSLMonitorLightBlock _Nullable monitorLightBlock;
/**
閃光燈的狀態,不需要設置,僅供外邊判斷狀態使用
*/
@property (nonatomic, assign) BOOL flashOpen;
/**
初始化 掃描工具
@param preview 展示輸出流的視圖
@param scanFrame 掃描中心識別區域范圍
*/
- (instancetype )initWithPreview:(UIView *)preview andScanFrame:(CGRect)scanFrame;
/**
閃光燈開關
*/
- (void)openFlashSwitch:(BOOL)open;
- (void)sessionStartRunning;
- (void)sessionStopRunning;
/**
識別圖中二維碼
*/
- (void)scanImageQRCode:(UIImage *_Nullable)imageCode;
/**
生成自定義樣式二維碼
注意:有些顏色結合生成的二維碼識別不了
@param codeString 字符串
@param size 大小
@param backColor 背景色
@param frontColor 前景色
@param centerImage 中心圖片
@return image二維碼
*/
+ (UIImage *)createQRCodeImageWithString:(nonnull NSString *)codeString andSize:(CGSize)size andBackColor:(nullable UIColor *)backColor andFrontColor:(nullable UIColor *)frontColor andCenterImage:(nullable UIImage *)centerImage;
- 再來看一下WSLScanView.h,用法也明朗??
//
// WSLScanView.h
// ScanQRcode
//
// Created by 王雙龍 on 2018/2/28.
// Copyright ? 2018年 http://www.lxweimin.com/u/e15d1f644bea
All rights reserved.
//
#import <UIKit/UIKit.h>
typedef void(^WSLMyQRCodeBlock)(void);
typedef void(^WSLFlashSwitchBlock)(BOOL open);
@interface WSLScanView : UIView
/**
點擊我的二維碼的回調
*/
@property (nonatomic,copy) WSLMyQRCodeBlock myQRCodeBlock;
/**
打開/關閉閃光燈的回調
*/
@property (nonatomic,copy) WSLFlashSwitchBlock flashSwitchBlock;
#pragma mark - 掃碼區域
/**
掃碼區域 默認為正方形,x = 60, y = 100
*/
@property (nonatomic,assign)CGRect scanRetangleRect;
/**
@brief 是否需要繪制掃碼矩形框,默認YES
*/
@property (nonatomic, assign) BOOL isNeedShowRetangle;
/**
@brief 矩形框線條顏色
*/
@property (nonatomic, strong, nullable) UIColor *colorRetangleLine;
#pragma mark - 矩形框(掃碼區域)周圍4個角
//4個角的顏色
@property (nonatomic, strong, nullable) UIColor* colorAngle;
//掃碼區域4個角的寬度和高度 默認都為20
@property (nonatomic, assign) CGFloat photoframeAngleW;
@property (nonatomic, assign) CGFloat photoframeAngleH;
/**
@brief 掃碼區域4個角的線條寬度,默認6
*/
@property (nonatomic, assign) CGFloat photoframeLineW;
#pragma mark --動畫效果
/**
* 動畫效果的圖像
*/
@property (nonatomic,strong, nullable) UIImage * animationImage;
/**
非識別區域顏色,默認 RGBA (0,0,0,0.5)
*/
@property (nonatomic, strong, nullable) UIColor * notRecoginitonArea;
/**
* 開始掃描動畫
*/
- (void)startScanAnimation;
/**
* 結束掃描動畫
*/
- (void)stopScanAnimation;
/**
正在處理掃描到的結果
*/
- (void)handlingResultsOfScan;
/**
完成掃描結果處理
*/
- (void)finishedHandle;
/**
是否顯示閃光燈開關
@param show YES or NO
*/
- (void)showFlashSwitch:(BOOL)show;
@end
用法
- 實例化WSLNativeScanTool和WSLScanView
//輸出流視圖
UIView *preview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 0)];
[self.view addSubview:preview];
__weak typeof(self) weakSelf = self;
//構建掃描樣式視圖
_scanView = [[WSLScanView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 0)];
_scanView.scanRetangleRect = CGRectMake(60, 120, (self.view.frame.size.width - 2 * 60), (self.view.frame.size.width - 2 * 60));
_scanView.colorAngle = [UIColor greenColor];
_scanView.photoframeAngleW = 20;
_scanView.photoframeAngleH = 20;
_scanView.photoframeLineW = 2;
_scanView.isNeedShowRetangle = YES;
_scanView.colorRetangleLine = [UIColor whiteColor];
_scanView.notRecoginitonArea = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
_scanView.animationImage = [UIImage imageNamed:@"scanLine"];
_scanView.myQRCodeBlock = ^{
[WSLNativeScanTool createQRCodeImageWithString:@"http://www.lxweimin.com/u/e15d1f644bea" andSize:CGSizeMake(250, 250) andBackColor:[UIColor whiteColor] andFrontColor:[UIColor orangeColor] andCenterImage:[UIImage imageNamed:@"piao"]];
createQRCodeController.qrString = @"http://www.lxweimin.com/u/e15d1f644bea";
};
_scanView.flashSwitchBlock = ^(BOOL open) {
[weakSelf.scanTool openFlashSwitch:open];
};
[self.view addSubview:_scanView];
//初始化掃描工具
_scanTool = [[WSLNativeScanTool alloc] initWithPreview:preview andScanFrame:_scanView.scanRetangleRect];
_scanTool.scanFinishedBlock = ^(NSString *scanString) {
NSLog(@"掃描結果 %@",scanString);
[weakSelf.scanTool sessionStopRunning];
[weakSelf.scanTool openFlashSwitch:NO];
};
_scanTool.monitorLightBlock = ^(float brightness) {
NSLog(@"環境光感 : %f",brightness);
if (brightness < 0) {
// 環境太暗,顯示閃光燈開關按鈕
[weakSelf.scanView showFlashSwitch:YES];
}else if(brightness > 0){
// 環境亮度可以,且閃光燈處于關閉狀態時,隱藏閃光燈開關
if(!weakSelf.scanTool.flashOpen){
[weakSelf.scanView showFlashSwitch:NO];
}
}
};
[_scanTool sessionStartRunning];
[_scanView startScanAnimation];
好了到此結束,告辭~ 需要的瞅這里→WSLNativeScanTool
贊.gif