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

 

 

 

 

 

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#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   高彰  阅读(184)  评论(0编辑  收藏  举报

编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
历史上的今天:
2017-12-07 iOS pch文件配置
2017-12-07 iOS 背景图片设置
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示