您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關怎么在iOS中實現二維碼掃描功能,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。
控件封裝后主要結構如圖:
如圖中代碼目錄,vender里面放的是UIView+Frame分類,Resource里面放的是圖片聲音資源,TZImagePickerController是第三方相冊,用來獲取相冊中的二維碼識別的。主要的就是以QR開頭的文件,我們具體說一說。
QRCode.h
這個文件主要放的是各個文件的頭文件,方便在各處調用
#import "QRCodeScanManager.h" #import #import "QRLightManager.h" #import "QRCodeScanView.h" #import "QRCodeHelper.h"
QRLightManager
這個類是用來開啟關閉閃光燈的
/** 打開手電筒 */ + (void)openFlashLight; /** 關閉手電筒 */ + (void)closeFlashLight;
#pragma mark 打開手電筒 + (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 關閉手電筒 + (void)closeFlashLight { AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; if ([device hasTorch]) { [device lockForConfiguration:nil]; [device setTorchMode:AVCaptureTorchModeOff]; [device unlockForConfiguration]; } }
QRCodeScanView
這個類是將這個界面單獨封裝出來,便于自定義
在.h文件中有個枚舉用來標識二維碼掃描四周角標的位置:
typedef enum : NSUInteger { CornerLoactionDefault,//默認與邊框同中心點 CornerLoactionInside,//在邊框線內部 CornerLoactionOutside,//在邊框線外部 } CornerLoaction;
自定義view各個屬性:
@property (nonatomic, strong) UIColor *borderColor;/** 邊框顏色*/ @property (nonatomic, assign) CornerLoaction cornerLocation;/** 邊角位置 默認default*/ @property (nonatomic, strong) UIColor *cornerColor;/** 邊角顏色 默認正保藍#32d2dc*/ @property (nonatomic, assign) CGFloat cornerWidth;/** 邊角寬度 默認2.f*/ @property (nonatomic, assign) CGFloat backgroundAlpha;/** 掃描區周邊顏色的alpha 默認0.5*/ @property (nonatomic, assign) CGFloat timeInterval;/** 掃描間隔 默認0.02*/ @property (nonatomic, strong) UIButton *lightBtn;/** 閃光燈*/
暴露外部調用的方法:
/** 添加定時器 */ - (void)addTimer; /** 移除定時器 */ - (void)removeTimer; /** 根據燈光判斷 */ - (void)lightBtnChangeWithBrightnessValue:(CGFloat)brightnessValue;
初始化默認值:
- (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]; }
重寫view的drawRect方法,在這個方法里面繪制scanView的邊框和邊角
//空白區域設置 [[[UIColor blackColor] colorWithAlphaComponent:self.backgroundAlpha] setFill]; UIRectFill(rect); //獲取上下文,并設置混合模式 -> kCGBlendModeDestinationOut CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetBlendMode(context, kCGBlendModeDestinationOut); //設置空白區 UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRect:CGRectMake(borderX + 0.5 * borderLineW, borderY+ 0.5 * borderLineW, borderW - borderLineW, borderH - borderLineW)]; [bezierPath fill]; //執行混合模式 CGContextSetBlendMode(context, kCGBlendModeNormal); //邊框設置 UIBezierPath *borderPath = [UIBezierPath bezierPathWithRect:CGRectMake(borderX, borderY, borderW, borderH)]; borderPath.lineCapStyle = kCGLineCapButt; borderPath.lineWidth = borderLineW; [self.borderColor set]; [borderPath stroke]; CGFloat cornerLength = 20; //左上角小圖標 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];
增加定時器以及開始動畫:
- (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 動畫 - (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; } } }
閃光燈按鈕點擊事件,開啟關閉閃光燈:
- (void)lightBtnClick:(UIButton *)btn { btn.selected = !btn.selected; if (btn.selected) { [QRLightManager openFlashLight]; } else { [QRLightManager closeFlashLight]; } }
QRCodeScanManager
這個單例用來控制所有的關于二維碼掃描的事件
初始開啟session 會話
//設置二維碼讀取率 數據類型 當前控制器 - (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 參數不能為空" userInfo:nil]; [excp raise]; } //1、獲取攝像設備 AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; //2、創建設備輸入流 AVCaptureDeviceInput *deviceInput = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil]; //3、創建數據輸出流 AVCaptureMetadataOutput *metadataOutput = [[AVCaptureMetadataOutput alloc] init]; [metadataOutput setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()]; //3(1)、創建設備輸出流 self.videoDataOutput = [[AVCaptureVideoDataOutput alloc] init]; [_videoDataOutput setSampleBufferDelegate:self queue:dispatch_get_main_queue()]; // 設置掃描范圍(每一個取值0~1,以屏幕右上角為坐標原點) // 注:微信二維碼的掃描范圍是整個屏幕,這里并沒有做處理(可不用設置); 如需限制掃描范圍,打開下一句注釋代碼并進行相應調試 // metadataOutput.rectOfInterest = CGRectMake(0.05, 0.2, 0.7, 0.6); //4、創建會話對象 _session = [[AVCaptureSession alloc] init]; //會話采集率:AVCaptureSessionPresetHigh _session.sessionPreset = sessionPreset; //5、添加設備輸出流到會話對象 [_session addOutput:metadataOutput]; //5(1)添加設備輸出流到會話對象;與3(1)構成識別光纖強弱 [_session addOutput:_videoDataOutput]; //6、添加設備輸入流到會話對象 [_session addInput:deviceInput]; //7、設置數據輸出類型,需要將數據輸出添加到會話后,才能指定元數據類型,否則會報錯 // 設置掃碼支持的編碼格式(如下設置條形碼和二維碼兼容) // @[AVMetadataObjectTypeQRCode, AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode128Code] metadataOutput.metadataObjectTypes = metadataObjectTypes; //8、實例化預覽圖層,傳遞_session是為了告訴圖層將來顯示什么內容 _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、啟動會話 [_session startRunning]; }
block:
typedef void(^GetBrightnessBlock)(CGFloat brightness);//用來向外部傳遞捕獲到的亮度值以便于識別何時開啟閃光燈, typedef void(^ScanBlock)(NSArray *metadataObjects);//捕獲到的結果集合 //亮度回調 - (void)brightnessChange:(GetBrightnessBlock)getBrightnessBlock; //掃描結果 - (void)scanResult:(ScanBlock)scanBlock;
- (void)startRunning { [_session startRunning]; } - (void)stopRunning { [_session stopRunning]; }
需要遵循:/** 重置根據光線強弱值打開手電筒 delegate方法 */ - (void)resetSampleBufferDelegate; /** 取消根據光線強弱值打開手電筒的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){ }
注:這里只是截取部分重要代碼,具體功能我會將代碼放到GitHub上,代碼里注釋也寫的很明白
使用功能
開啟、結束定時器
開啟、關閉session會話
啟用、移除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; //光掃描結果回調 [_scanManager scanResult:^(NSArray *metadataObjects) { if (metadataObjects != nil && metadataObjects.count > 0) { [weakSelf.scanManager playSoundName:@"sound.caf"]; //obj 為掃描結果 AVMetadataMachineReadableCodeObject *obj = metadataObjects[0]; NSString *url = [obj stringValue]; NSLog(@"---url = :%@", url); } else { NSLog(@"暫未識別出掃描的二維碼"); } }]; //光纖變化回調 [_scanManager brightnessChange:^(CGFloat brightness) { [weakSelf.scanView lightBtnChangeWithBrightnessValue:brightness]; }]; }
從相冊識別二維碼:
//借助第三方相冊 - (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可用于人臉識別)進行圖片解析,從而使我們可以便捷的從相冊中獲取到二維碼 // 聲明一個 CIDetector,并設定識別類型 CIDetectorTypeQRCode // 識別精度 CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeQRCode context:nil options:@{CIDetectorAccuracy:CIDetectorAccuracyHigh}]; //取得識別結果 NSArray *features = [detector featuresInImage:[CIImage imageWithCGImage:image.CGImage]]; NSString *resultStr; if (features.count == 0) { NSLog(@"暫未識別出二維碼"); } 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]; }
關于怎么在iOS中實現二維碼掃描功能就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。