iOS二維碼識別/二維碼生成

最近在做一個關(guān)于二維碼的組件,已發(fā)布,現(xiàn)總結(jié)下。
開發(fā)的APP所需支持的最低版本為7.0,最初的方案為掃描使用蘋果自帶的API實現(xiàn)掃一掃的功能、使用ZXing識別從相冊或別人轉(zhuǎn)發(fā)的二維碼圖片。但發(fā)現(xiàn)ZXing識別從相冊中來的圖片性能很差,很多圖片識別不了,且耗時較長,遂使用ZBar來實現(xiàn)識別從相冊或別人轉(zhuǎn)發(fā)的二維碼圖片。
這個組件重要有三個功能,掃一掃識別二維碼圖片、長按圖片識別二維碼圖片和生成二維碼圖片。

掃一掃識別二維碼圖片

- (void)initCapture {
    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)閉相機使用權(quán)限,請至手機“設(shè)置->隱私->相機”中打開" 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:@"未能找到相機設(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 if ([self.captureSession canSetSessionPreset:AVCaptureSessionPreset1280x720]) {
        self.captureSession.sessionPreset = AVCaptureSessionPreset1280x720;
        w = 1280.f;
        h = 720.f;
    } else if ([self.captureSession canSetSessionPreset:AVCaptureSessionPreset640x480]) {
        self.captureSession.sessionPreset = AVCaptureSessionPreset640x480;
        w = 960.f;
        h = 540.f;
    }
    captureOutput.metadataObjectTypes = [captureOutput availableMetadataObjectTypes];
    CGRect bounds = [[UIScreen mainScreen] bounds];
    
    if (!self.prevLayer) {
        self.prevLayer = [AVCaptureVideoPreviewLayer layerWithSession:self.captureSession];
    }
    self.prevLayer.frame = bounds;
    self.prevLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
    
    [self.view.layer insertSublayer:self.prevLayer atIndex:0];
    
//    計算rectOfInterest
    CGFloat p1 = bounds.size.height/bounds.size.width;
    CGFloat p2 = w/h;
    
    CGRect cropRect = CGRectMake(CGRectGetMinX(_cropRect) - kQRReaderScanExpandWidth, CGRectGetMinY(_cropRect) - kQRReaderScanExpandHeight, CGRectGetWidth(_cropRect) + 2*kQRReaderScanExpandWidth, CGRectGetHeight(_cropRect) + 2*kQRReaderScanExpandHeight);
    
    if (fabs(p1 - p2) < 0.00001) {
        captureOutput.rectOfInterest = CGRectMake(cropRect.origin.y /bounds.size.height,
                                                  cropRect.origin.x/bounds.size.width,
                                                  cropRect.size.height/bounds.size.height,
                                                  cropRect.size.width/bounds.size.width);
    } else if (p1 < p2) {
        //實際圖像被截取一段高
        CGFloat fixHeight = bounds.size.width * w / h;
        CGFloat fixPadding = (fixHeight - bounds.size.height)/2;
        captureOutput.rectOfInterest = CGRectMake((cropRect.origin.y + fixPadding)/fixHeight,
                                                  cropRect.origin.x/bounds.size.width,
                                                  cropRect.size.height/fixHeight,
                                                  cropRect.size.width/bounds.size.width);
    } else {
        CGFloat fixWidth = bounds.size.height * h / w;
        CGFloat fixPadding = (fixWidth - bounds.size.width)/2;
        captureOutput.rectOfInterest = CGRectMake(cropRect.origin.y/bounds.size.height,
                                                  (cropRect.origin.x + fixPadding)/fixWidth,
                                                  cropRect.size.height/bounds.size.height,
                                                  cropRect.size.width/fixWidth);
    }
}

長按圖片識別二維碼圖片

