iOS 扫描相册图片二维码

栏目: IOS · 发布时间: 7年前

内容简介:级别:★★☆☆☆标签:「iOS CIDetector」「CIQRCodeFeature」「识别相册图片」作者: Xs·H

级别:★★☆☆☆

标签:「iOS CIDetector」「CIQRCodeFeature」「识别相册图片」

作者: Xs·H

审校:QiShare团队

接上篇 iOS 扫描二维码/条形码 ,本文补充介绍扫描相册图片上二维码的实现方式。先看看 QiQRCode 中的示例效果:

iOS 扫描相册图片二维码

iOS 8 之后,可以结合 CIDetector 使用 CIQRCodeFeature 实现扫描相册图片上二维码的功能。具体实现过程如下:

1、遵守协议

@interface QiCodeManager () <UIImagePickerControllerDelegate, UINavigationControllerDelegate>
复制代码

2、打开相册

- (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、实现代理

// 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、透传给业务

- (void)handleCodeString:(NSString *)codeString {
    
    if (_autoStop) {
        [self stopScanning];
    }
    if (_callback) {
        _callback(codeString);
    }
}
复制代码

5、业务调用方式

// 创建“相册”按钮
UIBarButtonItem *photoItem = [[UIBarButtonItem alloc] initWithTitle:@"相册" style:UIBarButtonItemStylePlain target:self action:@selector(photo:)];

self.navigationItem.rightBarButtonItem = photoItem;
复制代码
// 实现“相册”按钮方法
- (void)photo:(id)sender {
    
    __weak typeof(self) weakSelf = self;
    [_codeManager presentPhotoLibraryWithRooter:self callback:^(NSString * _Nonnull code) {
        [weakSelf performSegueWithIdentifier:@"showCodeGeneration" sender:code];
    }];
}
复制代码

上述代码中的核心步骤是第3步—实现代理。 我们通过 UIImagePickerController 拿到 image 后,使用 CIDetectorCIQRCodeFeature 读取 image 上的信息,最终得到的 feature.messageString 就是二维码的字符串信息。

示例源码QiQRCode 可从GitHub的 QiShare开源库 中获取。

关注我们的途径有:

QiShare(简书)

QiShare(掘金)

QiShare(知乎)

QiShare(GitHub)

QiShare(CocoaChina)

QiShare(StackOverflow)

QiShare(微信公众号)


以上所述就是小编给大家介绍的《iOS 扫描相册图片二维码》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Practical Vim, Second Edition

Practical Vim, Second Edition

Drew Neil / The Pragmatic Bookshelf / 2015-10-31 / USD 29.00

Vim is a fast and efficient text editor that will make you a faster and more efficient developer. It’s available on almost every OS, and if you master the techniques in this book, you’ll never need an......一起来看看 《Practical Vim, Second Edition》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器