iOS 掃描相冊(cè)圖片二維碼

級(jí)別:★★☆☆☆
標(biāo)簽:「iOS CIDetector」「CIQRCodeFeature」「識(shí)別相冊(cè)圖片」
作者: Xs·H
審校: QiShare團(tuán)隊(duì)


接上篇 iOS 掃描二維碼/條形碼,本文補(bǔ)充介紹掃描相冊(cè)圖片上二維碼的實(shí)現(xiàn)方式。先看看QiQRCode中的示例效果:

iOS 掃描相冊(cè)圖片上二維碼效果

iOS 8之后,可以結(jié)合CIDetector使用CIQRCodeFeature實(shí)現(xiàn)掃描相冊(cè)圖片上二維碼的功能。具體實(shí)現(xiàn)過程如下:

1、遵守協(xié)議
@interface QiCodeManager () <UIImagePickerControllerDelegate, UINavigationControllerDelegate>
2、打開相冊(cè)
- (void)presentPhotoLibraryWithRooter:(UIViewController *)rooter callback:(nonnull void (^)(NSString * _Nonnull))callback {
    _callback = callback;
    
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    // imagePicker.allowsEditing = YES;
    imagePicker.delegate = self;
    [rooter presentViewController:imagePicker animated:YES completion:nil];
}
3、實(shí)現(xiàn)代理
// UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey,id> *)info {
    
    UIImage *pickedImage = info[UIImagePickerControllerEditedImage] ?: info[UIImagePickerControllerOriginalImage];
    CIImage *detectImage = [CIImage imageWithData:UIImagePNGRepresentation(pickedImage)];
    
    CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeQRCode context:nil options:@{CIDetectorAccuracy: CIDetectorAccuracyLow}];
    CIQRCodeFeature *feature = (CIQRCodeFeature *)[detector featuresInImage:detectImage options:nil].firstObject;
    
    [picker dismissViewControllerAnimated:YES completion:^{
        if (feature.messageString) {
            [self handleCodeString:feature.messageString];
        }
    }];
}
4、透?jìng)鹘o業(yè)務(wù)
- (void)handleCodeString:(NSString *)codeString {
    
    if (_autoStop) {
        [self stopScanning];
    }
    if (_callback) {
        _callback(codeString);
    }
}
5、業(yè)務(wù)調(diào)用方式
// 創(chuàng)建“相冊(cè)”按鈕
UIBarButtonItem *photoItem = [[UIBarButtonItem alloc] initWithTitle:@"相冊(cè)" style:UIBarButtonItemStylePlain target:self action:@selector(photo:)];

self.navigationItem.rightBarButtonItem = photoItem;
// 實(shí)現(xiàn)“相冊(cè)”按鈕方法
- (void)photo:(id)sender {
    
    __weak typeof(self) weakSelf = self;
    [_codeManager presentPhotoLibraryWithRooter:self callback:^(NSString * _Nonnull code) {
        [weakSelf performSegueWithIdentifier:@"showCodeGeneration" sender:code];
    }];
}

上述代碼中的核心步驟是第3步—實(shí)現(xiàn)代理。
我們通過UIImagePickerController拿到image后,使用CIDetectorCIQRCodeFeature讀取image上的信息,最終得到的feature.messageString就是二維碼的字符串信息。


示例源碼QiQRCode可從GitHub的QiShare開源庫中獲取。


推薦文章:
iOS 掃描二維碼/條形碼
iOS 了解Xcode Bitcode
iOS 重繪之drawRect
iOS 編寫高質(zhì)量Objective-C代碼(八)

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

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

  • 1、通過CocoaPods安裝項(xiàng)目名稱項(xiàng)目信息 AFNetworking網(wǎng)絡(luò)請(qǐng)求組件 FMDB本地?cái)?shù)據(jù)庫組件 SD...
    陽明AI閱讀 16,198評(píng)論 3 119
  • D13成就孩子一生幸福的媽媽情商課 讓我們的孩子成熟、自由、綻放于愛與善良之中!讓我們的家庭成為溫暖、和諧...
    小龍女安閱讀 230評(píng)論 0 0
  • 今天要聊一聊村上春樹。第一次看村上春樹的書是很多很多年前,是那本如今已經(jīng)翻閱得殘缺掉的《挪威的森林》,在無所事事的...
    金俊杰閱讀 1,906評(píng)論 38 51
  • 常見的助動(dòng)詞有: be(無詞義),do(無詞義),have(無詞義),shall 將要,will 將要。 一、be...
    春喜外語閱讀 1,949評(píng)論 0 0
  • 圖片來自網(wǎng)絡(luò),如有侵權(quán),請(qǐng)聯(lián)系刪除。 北國(guó)冬風(fēng)飄玉屑, 潔純清冷去煩嫌。 萬家燈火邀星月,(注) 數(shù)盞瓊漿腹內(nèi)炎。...
    青松_534d閱讀 2,843評(píng)論 35 74

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