![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLiAjM2EzLcd3LcJzLcJzdllmVldWYtl2Pml2ZuEGawk3Yqd2Mml2LcJTO0ETMyIzLcVmdhNXLwRHdo9CXt92YucWbpRWdvx2Yx5yazF2Lc9CX6MHc0RHaiojIsJye.gif)
效果預覽1.gif
效果預覽2.gif
功能描述:WSLNativeScanTool是在利用原生API的條件下封裝的二維碼掃描工具,支援二維碼的掃描、識别圖中二維碼、生成自定義顔色和中心圖示的二維碼、監測環境亮度、打開閃光燈這些功能;WSLScanView是參照微信封裝的一個掃一掃界面,支援線條顔色、大小、動畫圖檔、矩形掃描框樣式的自定義;這個示例本身就是仿照微信的掃一掃功能實作的。
- 詳細實作就不在此唠叨了,直接去看代碼吧,注釋詳細是我的習慣😀→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年 https://www.jianshu.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:@"https://www.jianshu.com/u/e15d1f644bea" andSize:CGSizeMake(250, 250) andBackColor:[UIColor whiteColor] andFrontColor:[UIColor orangeColor] andCenterImage:[UIImage imageNamed:@"piao"]];
createQRCodeController.qrString = @"https://www.jianshu.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