iOS 掃碼 掃一掃以及從相冊中識別 二維碼和條形碼

更具體的代碼見文末demo地址


效果圖

效果圖.gif

功能介紹

  1. 一行代碼生成二維碼
  2. 一行代碼進(jìn)行二維碼或條形碼掃描
  3. 一行代碼進(jìn)行相冊圖片識別二維碼或條形碼

使用方式

可以通過pod導(dǎo)入,也可以下載demo后把LYQRCodeGenerateAndScan文件夾導(dǎo)入。

pod 'LYQRCodeGenerateAndScan', '~> 0.0.3'
// 二維碼生成事件
UIImage *resultImg = [LYQRCodeGenerateManager generateNormalQRCodeWithDataString:@"測試" ImageWidth:200];

// 二維碼、條形碼掃描
[[LYQRCodeManager sharedManager] startScanWithController:self Delegate:self];

// 二維碼、條形碼相冊識別
[[LYQRCodeManager sharedManager] startAlumScanWithController:self Delegate:self];

// 回調(diào)代理
-(void)ly_QRCodeScanResultText:(NSString *)resultText{
    NSLog(@"識別結(jié)果 == %@", resultText);
}
-(void)ly_QRCodeScanResultFail{
    NSLog(@"識別失敗");
}

注意

如果pod repo update后還pod search不到,可以運(yùn)行如下命令清下緩存后應(yīng)該就可以了。
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];
    
    // 二維碼生成圖片預(yù)覽
    [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];
    
}
/// 掃碼識別結(jié)果
-(void)ly_QRCodeScanResultText:(NSString *)resultText{
    NSLog(@"識別結(jié)果 == %@", resultText);
    [[[UIAlertView alloc] initWithTitle:@"識別結(jié)果" message:resultText delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil] show];
}
-(void)ly_QRCodeScanResultFail{
    NSLog(@"識別失敗");
    [[[UIAlertView alloc] initWithTitle:@"識別結(jié)果" 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一下。
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

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

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