識別圖片使用的是ZBar,最初的方案為ZXing,因為ZXing有人在維護,但ZXing識別相冊中的二維碼圖片或本地的圖片,有些圖片根本就識別不出來,且耗時較長,所以改為使用ZBar。在網(wǎng)上找到一篇文章再見ZXing 使用系統(tǒng)原生代碼處理QRCode,實測發(fā)現(xiàn)在iOS9,iphone4s上傳回來的數(shù)組為空。代碼如下:

    //decode
    - (NSString *)decodeQRImageWith:(UIImage*)aImage {
        NSString *qrResult = nil;
        
        //iOS8及以上可以使用系統(tǒng)自帶的識別二維碼圖片接口,但此api有問題,在一些機型上detector為nil。
        
        //    if (iOS8_OR_LATER) {
        //        CIContext *context = [CIContext contextWithOptions:nil];
        //        CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeQRCode context:context options:@{CIDetectorAccuracy:CIDetectorAccuracyHigh}];
        //        CIImage *image = [CIImage imageWithCGImage:aImage.CGImage];
        //        NSArray *features = [detector featuresInImage:image];
        //        CIQRCodeFeature *feature = [features firstObject];
        //
        //        qrResult = feature.messageString;
        //    } else {
        
        ZBarReaderController* read = [ZBarReaderController new];
        CGImageRef cgImageRef = aImage.CGImage;
        ZBarSymbol* symbol = nil;
        for(symbol in [read scanImage:cgImageRef]) break;
        qrResult = symbol.data ;
        return qrResult;
    }

無圖無真相:

14567CBE-E1D2-4FA7-AFA3-8B2037171F38.jpg

detector的值為nil,也就是說

    CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeQRCode context:context options:@{CIDetectorAccuracy:CIDetectorAccuracyHigh}];

CIDetector的初始化方法無效。推測是蘋果API的問題。

生成二維碼圖片

在iOS8及以上版本使用蘋果的API生成二維碼圖片,代碼如下:

- (UIImage *)encodeQRImageWithContent:(NSString *)content size:(CGSize)size {
    UIImage *codeImage = nil;
    if (iOS8_OR_LATER) {
        NSData *stringData = [content dataUsingEncoding: NSUTF8StringEncoding];
       
        //生成
        CIFilter *qrFilter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
        [qrFilter setValue:stringData forKey:@"inputMessage"];
        [qrFilter setValue:@"M" forKey:@"inputCorrectionLevel"];
        
        UIColor *onColor = [UIColor blackColor];
        UIColor *offColor = [UIColor whiteColor];
        
        //上色
        CIFilter *colorFilter = [CIFilter filterWithName:@"CIFalseColor"
                                           keysAndValues:
                                 @"inputImage",qrFilter.outputImage,
                                 @"inputColor0",[CIColor colorWithCGColor:onColor.CGColor],
                                 @"inputColor1",[CIColor colorWithCGColor:offColor.CGColor],
                                 nil];
        
        CIImage *qrImage = colorFilter.outputImage;
        CGImageRef cgImage = [[CIContext contextWithOptions:nil] createCGImage:qrImage fromRect:qrImage.extent];
        UIGraphicsBeginImageContext(size);
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGContextSetInterpolationQuality(context, kCGInterpolationNone);
        CGContextScaleCTM(context, 1.0, -1.0);
        CGContextDrawImage(context, CGContextGetClipBoundingBox(context), cgImage);
        codeImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        
        CGImageRelease(cgImage);
    } else {
        codeImage = [QRCodeGenerator qrImageForString:content imageSize:size.width];
    }
    return codeImage;
}

iOS8以下使用libqrencode庫來生成二維碼圖片。

代碼完善

2015年12月11日

QA測試發(fā)現(xiàn),服務(wù)端生成的二維碼,使用ZBar識別不出來,但將這張圖片保存到相冊,然后發(fā)送就可以識別出來。最初的想法是要服務(wù)端修改生成的二維碼,但安卓能夠識別出來,此路不通,那只有看ZBar的源碼了。

- (id <NSFastEnumeration>) scanImage: (CGImageRef) image
{
    timer_start;
    int nsyms = [self scanImage: image
                      withScaling: 0];
  //沒有識別出來,判斷CGImageRef對象的寬和高是否大于640,大于或等于的話進行縮放再進行掃描
    if(!nsyms &&
       CGImageGetWidth(image) >= 640 &&
       CGImageGetHeight(image) >= 640)
        // make one more attempt for close up, grainy images
        nsyms = [self scanImage: image
                      withScaling: .5];

    NSMutableArray *syms = nil;
    if(nsyms) {
        // quality/type filtering
        int max_quality = MIN_QUALITY;
        for(ZBarSymbol *sym in scanner.results) {
            zbar_symbol_type_t type = sym.type;
            int quality;
            if(type == ZBAR_QRCODE)
                quality = INT_MAX;
            else
                quality = sym.quality;

            if(quality < max_quality) {
                zlog(@"    type=%d quality=%d < %d\n",
                     type, quality, max_quality);
                continue;
            }

            if(max_quality < quality) {
                max_quality = quality;
                if(syms)
                    [syms removeAllObjects];
            }
            zlog(@"    type=%d quality=%d\n", type, quality);
            if(!syms)
                syms = [NSMutableArray arrayWithCapacity: 1];

            [syms addObject: sym];
        }
    }

    zlog(@"read %d filtered symbols in %gs total\n",
          (!syms) ? 0 : [syms count], timer_elapsed(t_start, timer_now()));
    return(syms);
}

