寫(xiě)在前面
最近項(xiàng)目要實(shí)現(xiàn)相機(jī)掃描二維碼功能,具體要求:1、掃描框 2、掃描動(dòng)畫(huà) 3、相冊(cè)識(shí)別二維碼 4、聲音反饋。
記得之前用過(guò)三方庫(kù)做過(guò)類(lèi)似功能,但是也是知其然不知其所以然,然后今天自己用原生api簡(jiǎn)單封裝了一個(gè)二維碼掃描控件。
項(xiàng)目結(jié)構(gòu)介紹
控件封裝后主要結(jié)構(gòu)如圖:
如圖中代碼目錄,vender里面放的是UIView+Frame分類(lèi),Resource里面放的是圖片聲音資源,TZImagePickerController是第三方相冊(cè),用來(lái)獲取相冊(cè)中的二維碼識(shí)別的。主要的就是以QR開(kāi)頭的文件,我們具體說(shuō)一說(shuō)。
QRCode.h
這個(gè)文件主要放的是各個(gè)文件的頭文件,方便在各處調(diào)用
#import "QRCodeScanManager.h"#import #import "QRLightManager.h"#import "QRCodeScanView.h"#import "QRCodeHelper.h"
QRLightManager
這個(gè)類(lèi)是用來(lái)開(kāi)啟關(guān)閉閃光燈的
/** 打開(kāi)手電筒 */+ (void)openFlashLight;/** 關(guān)閉手電筒 */+ (void)closeFlashLight;
#pragma mark 打開(kāi)手電筒+ (void)openFlashLight { AVCaptureDevice *captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; NSError *error = nil; if ([captureDevice hasTorch]) { BOOL locked = [captureDevice lockForConfiguration:&error]; if (locked) { captureDevice.torchMode = AVCaptureTorchModeOn; [captureDevice unlockForConfiguration]; } }}#pragma mark 關(guān)閉手電筒+ (void)closeFlashLight { AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; if ([device hasTorch]) { [device lockForConfiguration:nil]; [device setTorchMode:AVCaptureTorchModeOff]; [device unlockForConfiguration]; }}
QRCodeScanView
這個(gè)類(lèi)是將這個(gè)界面單獨(dú)封裝出來(lái),便于自定義
在.h文件中有個(gè)枚舉用來(lái)標(biāo)識(shí)二維碼掃描四周角標(biāo)的位置:
typedef enum : NSUInteger { CornerLoactionDefault,//默認(rèn)與邊框同中心點(diǎn) CornerLoactionInside,//在邊框線內(nèi)部 CornerLoactionOutside,//在邊框線外部} CornerLoaction;
自定義view各個(gè)屬性:
@property (nonatomic, strong) UIColor *borderColor;/** 邊框顏色*/@property (nonatomic, assign) CornerLoaction cornerLocation;/** 邊角位置 默認(rèn)default*/@property (nonatomic, strong) UIColor *cornerColor;/** 邊角顏色 默認(rèn)正保藍(lán)#32d2dc*/@property (nonatomic, assign) CGFloat cornerWidth;/** 邊角寬度 默認(rèn)2.f*/@property (nonatomic, assign) CGFloat backgroundAlpha;/** 掃描區(qū)周邊顏色的alpha 默認(rèn)0.5*/@property (nonatomic, assign) CGFloat timeInterval;/** 掃描間隔 默認(rèn)0.02*/@property (nonatomic, strong) UIButton *lightBtn;/** 閃光燈*/
暴露外部調(diào)用的方法:
/** 添加定時(shí)器 */- (void)addTimer;/** 移除定時(shí)器 */- (void)removeTimer;/** 根據(jù)燈光判斷 */- (void)lightBtnChangeWithBrightnessValue:(CGFloat)brightnessValue;
初始化默認(rèn)值:
- (void)initilize { self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5]; self.borderColor = [UIColor whiteColor]; _cornerLocation = CornerLoactionDefault; _cornerColor = [UIColor colorWithRed:50/255.0f green:210/255.0f blue:220/255.0f alpha:1.0]; _cornerWidth = 2.0; self.timeInterval = 0.02; _backgroundAlpha = 0.5; [self addSubview:self.lightBtn];}
重寫(xiě)view的drawRect方法,在這個(gè)方法里面繪制scanView的邊框和邊角
//空白區(qū)域設(shè)置 [[[UIColor blackColor] colorWithAlphaComponent:self.backgroundAlpha] setFill]; UIRectFill(rect); //獲取上下文,并設(shè)置混合模式 -> kCGBlendModeDestinationOut CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetBlendMode(context, kCGBlendModeDestinationOut); //設(shè)置空白區(qū) UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRect:CGRectMake(borderX + 0.5 * borderLineW, borderY+ 0.5 * borderLineW, borderW - borderLineW, borderH - borderLineW)]; [bezierPath fill]; //執(zhí)行混合模式 CGContextSetBlendMode(context, kCGBlendModeNormal); //邊框設(shè)置 UIBezierPath *borderPath = [UIBezierPath bezierPathWithRect:CGRectMake(borderX, borderY, borderW, borderH)]; borderPath.lineCapStyle = kCGLineCapButt; borderPath.lineWidth = borderLineW; [self.borderColor set]; [borderPath stroke]; CGFloat cornerLength = 20; //左上角小圖標(biāo) UIBezierPath *leftTopPath = [UIBezierPath bezierPath]; leftTopPath.lineWidth = self.cornerWidth; [self.cornerColor set]; CGFloat insideExcess = fabs(0.5 * (self.cornerWidth - borderLineW)); CGFloat outsideExcess = 0.5 * (borderLineW + self.cornerWidth); if (self.cornerLocation == CornerLoactionInside) { [leftTopPath moveToPoint:CGPointMake(borderX + insideExcess, borderY + cornerLength + insideExcess)]; [leftTopPath addLineToPoint:CGPointMake(borderX + insideExcess, borderY + insideExcess)]; [leftTopPath addLineToPoint:CGPointMake(borderX + cornerLength + insideExcess, borderY + insideExcess)]; } else if (self.cornerLocation == CornerLoactionOutside) { [leftTopPath moveToPoint:CGPointMake(borderX - outsideExcess, borderY + cornerLength - outsideExcess)]; [leftTopPath addLineToPoint:CGPointMake(borderX - outsideExcess, borderY - outsideExcess)]; [leftTopPath addLineToPoint:CGPointMake(borderX + cornerLength - outsideExcess, borderY - outsideExcess)]; } else { [leftTopPath moveToPoint:CGPointMake(borderX, borderY + cornerLength)]; [leftTopPath addLineToPoint:CGPointMake(borderX, borderY)]; [leftTopPath addLineToPoint:CGPointMake(borderX + cornerLength, borderY)]; } [leftTopPath stroke];
增加定時(shí)器以及開(kāi)始動(dòng)畫(huà):
- (void)addTimer { [self addSubview:self.scanningLine]; self.timer = [NSTimer timerWithTimeInterval:self.timeInterval target:self selector:@selector(beginAnimaiton) userInfo:nil repeats:YES]; [[NSRunLoop mainRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];}#pragma mark 動(dòng)畫(huà)- (void)beginAnimaiton { static BOOL isOrignPostion = YES; if (isOrignPostion) { _scanningLine.y = 0; isOrignPostion = NO; [UIView animateWithDuration:self.timeInterval animations:^{ self->_scanningLine.y += 2; } completion:nil]; } else { if (_scanningLine.frame.origin.y >= 0) { CGFloat scanContent_MaxY = self.frame.size.width; if (_scanningLine.y >= scanContent_MaxY - 10) { _scanningLine.y = 0; isOrignPostion = YES; } else { [UIView animateWithDuration:0.02 animations:^{ self->_scanningLine.y += 2; } completion:nil]; } } else { isOrignPostion = !isOrignPostion; } }}
閃光燈按鈕點(diǎn)擊事件,開(kāi)啟關(guān)閉閃光燈:
- (void)lightBtnClick:(UIButton *)btn { btn.selected = !btn.selected; if (btn.selected) { [QRLightManager openFlashLight]; } else { [QRLightManager closeFlashLight]; }}
QRCodeScanManager
這個(gè)單例用來(lái)控制所有的關(guān)于二維碼掃描的事件
初始開(kāi)啟session 會(huì)話
//設(shè)置二維碼讀取率 數(shù)據(jù)類(lèi)型 當(dāng)前控制器- (void)setupSessionPreset:(NSString *)sessionPreset metadataObjectTypes:(NSArray *)metadataObjectTypes currentController:(UIViewController *)currentController { if (sessionPreset == nil || metadataObjectTypes == nil || currentController == nil) { NSException *excp = [NSException exceptionWithName:@"excp" reason:@"setupSessionPreset:metadataObjectTypes:currentController: 方法中的 sessionPreset 參數(shù)不能為空" userInfo:nil]; [excp raise]; } //1、獲取攝像設(shè)備 AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; //2、創(chuàng)建設(shè)備輸入流 AVCaptureDeviceInput *deviceInput = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil]; //3、創(chuàng)建數(shù)據(jù)輸出流 AVCaptureMetadataOutput *metadataOutput = [[AVCaptureMetadataOutput alloc] init]; [metadataOutput setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()]; //3(1)、創(chuàng)建設(shè)備輸出流 self.videoDataOutput = [[AVCaptureVideoDataOutput alloc] init]; [_videoDataOutput setSampleBufferDelegate:self queue:dispatch_get_main_queue()]; // 設(shè)置掃描范圍(每一個(gè)取值0~1,以屏幕右上角為坐標(biāo)原點(diǎn)) // 注:微信二維碼的掃描范圍是整個(gè)屏幕,這里并沒(méi)有做處理(可不用設(shè)置); 如需限制掃描范圍,打開(kāi)下一句注釋代碼并進(jìn)行相應(yīng)調(diào)試 // metadataOutput.rectOfInterest = CGRectMake(0.05, 0.2, 0.7, 0.6); //4、創(chuàng)建會(huì)話對(duì)象 _session = [[AVCaptureSession alloc] init]; //會(huì)話采集率:AVCaptureSessionPresetHigh _session.sessionPreset = sessionPreset; //5、添加設(shè)備輸出流到會(huì)話對(duì)象 [_session addOutput:metadataOutput]; //5(1)添加設(shè)備輸出流到會(huì)話對(duì)象;與3(1)構(gòu)成識(shí)別光纖強(qiáng)弱 [_session addOutput:_videoDataOutput]; //6、添加設(shè)備輸入流到會(huì)話對(duì)象 [_session addInput:deviceInput]; //7、設(shè)置數(shù)據(jù)輸出類(lèi)型,需要將數(shù)據(jù)輸出添加到會(huì)話后,才能指定元數(shù)據(jù)類(lèi)型,否則會(huì)報(bào)錯(cuò) // 設(shè)置掃碼支持的編碼格式(如下設(shè)置條形碼和二維碼兼容) // @[AVMetadataObjectTypeQRCode, AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode128Code] metadataOutput.metadataObjectTypes = metadataObjectTypes; //8、實(shí)例化預(yù)覽圖層,傳遞_session是為了告訴圖層將來(lái)顯示什么內(nèi)容 _videoPreviewLayer = [AVCaptureVideoPreviewLayer layerWithSession:_session]; //保持縱橫比;填充層邊界 _videoPreviewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill; CGFloat x = 0; CGFloat y = 0; CGFloat w = [UIScreen mainScreen].bounds.size.width; CGFloat h = [UIScreen mainScreen].bounds.size.height; _videoPreviewLayer.frame = CGRectMake(x, y, w, h); [currentController.view.layer insertSublayer:_videoPreviewLayer atIndex:0]; //9、啟動(dòng)會(huì)話 [_session startRunning]; }
block:
typedef void(^GetBrightnessBlock)(CGFloat brightness);//用來(lái)向外部傳遞捕獲到的亮度值以便于識(shí)別何時(shí)開(kāi)啟閃光燈,typedef void(^ScanBlock)(NSArray *metadataObjects);//捕獲到的結(jié)果集合//亮度回調(diào)- (void)brightnessChange:(GetBrightnessBlock)getBrightnessBlock;//掃描結(jié)果- (void)scanResult:(ScanBlock)scanBlock;
- (void)startRunning { [_session startRunning];}- (void)stopRunning { [_session stopRunning];}
需要遵循:/** 重置根據(jù)光線強(qiáng)弱值打開(kāi)手電筒 delegate方法 */- (void)resetSampleBufferDelegate;/** 取消根據(jù)光線強(qiáng)弱值打開(kāi)手電筒的delegate方法 */- (void)cancelSampleBufferDelegate;- (void)resetSampleBufferDelegate { [_videoDataOutput setSampleBufferDelegate:self queue:dispatch_get_main_queue()];}- (void)cancelSampleBufferDelegate { [_videoDataOutput setSampleBufferDelegate:nil queue:dispatch_get_main_queue()];}
#pragma mark 播放掃描提示音- (void)playSoundName:(NSString *)name { NSString *audioFile = [[NSBundle mainBundle] pathForResource:name ofType:nil]; NSURL *fileUrl = [NSURL fileURLWithPath:audioFile]; SystemSoundID soundID = 0; AudioServicesCreateSystemSoundID((__bridge CFURLRef)(fileUrl), &soundID); AudioServicesAddSystemSoundCompletion(soundID, NULL, NULL, soundCompleteCallback, NULL); AudioServicesPlaySystemSound(soundID); // 播放音效}void soundCompleteCallback(SystemSoundID soundID, void *clientData){ }
注:這里只是截取部分重要代碼,具體功能我會(huì)將代碼放到GitHub上,代碼里注釋也寫(xiě)的很明白
使用功能
開(kāi)啟、結(jié)束定時(shí)器
開(kāi)啟、關(guān)閉session會(huì)話
啟用、移除sampleBuffer代理
- (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [self.scanView addTimer]; [_scanManager startRunning]; [_scanManager resetSampleBufferDelegate];}- (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; [self.scanView removeTimer]; [_scanManager stopRunning]; [_scanManager cancelSampleBufferDelegate];}
初始化scanManager
- (void)setupScanManager { self.scanManager = [QRCodeScanManager sharedManager]; NSArray *arr = @[AVMetadataObjectTypeQRCode, AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode128Code]; [_scanManager setupSessionPreset:AVCaptureSessionPreset1920x1080 metadataObjectTypes:arr currentController:self]; __weak __typeof(self)weakSelf = self; //光掃描結(jié)果回調(diào) [_scanManager scanResult:^(NSArray *metadataObjects) { if (metadataObjects != nil && metadataObjects.count > 0) { [weakSelf.scanManager playSoundName:@"sound.caf"]; //obj 為掃描結(jié)果 AVMetadataMachineReadableCodeObject *obj = metadataObjects[0]; NSString *url = [obj stringValue]; NSLog(@"---url = :%@", url); } else { NSLog(@"暫未識(shí)別出掃描的二維碼"); } }]; //光纖變化回調(diào) [_scanManager brightnessChange:^(CGFloat brightness) { [weakSelf.scanView lightBtnChangeWithBrightnessValue:brightness]; }]; }
從相冊(cè)識(shí)別二維碼:
//借助第三方相冊(cè)- (void)albumBtnClick { TZImagePickerController *pickerController = [[TZImagePickerController alloc] initWithMaxImagesCount:1 delegate:self]; __weak __typeof(self)weakSelf = self; [pickerController setDidFinishPickingPhotosHandle:^(NSArray *photos, NSArray *assets, BOOL isSelectOriginalPhoto) { UIImage *image = photos[0]; // CIDetector(CIDetector可用于人臉識(shí)別)進(jìn)行圖片解析,從而使我們可以便捷的從相冊(cè)中獲取到二維碼 // 聲明一個(gè) CIDetector,并設(shè)定識(shí)別類(lèi)型 CIDetectorTypeQRCode // 識(shí)別精度 CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeQRCode context:nil options:@{CIDetectorAccuracy:CIDetectorAccuracyHigh}]; //取得識(shí)別結(jié)果 NSArray *features = [detector featuresInImage:[CIImage imageWithCGImage:image.CGImage]]; NSString *resultStr; if (features.count == 0) { NSLog(@"暫未識(shí)別出二維碼"); } else { for (int index = 0; index < [features count]; index++) { CIQRCodeFeature *feature = [features objectAtIndex:index]; resultStr = feature.messageString; } NSLog(@"---url:%@", resultStr); } }]; [self presentViewController:pickerController animated:YES completion:nil];}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持VEVB武林網(wǎng)。
|
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注