iOS 开发之 ZBarSDK 二维码扫描自定义二维码扫描页面(二)
iOS 开发之 ZBarSDK 二维码扫描自定义二维码扫描页面(二)
上一篇解决了ZBarSDK不支持64bit的问题,下面我们就可以使用ZBarSDK了。
导入ZBarSDk.h文件
附上代码:
//
// MeViewController.m
// Auditory Blog
//
// Created by 寒竹子 on 15/4/28.
// Copyright (c) 2015年 寒竹子. All rights reserved.
//
#define ScanWidth 220
#define ScanHeight 220
#import "MeViewController.h"
#import "ZBarSDK.h"
@interface MeViewController ()<ZBarReaderDelegate>
@property (nonatomic, strong) UIImageView * imageView;
@property (nonatomic, strong) UIView * scanLine; // 扫描仪
@property (nonatomic, strong) NSTimer * timer;
@end
@implementation MeViewController
/**
* @brief 初始化UI
*
* @param
* @return
*/
- (void)setupUI
{
self.view.backgroundColor = RGB(248, 248, 248, 1.0f);
UIButton * scanBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 200, self.view.frame.size.width, 30)];
[scanBtn setTitle:@"扫描二维码" forState:UIControlStateNormal];
scanBtn.titleLabel.font = TextFont(16.0f);
[scanBtn setTitleColor:RGB(10, 10, 10, 1.0f) forState:UIControlStateNormal];
[scanBtn addTarget:self action:@selector(scanAction) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:scanBtn];
_imageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 300, 200, 100)];
[self.view addSubview:_imageView];
}
/**
* @brief 起一个定时器
*
* @param
* @return
*/
- (void)startTimer
{
_timer = [NSTimer scheduledTimerWithTimeInterval:.02f target:self selector:@selector(updateScanLine) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
}
/**
* @brief 停止定时器
*
* @param
* @return
*/
- (void)stopTimer
{
if ([_timer isValid]) {
[_timer invalidate];
_timer = nil;
}
}
/**
* @brief 扫描二维码
*
* @param
* @return
*/
- (void)scanAction
{
ZBarReaderViewController * readerVc = [[ZBarReaderViewController alloc] init];
readerVc.readerDelegate = self;
// 非全屏
readerVc.wantsFullScreenLayout = NO;
// 隐藏底部控制按钮
readerVc.showsZBarControls = NO;
// 设置自己定义的界面
[self setOverlayPickerView:readerVc];
ZBarImageScanner * scanner = readerVc.scanner;
[scanner setSymbology:ZBAR_I25 config:ZBAR_CFG_ENABLE to:0];
[self presentViewController:readerVc animated:YES completion:nil];
[self startTimer];
}
- (void)setOverlayPickerView:(ZBarReaderViewController *)readerVc
{
// 清除原有的控件
for (UIView * temp in [readerVc.view subviews]) {
for (UIButton * btn in [temp subviews]) {
if ([btn isKindOfClass:[UIButton class]]) {
[btn removeFromSuperview];
}
}
for (UIToolbar * toolBar in [temp subviews]) {
if ([toolBar isKindOfClass:[UIToolbar class]]) {
[toolBar setHidden:YES];
[toolBar removeFromSuperview];
}
}
}
// 画中间的基准线
_scanLine = [[UIView alloc] initWithFrame:CGRectMake((self.view.frame.size.width - ScanWidth) / 2.0f, (self.view.frame.size.height - ScanHeight) / 2.0f, ScanWidth, 1)];
_scanLine.backgroundColor = [UIColor redColor];
[readerVc.view addSubview:_scanLine];
// 最上部的View
UIView * upView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, (self.view.frame.size.height - ScanHeight) / 2.0f)];
upView.alpha = .4f;
upView.backgroundColor = [UIColor blackColor];
[readerVc.view addSubview:upView];
// 用于说明的label
UILabel * infoLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, self.view.frame.size.width - 40, 60)];
infoLabel.backgroundColor = [UIColor clearColor];
infoLabel.numberOfLines = 2;
infoLabel.font = TextFont(15.0f);
infoLabel.textColor = [UIColor whiteColor];
infoLabel.text = @"将二维码置于矩形方框内,离手机摄像头10CM左右,系统会自动识别。";
[upView addSubview:infoLabel];
// 左侧的view
UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(upView.frame), (self.view.frame.size.width - ScanWidth) / 2.0f, ScanHeight)];
leftView.alpha = 0.4;
leftView.backgroundColor = [UIColor blackColor];
[readerVc.view addSubview:leftView];
// 右侧的view
UIView *rightView = [[UIView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(leftView.frame) + ScanWidth, leftView.frame.origin.y, (self.view.frame.size.width - ScanWidth) / 2.0f, ScanHeight)];
rightView.alpha = 0.4;
rightView.backgroundColor = [UIColor blackColor];
[readerVc.view addSubview:rightView];
//底部view
UIView * downView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(leftView.frame), self.view.frame.size.width, self.view.frame.size.height - CGRectGetMaxY(leftView.frame))];
downView.alpha = 0.4;
downView.backgroundColor = [UIColor blackColor];
[readerVc.view addSubview:downView];
// 取消操作的按钮
UIButton * cancelBtn = [[UIButton alloc] initWithFrame:CGRectMake((self.view.frame.size.width - 55) / 2.0f, 30, 70, 70.0f)];
[cancelBtn setImage:[UIImage imageNamed:@"close"] forState:UIControlStateNormal];
[cancelBtn addTarget:self action:@selector(closeAction) forControlEvents:UIControlEventTouchUpInside];
[downView addSubview:cancelBtn];
}
/**
* @brief 更新扫描
*
* @param
* @return
*/
- (void)updateScanLine
{
[UIView animateWithDuration:.01f animations:^{
CGFloat currentY = _scanLine.frame.origin.y;
if (currentY >= (self.view.frame.size.height - ScanHeight) / 2.0f + ScanHeight) {
currentY = (self.view.frame.size.height - ScanHeight) / 2.0f;
}else {
currentY += 1.0f;
}
CGRect frame = _scanLine.frame;
frame.origin.y = currentY;
_scanLine.frame = frame;
} completion:nil];
}
/**
* @brief 关闭扫描页面
*
* @param
* @return
*/
- (void)closeAction
{
[self stopTimer];
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)viewDidLoad {
[super viewDidLoad];
[self setupUI];
}
#pragma mark ZBarSDKDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
id<NSFastEnumeration> results = [info objectForKey:ZBarReaderControllerResults];
ZBarSymbol * symbol;
for(symbol in results)
break;
_imageView.image = [info objectForKey:UIImagePickerControllerOriginalImage];
[self stopTimer];
[picker dismissViewControllerAnimated:YES completion:nil];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:symbol.data]];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end
至此ZBarSDK已经讲解完毕。
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。