在這里就產(chǎn)生了一個解決有些二維碼圖片識別不出來的解決思路:將傳過來的UIImage的寬和高設(shè)置為640,識別不出來再進行縮放識別。修改UIImage的代碼如下:

-(UIImage *)TransformtoSize:(CGSize)Newsize
{
    // 創(chuàng)建一個bitmap的context
    UIGraphicsBeginImageContext(Newsize);
    // 繪制改變大小的圖片
    [self drawInRect:CGRectMake(0, 0, Newsize.width, Newsize.height)];
    // 從當(dāng)前context中創(chuàng)建一個改變大小后的圖片
    UIImage *TransformedImg=UIGraphicsGetImageFromCurrentImageContext();
    // 使當(dāng)前的context出堆棧
    UIGraphicsEndImageContext();
    // 返回新的改變大小后的圖片
    return TransformedImg;
}

這樣類似于ZXing中的tryHard設(shè)置為YES。識別不出來的二維碼圖片就可以識別了。

2016年5月20日
bug: 點擊進入掃一掃界面,退出,再進入,這樣重復(fù)5次左右,掃一掃之前的界面的會出現(xiàn)卡頓。
原因:多次進入掃一掃界面,再退出,因此界面未被系統(tǒng)回收,captureSession對象一直在運行,會造成內(nèi)存泄露,引起上一個界面卡頓。
解決方案:在視圖將要消失的時候,確保captureSession對象停止運行。

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    if ([self.captureSession isRunning]) {
        [self.captureSession stopRunning];
    }
}

2018年4月28日
識別二維碼圖片優(yōu)化

近期通過bugly收集卡頓問題發(fā)現(xiàn),二維碼組件在識別二維碼圖片時,會出現(xiàn)卡頓問題。為優(yōu)化識別速度,采用了三種方案,并分別進行測試,并對測試數(shù)據(jù)進行分析,最終挑選出最優(yōu)的方案。

任務(wù)A:使用系統(tǒng)提供的CoreImage的CIDetector接口去識別二維碼圖片,返回對應(yīng)的字符串;
任務(wù)B:使用zbar中的方法去識別二維碼圖片,返回對應(yīng)的字符串。

//任務(wù)A
+ (NSString *)useSystemMethodDecodeImage:(UIImage *)image {
    NSString *resultString = nil;
    CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeQRCode
                                              context:nil
                                              options:@{CIDetectorAccuracy:CIDetectorAccuracyHigh}];
    if (detector) {
        CIImage *ciImage = [CIImage imageWithCGImage:image.CGImage];
        NSArray *features = [detector featuresInImage:ciImage];
        CIQRCodeFeature *feature = [features firstObject];
        resultString = feature.messageString;
    }
    return resultString;
}
//任務(wù)B
+ (NSString *)useZbarMethodDecodeImage:(UIImage *)image {
    UIImage *decodeImage = image;
    if (decodeImage.size.width < 641) {
        decodeImage = [decodeImage TransformtoSize:CGSizeMake(640, 640)];
    }
    QRCodeZBarReaderController* read = [QRCodeZBarReaderController new];
    CGImageRef cgImageRef = decodeImage.CGImage;
    QRCodeZBarSymbol *symbol = nil;
    for(symbol in [read scanImage:cgImageRef]) break;
    return symbol.data;
}
  • 方案A:先執(zhí)行任務(wù)A,如果獲取到的字符串為空,再執(zhí)行任務(wù)B。
