最近在做一個(gè)關(guān)于二維碼的組件,已發(fā)布,現(xiàn)總結(jié)下。開(kāi)發(fā)的APP所需支持的最低版本為7.0,最初的方案為掃描使用蘋(píng)果自帶的API實(shí)現(xiàn)掃一掃的功能、使用ZXing識(shí)別從相冊(cè)或別人轉(zhuǎn)發(fā)的二維碼圖片。但發(fā)現(xiàn)ZXing識(shí)別從相冊(cè)中來(lái)的圖片性能很差,很多圖片識(shí)別不了,且耗時(shí)較長(zhǎng),遂使用ZBar來(lái)實(shí)現(xiàn)識(shí)別從相冊(cè)或別人轉(zhuǎn)發(fā)的二維碼圖片。
掃描:
AVCaptureDevice* inputDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; [inputDevice lockForConfiguration:nil]; if ([inputDevice hasTorch]) { inputDevice.torchMode = AVCaptureTorchModeAuto; } [inputDevice unlockForConfiguration]; AVCaptureDeviceInput *captureInput = [AVCaptureDeviceInput deviceInputWithDevice:inputDevice error:nil]; if (!captureInput) { if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) { UIAlertController *alterVC = [UIAlertController alertControllerWithTitle:@"系統(tǒng)提示" message:@"您已關(guān)閉相機(jī)使用權(quán)限,請(qǐng)至手機(jī)“設(shè)置->隱私->相機(jī)”中打開(kāi)" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:nil]; [alterVC addAction:confirmAction]; [self presentViewController:alterVC animated:YES completion:nil]; } else { UIAlertController *alterVC = [UIAlertController alertControllerWithTitle:@"系統(tǒng)提示" message:@"未能找到相機(jī)設(shè)備" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:nil]; [alterVC addAction:confirmAction]; [self presentViewController:alterVC animated:YES completion:nil]; } return; } AVCaptureMetadataOutput *captureOutput = [[AVCaptureMetadataOutput alloc] init]; [captureOutput setMetadataObjectsDelegate:self queue:_queue]; self.captureOutput = captureOutput; self.captureSession = [[AVCaptureSession alloc] init]; [self.captureSession addInput:captureInput]; [self.captureSession addOutput:captureOutput]; CGFloat w = 1920.f; CGFloat h = 1080.f; if ([self.captureSession canSetSessionPreset:AVCaptureSessionPreset1920x1080]) { self.captureSession.sessionPreset = AVCaptureSessionPreset1920x1080; } else