標籤:
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已經講解完畢。
iOS 開發之 ZBarSDK 二維碼掃描自訂二維碼掃描頁面(二)