做项目要用到二维码扫描,在git上搜索到了LBXScan开源库很不错,详细的可以下载demo,我只是使用了部分功能因此pod中只导入了
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
#import <Foundation/Foundation.h>
#import "LBXScanViewStyle.h"
@interface ScanStyleDIY : NSObject
#pragma mark -无边框,内嵌4个角
+ (LBXScanViewStyle*)InnerStyle;
@end
ScanStyleDIY.m
#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
#import <UIKit/UIKit.h>
#import "LBXScanViewController.h"
@interface ScanViewController : LBXScanViewController
@end
ScanViewController.m
#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