+ (NSString *)planOneDecodeWithImage:(UIImage *)image index:(NSInteger)index{
 
    NSMutableString *costTimeInfo = [NSMutableString stringWithFormat:@"%ld\r\n",index];
    CFAbsoluteTime startTime = CFAbsoluteTimeGetCurrent();
    NSString *detectorString = [MUIQRCodeDecoder useSystemMethodDecodeImage:image];
    CFAbsoluteTime detectorCostTime = (CFAbsoluteTimeGetCurrent() - startTime);
    
    [costTimeInfo appendString:[NSString stringWithFormat:@"detector : %f ms\r\n",detectorCostTime *1000.0]];
    
    NSAssert(detectorString.length > 0, @"detector fail!");
    CFAbsoluteTime zbarStartTime = CFAbsoluteTimeGetCurrent();
    NSString *zbarSymbolString = [MUIQRCodeDecoder useZbarMethodDecodeImage:image];
    NSAssert(zbarSymbolString.length > 0, @"zbar fail!");
    CFAbsoluteTime zbarCostTime = (CFAbsoluteTimeGetCurrent() - zbarStartTime);
    
    [costTimeInfo appendString:[NSString stringWithFormat:@"zbar : %f ms\r\n",zbarCostTime *1000.0]];
    
    CFAbsoluteTime totalCostTime = (CFAbsoluteTimeGetCurrent() - startTime);
    
    [costTimeInfo appendString:[NSString stringWithFormat:@"total cost : %f ms\r\n",totalCostTime *1000.0]];
    [costTimeInfo appendString:[NSString stringWithFormat:@"detectorString : %@ ms\r\n",detectorString]];
    [costTimeInfo appendString:[NSString stringWithFormat:@"zbarSymbolString : %@ ms\r\n",zbarSymbolString]];
    return [costTimeInfo copy];
}
  • 方案B:同時執(zhí)行任務(wù)A和任務(wù)B,兩者均執(zhí)行完后,返回識別的結(jié)果;
+ (NSString *)planTwoDecodeWithImage:(UIImage *)image index:(NSInteger)index { 
    __block NSMutableString *costTimeInfo = [NSMutableString stringWithFormat:@"%ld\r\n",index];
    __block NSString *detectorString = nil;
    __block NSString *zbarSymbolString = nil;
    
    CFAbsoluteTime startTime = CFAbsoluteTimeGetCurrent();
    
    dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
    dispatch_group_t group = dispatch_group_create();
    
    dispatch_group_async(group, dispatch_get_global_queue(0,0), ^{
        detectorString = [MUIQRCodeDecoder useSystemMethodDecodeImage:image];
        NSAssert(detectorString.length > 0, @"detector fail!");
        CFAbsoluteTime costTime = (CFAbsoluteTimeGetCurrent() - startTime);
        [costTimeInfo appendString:[NSString stringWithFormat:@"detector : %f ms\r\n",costTime *1000.0]];
    });
    
    dispatch_group_async(group, dispatch_get_global_queue(0,0), ^{
        zbarSymbolString = [MUIQRCodeDecoder useZbarMethodDecodeImage:image];
        NSAssert(zbarSymbolString.length > 0, @"zbar fail!");
        CFAbsoluteTime costTime = (CFAbsoluteTimeGetCurrent() - startTime);
        [costTimeInfo appendString:[NSString stringWithFormat:@"zbar : %f ms\r\n",costTime *1000.0]];
    });
    
    dispatch_group_notify(group, dispatch_get_global_queue(0,0), ^{
        dispatch_semaphore_signal(semaphore);
    });
    
    dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
    
    CFAbsoluteTime totalCostTime = (CFAbsoluteTimeGetCurrent() - startTime);
    [costTimeInfo appendString:[NSString stringWithFormat:@"total cost : %f ms\r\n",totalCostTime *1000.0]];
    [costTimeInfo appendString:[NSString stringWithFormat:@"detectorString : %@ ms\r\n",detectorString]];
    [costTimeInfo appendString:[NSString stringWithFormat:@"zbarSymbolString : %@ ms\r\n",zbarSymbolString]];
    return [costTimeInfo copy];
}
  • 方案C:同時執(zhí)行任務(wù)A和任務(wù)B
    1、任務(wù)A先執(zhí)行完且識別成功,返回識別結(jié)果;
    2、任務(wù)B先執(zhí)行完且識別成功,返回識別結(jié)果;
    3、任務(wù)A和任務(wù)B均識別失敗,兩者均執(zhí)行完后,返回識別的結(jié)果。
