iOS使用Zbar扫描二维码
二维码扫描
iOS
Zbar64位
正文:
首先下载一个支持64位系统的ZbarSDK的包,保存在了我的云盘里,地址:ZbarSDK
- 把文件拖到工程里面
- 然后添加依赖库:AVFoundation.framework、libiconv.2.dylib、CoreMedia.framework、CoreVideo.framework,其中libzbar.a是SDK中的,(如果需要通过SVN上传自己代码时,要记得还有这个.a文件)
-
在你要进行添加扫描的界面的.h里 可直接复制到工程里
添加头文件
- #import "ZBarSDK.h"
- //设置代理
- @protocol ErweimaDelegate <NSObject>
- //扫描出结果则调用这个方法
- -(void)finishRead:(NSString *)barCode;
- @end
-
- @interface ErweimaViewController : superVC < ZBarReaderViewDelegate > //此处superVC是自定义的父类
- @property (strong, nonatomic)id < ErweimaDelegate >delegate;
- @end
4.在.m文件中:
- @interface ErweimaViewController ()
- {
- ZBarReaderView *rederView;//二维码扫描View,可自定义样式
- UIImageView *lineView;//自定义的扫描线
- NSTimer *time;//定时器,控制扫描线跑动
- }
- //在进入界面时就开始扫描
- -(void)viewWillAppear:(BOOL)animated{
-
- [super viewWillAppear:animated];
- [rederView start];
- }
- //退出界面时就停止扫描
- - (void)viewWillDisappear:(BOOL)animated
- {
- [super viewWillDisappear:animated];
- [rederView stop];
- }
- -(void)viewDidLoad {
- [super viewDidLoad];
- // Do any additional setup after loading the view.
- self.title = @"扫描";
-
- [self initView];
-
- }
-
- - (void)initView
- {
- rederView = [[ZBarReaderView alloc] init];
- rederView.frame = CGRectMake(0, 0,self.view.frame.size.width,self.view.frame.size.height);
- rederView.readerDelegate = self;
- rederView.allowsPinchZoom = NO; // 不使用Pinch手势变焦
- rederView.torchMode = 0;//闪光灯 关
- rederView.trackingColor = [UIColor clearColor];//锁定图码 显示的颜色框
-
-
- if (TARGET_IPHONE_SIMULATOR) {
- ZBarCameraSimulator *camera = [[ZBarCameraSimulator alloc] initWithViewController:self];
- camera.readerView = rederView;
- }
- [self.view addSubview:rederView];
- /*************可忽略此域中的以下代码,可以自己自定义***************/
-
- //扫描区域计算
- CGRect scanMaskRect = CGRectMake(Width_Screen/2-75, 150, 150, 150);
- CGRect scanCrop=[self getScanCrop:scanMaskRect readerViewBounds:rederView.bounds];
- rederView.scanCrop = scanCrop;
- [rederView.captureReader captureFrame];
- [rederView start];
-
-
- UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(-20, -64, Width_Screen+40, Height_Screen)];
- image.image = [UIImage imageNamed:@"111111111"];
- image.backgroundColor = [UIColor clearColor];
- image.alpha = 0.3;
- [self.view addSubview:image];
-
- //这里添加了类似微信的扫描线,并开始上线滑动动画
- lineView=[[UIImageView alloc] initWithFrame:CGRectMake(51, 200, 219, 3)];
- // lineView.image=[UIImage imageNamed:@"scan_line"];
- lineView.backgroundColor = UIColor_Theme;
- [self.view addSubview:lineView];
- [self loadAnimationStart];
- }
-
- -(CGRect)getScanCrop:(CGRect)rect readerViewBounds:(CGRect)rvBounds{
- CGFloat x,y,width,height;
- x = rect.origin.y / rvBounds.size.height;
- y = 1 - (rect.origin.x + rect.size.width) / rvBounds.size.width;
- width = (rect.origin.y + rect.size.height) / rvBounds.size.height;
- height = 1 - rect.origin.x / rvBounds.size.width;
- return CGRectMake(x, y, width, height);
-
- }
-
- -(void)loadAnimationStart{
-
- if(Width_Screen != 320){
-
- [UIView animateWithDuration:2.0 animations:^{
- lineView.frame=CGRectMake(lineView.frame.origin.x, 390,lineView.frame.size.width,lineView.frame.size.height);
- } completion:^(BOOL finished) {
-
- time = [NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(secStart) userInfo:nil repeats:NO];
- }];
- }else{
-
- [UIView animateWithDuration:2.0 animations:^{
- lineView.frame=CGRectMake(lineView.frame.origin.x, 345,lineView.frame.size.width,lineView.frame.size.height);
- } completion:^(BOOL finished) {
-
- time = [NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(secStart) userInfo:nil repeats:NO];
- }];
-
- }
-
- }
- -(void)secStart{
-
- if(Width_Screen != 320){
- lineView.frame=CGRectMake(lineView.frame.origin.x, 175,lineView.frame.size.width,lineView.frame.size.height);
- if (time != nil) {
- [self loadAnimationStart];
- }
-
- }else{
- lineView.frame=CGRectMake(lineView.frame.origin.x, 130,lineView.frame.size.width,lineView.frame.size.height);
- if (time != nil) {
- [self loadAnimationStart];
- NSLog(@"123");
- }
- NSLog(@"qwe");
- }
-
- }
-
- -(void)readerView:(ZBarReaderView *)readerView didReadSymbols:(ZBarSymbolSet *)symbols fromImage:(UIImage *)image
- {
- NSString *str;//扫描出的字段,记录下来
-
- for (ZBarSymbol *symbol in symbols) {
- NSLog(@"%@", symbol.data);
- str=symbol.data;
- [rederView stop];
- [self.navigationController popViewControllerAnimated:YES];
- [self showHudMessageAndHidden:str];//展示出扫描结果,根据需求可自定义展示样式
- lineView.hidden = YES;
- [time setFireDate:[NSDate distantFuture]];
- [time invalidate];
- time = nil;
- break;
- }
-
-
- //这个代理是 返回到上个界面把str传过去的,根据需求可改变
- if([_delegate respondsToSelector:@selector(finishRead:)]){
-
- [_delegate finishRead:str];
- }
-
-
- }
结束,搞定。预祝成功。
<div class="remark-icons">
</div>