前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ios 一款集成方便的二维码扫描

ios 一款集成方便的二维码扫描

作者头像
赵哥窟
发布2018-09-13 11:43:16
1.7K0
发布2018-09-13 11:43:16
举报
文章被收录于专栏:日常技术分享日常技术分享

做项目要用到二维码扫描,在git上搜索到了LBXScan开源库很不错,详细的可以下载demo,我只是使用了部分功能因此pod中只导入了

代码语言:javascript
复制
platform:ios,'9.0'
use_frameworks!
target 'ScanDemo' do
    pod 'LBXScan/LBXZXing'
    pod 'LBXScan/UI'
end

WechatIMG8_gaitubao_com_180x389.jpeg

使用了 内四角的UI 所以我自定义了一个ScanStyleDIY类

ScanStyleDIY.h

代码语言:javascript
复制
#import <Foundation/Foundation.h>
#import "LBXScanViewStyle.h"

@interface ScanStyleDIY : NSObject

#pragma mark -无边框,内嵌4个角
+ (LBXScanViewStyle*)InnerStyle;

@end

ScanStyleDIY.m

代码语言:javascript
复制
#import "ScanStyleDIY.h"

@implementation ScanStyleDIY

#pragma mark -无边框,内嵌4个角
+ (LBXScanViewStyle*)InnerStyle
{
    //设置扫码区域参数
    LBXScanViewStyle *style = [[LBXScanViewStyle alloc]init];
    style.centerUpOffset = 44;
    style.photoframeAngleStyle = LBXScanViewPhotoframeAngleStyle_Inner;
    style.photoframeLineW = 3;
    style.photoframeAngleW = 18;
    style.photoframeAngleH = 18;
    // 修改四角的颜色
    style.colorAngle = [UIColor blueColor];
    style.isNeedShowRetangle = NO;
    style.anmiationStyle = LBXScanViewAnimationStyle_LineMove;
    //qq里面的线条图片
    UIImage *imgLine = [UIImage imageNamed:@"scan_light_green"];
    style.animationImage = imgLine;
    
    style.notRecoginitonArea = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.6];
    
    
    return style;
}

然后定义一个ScanViewController 继承LBXScanViewController ScanViewController.h

代码语言:javascript
复制
#import <UIKit/UIKit.h>
#import "LBXScanViewController.h"

@interface ScanViewController : LBXScanViewController

@end

ScanViewController.m

代码语言:javascript
复制
#import "ScanViewController.h"
#import "ScanStyleDIY.h"

@interface ScanViewController ()

/**
 @brief  扫码区域上方提示文字
 */
@property (nonatomic, strong) UILabel *topTitle;

@end

@implementation ScanViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.title = @"ZXing 扫描";
    self.cameraInvokeMsg = @"相机启动中";
    self.style = [ScanStyleDIY InnerStyle];
    self.isOpenInterestRect = YES;
    self.libraryType = SLT_ZXing;
    self.scanCodeType = SCT_QRCode;

}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    [self drawTitle];
    [self.view bringSubviewToFront:_topTitle];
}

//绘制扫描区域
- (void)drawTitle
{
    if (!_topTitle)
    {
        self.topTitle = [[UILabel alloc]init];
        _topTitle.bounds = CGRectMake(0, 0, 200, 60);
        _topTitle.center = CGPointMake(CGRectGetWidth(self.view.frame)/2, self.view.frame.size.height/2+140);
        _topTitle.font = [UIFont systemFontOfSize:12];
    
        _topTitle.textAlignment = NSTextAlignmentCenter;
        _topTitle.numberOfLines = 0;
        _topTitle.text = @"将二维码/条码放入框内,即可自动扫描";
        _topTitle.textColor = [UIColor whiteColor];
        [self.view addSubview:_topTitle];
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark -实现类继承该方法,作出对应处理
- (void)scanResultWithArray:(NSArray<LBXScanResult*>*)array
{
    if (!array ||  array.count < 1)
    {
        [self popAlertMsgWithScanResult:nil];
        return;
    }

    LBXScanResult *scanResult = array[0];
    NSString*strResult = scanResult.strScanned;
    self.scanImage = scanResult.imgScanned;
    
    if (!strResult) {
        
        [self popAlertMsgWithScanResult:nil];
        
        return;
    }
    
    //TODO: 这里可以根据需要自行添加震动或播放声音提示相关代码
    //...
    
    [self showNextVCWithScanResult:scanResult];
}

- (void)popAlertMsgWithScanResult:(NSString*)strResult
{
    if (!strResult) {
        
        strResult = @"识别失败";
    }
    
}

- (void)showNextVCWithScanResult:(LBXScanResult*)strResult
{
    NSLog(@"扫描结果:%@", strResult.strScanned);
}

这样就可以使用了集成还是很方便的。 demo:https://github.com/destinyzhao/QRScanDemo

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018.09.04 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档