+ (NSString *)planThreeDecodeWithImage:(UIImage *)image index:(NSInteger)index {
    __block NSMutableString *costTimeInfo = [NSMutableString stringWithFormat:@"%ld\r\n",index];
    __block NSString *detectorString = nil;
    __block NSString *zbarSymbolString = nil;
    __block BOOL isNeedSendSignal = YES;
    
    CFAbsoluteTime startTime = CFAbsoluteTimeGetCurrent();
    
    dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
    dispatch_group_t group = dispatch_group_create();
    
    dispatch_group_async(group, dispatch_get_global_queue(0,0), ^{
        detectorString = [MUIQRCodeDecoder useSystemMethodDecodeImage:image];
        //NSAssert(detectorString.length > 0, @"detector fail!");
        CFAbsoluteTime costTime = (CFAbsoluteTimeGetCurrent() - startTime);
        [costTimeInfo appendString:[NSString stringWithFormat:@"detector : %f ms\r\n",costTime *1000.0]];
        if (detectorString.length > 0 && isNeedSendSignal) {
            isNeedSendSignal = NO;
            dispatch_semaphore_signal(semaphore);
        }
    });
    
    dispatch_group_async(group, dispatch_get_global_queue(0,0), ^{
        zbarSymbolString = [MUIQRCodeDecoder useZbarMethodDecodeImage:image];
        //NSAssert(zbarSymbolString.length > 0, @"zbar fail!");
        CFAbsoluteTime costTime = (CFAbsoluteTimeGetCurrent() - startTime);
        [costTimeInfo appendString:[NSString stringWithFormat:@"zbar : %f ms\r\n",costTime *1000.0]];
        if (zbarSymbolString.length > 0 && isNeedSendSignal) {
            isNeedSendSignal = NO;
            dispatch_semaphore_signal(semaphore);
        }
    });
    
    dispatch_group_notify(group, dispatch_get_global_queue(0,0), ^{
        if (isNeedSendSignal) {
            dispatch_semaphore_signal(semaphore);
        }
    });
    
    dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
    
    CFAbsoluteTime totalCostTime = (CFAbsoluteTimeGetCurrent() - startTime);
    [costTimeInfo appendString:[NSString stringWithFormat:@"total cost : %f ms\r\n",totalCostTime *1000.0]];
    [costTimeInfo appendString:[NSString stringWithFormat:@"detectorString : %@ ms\r\n",detectorString]];
    [costTimeInfo appendString:[NSString stringWithFormat:@"zbarSymbolString : %@ ms\r\n",zbarSymbolString]]; 
    return [costTimeInfo copy];
}

測試數(shù)據(jù)如下所示:(取了前10張圖片)

識別二維碼圖片耗時.png

分析測試數(shù)據(jù)發(fā)現(xiàn):
1、在測試第一張二維碼圖片時,總耗時均較大,如果第一次識別使用的是系統(tǒng)方法,耗時超過500ms,這也是為什么會出現(xiàn)卡頓的原因;
2、使用系統(tǒng)方法去識別二維碼圖片時,如果不是第一次去識別,耗時較小,在65ms以內(nèi);
3、使用zbar的方法去識別二維碼圖片,耗時均值在200ms以內(nèi);
4、在方案C中,如果第一次使用系統(tǒng)方法,耗時為226ms。

總結(jié)得出,從優(yōu)化卡頓問題的角度出發(fā),使用方案C最優(yōu),同時發(fā)現(xiàn),如果使用系統(tǒng)方法能識別出二維碼圖片,在初始化之后(也就是第二次使用),耗時最短。同時因為在實際的使用場景中,圖片是一張一張識別的,識別過程有一個間隔時間,如果已經(jīng)使用系統(tǒng)方法識別過二維碼圖片,那下次識別就能達到最優(yōu)。所以使用方案C的話,最差情況均值在200ms左右,最好的情況和方案A中第二次使用系統(tǒng)方法耗時基本一致。綜合考慮,使用方案C。

小結(jié)

在實際的項目開發(fā)過程中,設(shè)想的情況和實際情況會存在偏差,需要自己時刻使用性能調(diào)優(yōu)工具,根據(jù)數(shù)據(jù)去進行優(yōu)化,而不能想當(dāng)然的認(rèn)為某種方式是最優(yōu)的。
源碼和demo請點這里
參考的文章鏈接如下
再見ZXing 使用系統(tǒng)原生代碼處理QRCode
IOS二維碼掃描,你需要注意的兩件事
Zbar算法流程介紹
本文已經(jīng)同步到我的個人技術(shù)博客: 傳送門 ,歡迎常來^^。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容