iOS-SDK开发

iOS 扫码 扫一扫以及从相册中识别 二维码和条形码

2023-01-05  本文已影响0人  木子李55
更具体的代码见文末demo地址

效果图

效果图.gif

功能介绍

  1. 一行代码生成二维码
  2. 一行代码进行二维码或条形码扫描
  3. 一行代码进行相册图片识别二维码或条形码

使用方式

可以通过pod导入,也可以下载demo后把LYQRCodeGenerateAndScan文件夹导入。

pod 'LYQRCodeGenerateAndScan', '~> 0.0.3'
// 二维码生成事件
UIImage *resultImg = [LYQRCodeGenerateManager generateNormalQRCodeWithDataString:@"测试" ImageWidth:200];

// 二维码、条形码扫描
[[LYQRCodeManager sharedManager] startScanWithController:self Delegate:self];

// 二维码、条形码相册识别
[[LYQRCodeManager sharedManager] startAlumScanWithController:self Delegate:self];

// 回调代理
-(void)ly_QRCodeScanResultText:(NSString *)resultText{
    NSLog(@"识别结果 == %@", resultText);
}
-(void)ly_QRCodeScanResultFail{
    NSLog(@"识别失败");
}

注意

如果pod repo update后还pod search不到,可以运行如下命令清下缓存后应该就可以了。
rm ~/Library/Caches/CocoaPods/search_index.json

具体demo代码

在使用的控制器中,
    
遵守代理<LYQRCodeManagerDelegate>

@property (nonatomic, strong) UIButton *qrCodeGenerateBtn;
@property (nonatomic, strong) UIButton *qrCodeScanBtn;
@property (nonatomic, strong) UIButton *qrCodeAlumScanBtn;

@property (nonatomic, strong) UIImageView *resultImgView;

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor yellowColor];
    
    [self creatUI];
    
}

-(void)creatUI{
    
    // 二维码生成
    [self.view addSubview:self.qrCodeGenerateBtn];
    
    // 二维码、条形码扫描
    [self.view addSubview:self.qrCodeScanBtn];
    
    // 二维码、条形码相册识别
    [self.view addSubview:self.qrCodeAlumScanBtn];
    
    // 二维码生成图片预览
    [self.view addSubview:self.resultImgView];
    
}

#pragma mark - 二维码生成事件
-(void)qrCodeGenerateBtnAction{
    
    UIImage *resultImg = [LYQRCodeGenerateManager generateNormalQRCodeWithDataString:@"测试" ImageWidth:200];
    self.resultImgView.image = resultImg;
    
}
#pragma mark - 二维码扫描事件
-(void)qrCodeScanBtnAction{
    
    [[LYQRCodeManager sharedManager] startScanWithController:self Delegate:self];
    
}
#pragma mark - 相册扫描事件
-(void)qrCodeAlumScanBtnAction{
    
    [[LYQRCodeManager sharedManager] startAlumScanWithController:self Delegate:self];
    
}
/// 扫码识别结果
-(void)ly_QRCodeScanResultText:(NSString *)resultText{
    NSLog(@"识别结果 == %@", resultText);
    [[[UIAlertView alloc] initWithTitle:@"识别结果" message:resultText delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil] show];
}
-(void)ly_QRCodeScanResultFail{
    NSLog(@"识别失败");
    [[[UIAlertView alloc] initWithTitle:@"识别结果" message:@"失败" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil] show];
}



#pragma mark - 懒加载
-(UIButton *)qrCodeGenerateBtn{
    if (!_qrCodeGenerateBtn) {
        _qrCodeGenerateBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _qrCodeGenerateBtn.frame = CGRectMake(50, 100, self.view.frame.size.width-100, 50);
        _qrCodeGenerateBtn.backgroundColor = [UIColor whiteColor];
        [_qrCodeGenerateBtn setTitle:@"二维码生成" forState:UIControlStateNormal];
        [_qrCodeGenerateBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_qrCodeGenerateBtn addTarget:self action:@selector(qrCodeGenerateBtnAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _qrCodeGenerateBtn;
}
-(UIButton *)qrCodeScanBtn{
    if (!_qrCodeScanBtn) {
        _qrCodeScanBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _qrCodeScanBtn.frame = CGRectMake(50, CGRectGetMaxY(_qrCodeGenerateBtn.frame)+50, self.view.frame.size.width-100, 50);
        _qrCodeScanBtn.backgroundColor = [UIColor whiteColor];
        [_qrCodeScanBtn setTitle:@"二维码扫描" forState:UIControlStateNormal];
        [_qrCodeScanBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_qrCodeScanBtn addTarget:self action:@selector(qrCodeScanBtnAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _qrCodeScanBtn;
}
-(UIButton *)qrCodeAlumScanBtn{
    if (!_qrCodeAlumScanBtn) {
        _qrCodeAlumScanBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _qrCodeAlumScanBtn.frame = CGRectMake(50, CGRectGetMaxY(_qrCodeScanBtn.frame)+50, self.view.frame.size.width-100, 50);
        _qrCodeAlumScanBtn.backgroundColor = [UIColor whiteColor];
        [_qrCodeAlumScanBtn setTitle:@"相册识别" forState:UIControlStateNormal];
        [_qrCodeAlumScanBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_qrCodeAlumScanBtn addTarget:self action:@selector(qrCodeAlumScanBtnAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _qrCodeAlumScanBtn;
}
-(UIImageView *)resultImgView{
    if (!_resultImgView) {
        _resultImgView = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.frame.size.width/4, CGRectGetMaxY(_qrCodeAlumScanBtn.frame)+50, self.view.frame.size.width/2, self.view.frame.size.width/2)];
    }
    return _resultImgView;
}

demo下载https://github.com/zhuzi55/LYQRCodeGenerateAndScan.git

如对您有帮助,还请star一下。
上一篇下一篇

猜你喜欢

热点阅读