|
|
//
|
|
|
// YH_AVScanViewController.m
|
|
|
// YH_Mall
|
|
|
//
|
|
|
// Created by XingYaXin on 14-9-18.
|
|
|
// Copyright (c) 2014年 YOHO. All rights reserved.
|
|
|
//
|
|
|
|
|
|
#import "YH_AVScanViewController.h"
|
|
|
#import "YH_ScanUtility.h"
|
|
|
#import "UIAlertView+BBlock.h"
|
|
|
|
|
|
|
|
|
#define is3_5Inches ([[UIScreen mainScreen] bounds].size.height == 480)
|
|
|
|
|
|
#define IsNilOrNull(_ref) (((_ref) == nil) || ([(_ref) isEqual:[NSNull null]]))
|
|
|
|
|
|
#define IsDictionaryClass(_ref) (!IsNilOrNull(_ref) && ([(_ref) isKindOfClass:[NSDictionary class]]))
|
|
|
|
|
|
#define IsArrayClass(_ref) (!IsNilOrNull(_ref) && ([(_ref) isKindOfClass:[NSArray class]]))
|
|
|
|
|
|
#define IsStrEmpty(_ref) (IsNilOrNull(_ref) || (![(_ref) isKindOfClass:[NSString class]]) || ([(_ref) isEqualToString:@""]))
|
|
|
|
|
|
|
|
|
#define kScanWindowTop (is3_5Inches ? 80.0f : 95.0f)
|
|
|
#define kScanWindowWidth (220.f)
|
|
|
#define kScanWindowLeft (50.f)
|
|
|
#define kScanWindowLineStep (2)
|
|
|
#define kScanToolbarHeight (44.f)
|
|
|
#define KSknString @"skn="
|
|
|
#define kBrandIdString @"brand_id="
|
|
|
#define kPCLoginCodeString @"qr="
|
|
|
|
|
|
////////////////////////
|
|
|
|
|
|
static CGFloat const kIntroductionOriginX = 75.0f; //introudction数据
|
|
|
static CGFloat const kIntroductionOriginY = 350.0f;
|
|
|
static CGFloat const kIntroductionWidth = 170.0f;
|
|
|
static CGFloat const kIntroductionHeight = 20.0f;
|
|
|
|
|
|
static CGFloat const kScanLineOriginX = 50.0f;
|
|
|
static CGFloat const kScanLineWidth = 220.0f;
|
|
|
static CGFloat const kScanLineHeight = 2.0f;
|
|
|
|
|
|
static CGFloat const ktoolBarViewHeight = 44.0f; //toolBarView高度
|
|
|
|
|
|
static CGFloat const kCloseZBarOriginX = 10.0f; //closeZBar关闭按钮X值
|
|
|
|
|
|
@interface YH_AVScanViewController ()
|
|
|
{
|
|
|
NSInteger numberCount;
|
|
|
BOOL upOrdown;
|
|
|
NSTimer * timerHandle;
|
|
|
}
|
|
|
|
|
|
@property (strong, nonatomic) UIImageView *scanLine;
|
|
|
@property (strong, nonatomic) UIView *toolBarView;
|
|
|
@property (strong, nonatomic) UILabel *introudction;
|
|
|
@property (strong, nonatomic) UIImageView *maskImageView;
|
|
|
@property (copy, nonatomic) NSString *scanInfoString; //扫描信息全局变量
|
|
|
|
|
|
@property (strong, nonatomic) YH_ScanUtility *scanUtility;
|
|
|
|
|
|
@property (assign, nonatomic) dispatch_once_t onceToken;
|
|
|
|
|
|
@property (assign, nonatomic) NSInteger kNumberCount; //扫描计数器
|
|
|
|
|
|
@end
|
|
|
|
|
|
@implementation YH_AVScanViewController
|
|
|
|
|
|
- (YH_ScanUtility *)scanUtility
|
|
|
{
|
|
|
if (!_scanUtility) {
|
|
|
_scanUtility = [[YH_ScanUtility alloc] init];
|
|
|
}
|
|
|
return _scanUtility;
|
|
|
}
|
|
|
|
|
|
- (id)init{
|
|
|
|
|
|
self = [super init];
|
|
|
if (self) {
|
|
|
self.scanType = YHScanType_Express;
|
|
|
}
|
|
|
return self;
|
|
|
}
|
|
|
|
|
|
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
|
|
|
|
|
|
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
|
|
|
if (self) {
|
|
|
}
|
|
|
return self;
|
|
|
}
|
|
|
|
|
|
|
|
|
- (void)didReceiveMemoryWarning{
|
|
|
|
|
|
[super didReceiveMemoryWarning];
|
|
|
}
|
|
|
|
|
|
|
|
|
- (void)viewDidLoad{
|
|
|
|
|
|
[super viewDidLoad];
|
|
|
|
|
|
self.view.backgroundColor = [UIColor blackColor];
|
|
|
|
|
|
upOrdown = NO;
|
|
|
numberCount = 0;
|
|
|
self.kNumberCount = is3_5Inches ? 170 : 200;
|
|
|
|
|
|
__weak typeof(self) weakSelf = self;
|
|
|
[self.scanUtility addScanCallBackHandle:^(NSString *info, NSError *error) {
|
|
|
[weakSelf dispatchQRByScanString:info];
|
|
|
}];
|
|
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setupCamera) name:UIApplicationWillEnterForegroundNotification object:nil];
|
|
|
|
|
|
}
|
|
|
|
|
|
- (void)dealloc
|
|
|
{
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
|
|
}
|
|
|
|
|
|
- (void)viewDidAppear:(BOOL)animated{
|
|
|
|
|
|
[super viewDidAppear:animated];
|
|
|
|
|
|
[self setupCamera];
|
|
|
|
|
|
}
|
|
|
|
|
|
- (void)addCustomeViews:(UIView *)containView
|
|
|
{
|
|
|
[self addScanMask:containView];
|
|
|
[self addHelpInfo:containView];
|
|
|
[self addToolBar:containView];
|
|
|
}
|
|
|
|
|
|
- (void)addHelpInfo:(UIView *)currentView{
|
|
|
if (self.scanType == YHScanType_Express) {
|
|
|
self.introudction.text = @"尽量让二维码/条形码充满框内";
|
|
|
self.introudction.frame = CGRectMake(kIntroductionOriginX, kIntroductionOriginY, kIntroductionWidth+30, kIntroductionHeight);
|
|
|
self.introudction.center = CGPointMake(self.view.center.x, kIntroductionOriginY * kScreenPointScale);
|
|
|
}
|
|
|
|
|
|
[currentView addSubview:self.introudction];
|
|
|
[currentView addSubview:self.scanLine];
|
|
|
|
|
|
if (!timerHandle) {
|
|
|
timerHandle = [NSTimer scheduledTimerWithTimeInterval:0.02 target:self selector:@selector(scanAnimation) userInfo:nil repeats:YES];
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
- (void)addToolBar:(UIView *)currentView{
|
|
|
|
|
|
[currentView addSubview:self.toolBarView];
|
|
|
}
|
|
|
|
|
|
- (void)addScanMask:(UIView *)currentView{
|
|
|
|
|
|
[currentView addSubview:self.maskImageView];
|
|
|
}
|
|
|
|
|
|
- (void)scanAnimation{
|
|
|
|
|
|
if (upOrdown == NO) {
|
|
|
numberCount ++;
|
|
|
self.scanLine.frame = CGRectMake( kScanWindowLeft, kScanWindowTop + kScanWindowLineStep * numberCount, kScanWindowWidth, 2);
|
|
|
self.scanLine.center = CGPointMake(self.view.center.x, (kScanWindowTop + kScanWindowLineStep * numberCount) * kScreenPointScale);
|
|
|
if (kScanWindowLineStep * numberCount == self.kNumberCount) {
|
|
|
upOrdown = YES;
|
|
|
}
|
|
|
}
|
|
|
else {
|
|
|
numberCount --;
|
|
|
self.scanLine.frame = CGRectMake( kScanWindowLeft, kScanWindowTop + kScanWindowLineStep * numberCount, kScanWindowWidth, 2);
|
|
|
self.scanLine.center = CGPointMake(self.view.center.x, (kScanWindowTop + kScanWindowLineStep * numberCount) * kScreenPointScale);
|
|
|
if (numberCount == 0) {
|
|
|
upOrdown = NO;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- (void)backAction:(id)sender
|
|
|
{
|
|
|
if ([timerHandle isValid]) {
|
|
|
[timerHandle invalidate];
|
|
|
}
|
|
|
|
|
|
[self dismissViewControllerAnimated:YES completion:nil];
|
|
|
}
|
|
|
|
|
|
|
|
|
- (void)setupCamera
|
|
|
{
|
|
|
[self.scanUtility resetCamera];
|
|
|
[self.view.layer addSublayer:self.scanUtility.previewLayer];
|
|
|
[self addCustomeViews:self.view];
|
|
|
}
|
|
|
|
|
|
|
|
|
- (void)dispatchQRByScanString:(NSString *)scanQRString{
|
|
|
|
|
|
switch (self.scanType) {
|
|
|
case YHScanType_Express:{
|
|
|
[self scanExpress:scanQRString];
|
|
|
return;
|
|
|
}
|
|
|
break;
|
|
|
|
|
|
default:
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- (BOOL)scanExpress:(NSString *)expressString
|
|
|
{
|
|
|
if (IsStrEmpty(expressString)) {
|
|
|
[self showAlertAndReinit:[NSString stringWithFormat:@"快递单号无法识别:%@",expressString]];
|
|
|
return NO;
|
|
|
}
|
|
|
[timerHandle invalidate];
|
|
|
[self dismissViewControllerAnimated:YES completion:^(void){
|
|
|
if (self.resultCallBack) {
|
|
|
self.resultCallBack(expressString);
|
|
|
}
|
|
|
}];
|
|
|
return YES;
|
|
|
}
|
|
|
|
|
|
|
|
|
- (void)showAlertAndReinit:(NSString *)alertString{
|
|
|
__weak typeof(self) weakSelf = self;
|
|
|
UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"二维码扫描"
|
|
|
message:alertString
|
|
|
cancelButtonTitle:@"确定"
|
|
|
otherButtonTitle:nil
|
|
|
completionBlock:^(NSInteger buttonIndex, UIAlertView *alertView){
|
|
|
[weakSelf setupCamera];
|
|
|
}];
|
|
|
|
|
|
[av show];
|
|
|
}
|
|
|
|
|
|
|
|
|
#pragma mark - init
|
|
|
|
|
|
- (UIImageView *)scanLine
|
|
|
{
|
|
|
if (IsNilOrNull(_scanLine)) {
|
|
|
_scanLine = [[UIImageView alloc] initWithFrame:CGRectMake(kScanLineOriginX, kScanWindowTop, kScanLineWidth, kScanLineHeight)];
|
|
|
_scanLine.center = CGPointMake(self.view.center.x, kScanWindowTop * kScreenPointScale);
|
|
|
_scanLine.image = [UIImage imageNamed:@"cameraQRLine.png"];
|
|
|
}
|
|
|
return _scanLine;
|
|
|
}
|
|
|
|
|
|
- (UILabel *)introudction{
|
|
|
|
|
|
if (IsNilOrNull(_introudction)) {
|
|
|
_introudction = [[UILabel alloc] initWithFrame:CGRectMake(kIntroductionOriginX, kIntroductionOriginY, kIntroductionWidth, kIntroductionHeight)];
|
|
|
_introudction.center = CGPointMake(self.view.center.x, kIntroductionOriginY * kScreenPointScale);
|
|
|
_introudction.backgroundColor = [UIColor grayColor];
|
|
|
_introudction.layer.cornerRadius = 10.0f;
|
|
|
_introudction.clipsToBounds = YES;
|
|
|
_introudction.numberOfLines = 1;
|
|
|
_introudction.textColor = [UIColor blackColor];
|
|
|
_introudction.font = [UIFont systemFontOfSize:14.0f];
|
|
|
_introudction.textAlignment = NSTextAlignmentCenter;
|
|
|
_introudction.text = @"尽量让二维码/条形码充满框内";
|
|
|
}
|
|
|
|
|
|
return _introudction;
|
|
|
}
|
|
|
|
|
|
- (UIView *)toolBarView{
|
|
|
|
|
|
if (IsNilOrNull(_toolBarView)) {
|
|
|
_toolBarView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, [[UIScreen mainScreen] bounds].size.height - ktoolBarViewHeight, self.view.frame.size.width, ktoolBarViewHeight)];
|
|
|
UIButton *closeZBar = [UIButton buttonWithType:UIButtonTypeCustom];
|
|
|
[closeZBar setImage:[UIImage imageNamed:@"favoites_barButtonItemiconCancel_normal"] forState:UIControlStateNormal];
|
|
|
[closeZBar addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
|
|
|
closeZBar.frame = CGRectMake(kCloseZBarOriginX, 0.0f, ktoolBarViewHeight, ktoolBarViewHeight);
|
|
|
[_toolBarView addSubview:closeZBar];
|
|
|
_toolBarView.backgroundColor = [UIColor blackColor];
|
|
|
}
|
|
|
|
|
|
return _toolBarView;
|
|
|
}
|
|
|
|
|
|
- (UIImageView *)maskImageView{
|
|
|
|
|
|
if (IsNilOrNull(_maskImageView)) {
|
|
|
_maskImageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
|
|
|
[_maskImageView setImage:[UIImage imageNamed:@"cameraQR5"]];
|
|
|
}
|
|
|
|
|
|
return _maskImageView;
|
|
|
}
|
|
|
|
|
|
|
|
|
@end |
...
|
...
|
|