iOS 识别图片二维码demo,复制粘贴即用

 

 

 

 

 

 

 

#import "ViewController.h"

@interface ViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>

/** 背景图片 */
@property (nonatomic, strong) UIImageView *imgView;

@end

@implementation ViewController
- (UIImageView *)imgView {
    if (!_imgView) {
        UIImageView *imgView = [[UIImageView alloc] init];
        imgView.backgroundColor = [UIColor greenColor];
        [self.view addSubview:imgView];
        _imgView = imgView;
    }
    return _imgView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor grayColor];

    CGFloat imgX = 50;
    CGFloat imgY = 200;
    CGFloat imgW = 300;
    CGFloat imgH = 300;
    self.imgView.frame = CGRectMake(imgX, imgY, imgW, imgH);
    
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
    btn.frame = CGRectMake(40, 550, 200, 50);
    [btn setTitle:@"选照片" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:15];
    
    [btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark - 从系统相册中选择 选取
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    _imgView.image = image;
    [picker dismissViewControllerAnimated:YES completion:nil];
    
    UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, YES, 2.2);
    CGContextRef context = UIGraphicsGetCurrentContext();
    [self.view.layer renderInContext:context];
    _imgView.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    NSString *sss = [self stringValueFrom:_imgView.image];
    NSLog(@"结果是666:%@",sss);
}
#pragma mark - 从系统相册中选择 取消
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    [picker dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark -//系统识别相册二维码
-(NSString *)stringValueFrom:(UIImage *)image{
    ///系统识别二维码
    CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeQRCode context:nil options:@{CIDetectorAccuracy: CIDetectorAccuracyHigh}];
    // 取得识别结果
    NSArray *features = [detector featuresInImage:[CIImage imageWithCGImage:image.CGImage]];
    NSString *resultStr;
    if (features.count == 0) {
        return @"请检查图片是不是二维码";
    }
    for (int index = 0; index < [features count]; index ++) {
        CIQRCodeFeature *feature = [features objectAtIndex:index];
        resultStr = feature.messageString;
    }
    return resultStr;
}
///////////////////////////////////////////////////////////////////////////////////

 

posted on 2021-12-07 17:06  高彰  阅读(177)  评论(0编辑  收藏  举报

导航