Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
O
Opple-iOS
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
张杰
Opple-iOS
Commits
4be36868
Commit
4be36868
authored
May 12, 2016
by
勾芒
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
caoyunxiao
parent
0889cec3
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
22 changed files
with
1 addition
and
1025 deletions
+1
-1025
LoginViewController.m
Lighting/Class/Login/LoginViewController.m
+0
-3
ProductLibraryViewController.m
Lighting/Class/ProductLibraryViewController.m
+1
-18
CustomTabbarController.m
Lighting/Class/Tabbar/CustomTabbarController.m
+0
-1
project.pbxproj
Lighting/Lighting.xcodeproj/project.pbxproj
+0
-48
Info.plist
Lighting/Lighting/Info.plist
+0
-1
HMScanerCardViewController.h
Lighting/Tools/QRCode/HMScanerCardViewController.h
+0
-15
HMScanerCardViewController.m
Lighting/Tools/QRCode/HMScanerCardViewController.m
+0
-62
QRCodeScanLine@2x.png
Lighting/Tools/QRCode/HMScanner.bundle/QRCodeScanLine@2x.png
+0
-0
ScanQR1@2x.png
Lighting/Tools/QRCode/HMScanner.bundle/ScanQR1@2x.png
+0
-0
ScanQR2@2x.png
Lighting/Tools/QRCode/HMScanner.bundle/ScanQR2@2x.png
+0
-0
ScanQR3@2x.png
Lighting/Tools/QRCode/HMScanner.bundle/ScanQR3@2x.png
+0
-0
ScanQR4@2x.png
Lighting/Tools/QRCode/HMScanner.bundle/ScanQR4@2x.png
+0
-0
HMScanner.h
Lighting/Tools/QRCode/HMScanner.h
+0
-49
HMScanner.m
Lighting/Tools/QRCode/HMScanner.m
+0
-312
HMScannerBorder.h
Lighting/Tools/QRCode/HMScannerBorder.h
+0
-19
HMScannerBorder.m
Lighting/Tools/QRCode/HMScannerBorder.m
+0
-96
HMScannerController.h
Lighting/Tools/QRCode/HMScannerController.h
+0
-65
HMScannerController.m
Lighting/Tools/QRCode/HMScannerController.m
+0
-50
HMScannerMaskView.h
Lighting/Tools/QRCode/HMScannerMaskView.h
+0
-25
HMScannerMaskView.m
Lighting/Tools/QRCode/HMScannerMaskView.m
+0
-42
HMScannerViewController.h
Lighting/Tools/QRCode/HMScannerViewController.h
+0
-23
HMScannerViewController.m
Lighting/Tools/QRCode/HMScannerViewController.m
+0
-196
No files found.
Lighting/Class/Login/LoginViewController.m
View file @
4be36868
...
@@ -12,9 +12,6 @@
...
@@ -12,9 +12,6 @@
#import "authenticateView.h"
#import "authenticateView.h"
#import "FollowHeartViewController.h"
#import "FollowHeartViewController.h"
#import "HMScannerController.h"
@interface
LoginViewController
()
<
RightVCselectedDelegate
>
@interface
LoginViewController
()
<
RightVCselectedDelegate
>
@property
(
nonatomic
,
strong
)
MMDrawerController
*
drawerController
;
@property
(
nonatomic
,
strong
)
MMDrawerController
*
drawerController
;
...
...
Lighting/Class/ProductLibraryViewController.m
View file @
4be36868
...
@@ -12,7 +12,6 @@
...
@@ -12,7 +12,6 @@
#import "ScreeningView.h"
#import "ScreeningView.h"
#import "screeningFirstView.h"
#import "screeningFirstView.h"
#import "screeningSecondView.h"
#import "screeningSecondView.h"
#import "HMScannerController.h"
@interface
ProductLibraryViewController
()
<
UICollectionViewDelegate
,
UICollectionViewDataSource
,
UIGestureRecognizerDelegate
,
ScreeningButtonClickdelegate
,
TableviewSelectedIndexdelegate
,
SelectedCollectionItemDelegate
>
@interface
ProductLibraryViewController
()
<
UICollectionViewDelegate
,
UICollectionViewDataSource
,
UIGestureRecognizerDelegate
,
ScreeningButtonClickdelegate
,
TableviewSelectedIndexdelegate
,
SelectedCollectionItemDelegate
>
...
@@ -344,22 +343,6 @@
...
@@ -344,22 +343,6 @@
#pragma mark -筛选
#pragma mark -筛选
-
(
void
)
ScreeningButtonClick
-
(
void
)
ScreeningButtonClick
{
{
NSString
*
cardName
=
@"天涯刀哥 - 傅红雪"
;
UIImage
*
avatar
=
[
UIImage
imageNamed
:
@"1"
];
HMScannerController
*
scanner
=
[
HMScannerController
scannerWithCardName
:
cardName
avatar
:
avatar
completion
:^
(
NSString
*
stringValue
)
{
// self.scanResultLabel.text = stringValue;
NSLog
(
@"%@"
,
stringValue
);
}];
[
scanner
setTitleColor
:[
UIColor
whiteColor
]
tintColor
:[
UIColor
greenColor
]];
// [self showDetailViewController:scanner sender:nil];
[
self
presentViewController
:
scanner
animated
:
YES
completion
:
nil
];
// [self.navigationController pushViewController:scanner animated:YES];
return
;
self
.
screenView
=
[[[
NSBundle
mainBundle
]
loadNibNamed
:
@"ScreeningView"
owner
:
self
options
:
nil
]
firstObject
];
self
.
screenView
=
[[[
NSBundle
mainBundle
]
loadNibNamed
:
@"ScreeningView"
owner
:
self
options
:
nil
]
firstObject
];
...
...
Lighting/Class/Tabbar/CustomTabbarController.m
View file @
4be36868
...
@@ -19,7 +19,6 @@
...
@@ -19,7 +19,6 @@
#import "CustomerOrderViewController.h"
#import "CustomerOrderViewController.h"
#import "UserViewController.h"
#import "UserViewController.h"
#import "ChangePasswordViewController.h"
#import "ChangePasswordViewController.h"
#import "HMScannerController.h"
@interface
CustomTabbarController
()
<
TabbarButtonClickdelegate
,
ChangpasswordDelegate
,
CancelButtondelegate
>
@interface
CustomTabbarController
()
<
TabbarButtonClickdelegate
,
ChangpasswordDelegate
,
CancelButtondelegate
>
...
...
Lighting/Lighting.xcodeproj/project.pbxproj
View file @
4be36868
This diff is collapsed.
Click to expand it.
Lighting/Lighting/Info.plist
View file @
4be36868
...
@@ -43,7 +43,6 @@
...
@@ -43,7 +43,6 @@
<
a
rr
a
y
>
<
a
rr
a
y
>
<
string
>
UIInterfaceOrientationLandscapeLeft
<
/string
>
<
string
>
UIInterfaceOrientationLandscapeLeft
<
/string
>
<
string
>
UIInterfaceOrientationLandscapeRight
<
/string
>
<
string
>
UIInterfaceOrientationLandscapeRight
<
/string
>
<
string
>
UIInterfaceOrientationPortrait
<
/string
>
<
/
a
rr
a
y
>
<
/
a
rr
a
y
>
<
k
e
y
>
UIViewControllerBasedStatusBarAppearance
<
/k
e
y
>
<
k
e
y
>
UIViewControllerBasedStatusBarAppearance
<
/k
e
y
>
<
fa
ls
e
/
>
<
fa
ls
e
/
>
...
...
Lighting/Tools/QRCode/HMScanerCardViewController.h
deleted
100755 → 0
View file @
0889cec3
//
// HMScanerCardViewController.h
// HMQRCodeScanner
//
// Created by 刘凡 on 16/1/3.
// Copyright © 2016年 itheima. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface
HMScanerCardViewController
:
UIViewController
-
(
instancetype
)
initWithCardName
:(
NSString
*
)
cardName
avatar
:(
UIImage
*
)
avatar
;
@end
Lighting/Tools/QRCode/HMScanerCardViewController.m
deleted
100755 → 0
View file @
0889cec3
//
// HMScanerCardViewController.m
// HMQRCodeScanner
//
// Created by 刘凡 on 16/1/3.
// Copyright © 2016年 itheima. All rights reserved.
//
#import "HMScanerCardViewController.h"
#import "HMScanner.h"
@interface
HMScanerCardViewController
()
/// 名片字符串
@property
(
nonatomic
)
NSString
*
cardName
;
/// 头像图片
@property
(
nonatomic
)
UIImage
*
avatar
;
@end
@implementation
HMScanerCardViewController
{
UIImageView
*
cardImageView
;
}
#pragma mark - 构造函数
-
(
instancetype
)
initWithCardName
:
(
NSString
*
)
cardName
avatar
:
(
UIImage
*
)
avatar
{
self
=
[
super
init
];
if
(
self
)
{
self
.
cardName
=
cardName
;
self
.
avatar
=
avatar
;
}
return
self
;
}
#pragma mark - 设置界面
-
(
void
)
viewDidLoad
{
[
super
viewDidLoad
];
self
.
view
.
backgroundColor
=
[
UIColor
darkGrayColor
];
[
self
prepareNavigationBar
];
CGFloat
width
=
self
.
view
.
bounds
.
size
.
width
-
80
;
cardImageView
=
[[
UIImageView
alloc
]
initWithFrame
:
CGRectMake
(
0
,
0
,
width
,
width
)];
cardImageView
.
center
=
self
.
view
.
center
;
[
self
.
view
addSubview
:
cardImageView
];
[
HMScanner
qrImageWithString
:
self
.
cardName
avatar
:
self
.
avatar
completion
:^
(
UIImage
*
image
)
{
cardImageView
.
image
=
image
;
}];
}
/// 准备导航栏
-
(
void
)
prepareNavigationBar
{
// 1> 背景颜色
[
self
.
navigationController
.
navigationBar
setBarTintColor
:[
UIColor
colorWithWhite
:
0
.
1
alpha
:
1
.
0
]];
self
.
navigationController
.
navigationBar
.
translucent
=
YES
;
self
.
navigationController
.
navigationBar
.
shadowImage
=
[[
UIImage
alloc
]
init
];
// 2> 标题
self
.
title
=
@"我的名片"
;
}
@end
Lighting/Tools/QRCode/HMScanner.bundle/QRCodeScanLine@2x.png
deleted
100755 → 0
View file @
0889cec3
1.24 KB
Lighting/Tools/QRCode/HMScanner.bundle/ScanQR1@2x.png
deleted
100755 → 0
View file @
0889cec3
106 Bytes
Lighting/Tools/QRCode/HMScanner.bundle/ScanQR2@2x.png
deleted
100755 → 0
View file @
0889cec3
106 Bytes
Lighting/Tools/QRCode/HMScanner.bundle/ScanQR3@2x.png
deleted
100755 → 0
View file @
0889cec3
106 Bytes
Lighting/Tools/QRCode/HMScanner.bundle/ScanQR4@2x.png
deleted
100755 → 0
View file @
0889cec3
106 Bytes
Lighting/Tools/QRCode/HMScanner.h
deleted
100755 → 0
View file @
0889cec3
//
// HMScanner.h
// HMQRCodeScanner
//
// Created by 刘凡 on 16/1/2.
// Copyright © 2016年 itheima. All rights reserved.
//
#import <UIKit/UIKit.h>
/// 二维码/条码扫描器
@interface
HMScanner
:
NSObject
/// 使用视图实例化扫描器,扫描预览窗口会添加到指定视图中
///
/// @param view 指定的视图
/// @param scanFrame 扫描范围
/// @param completion 完成回调
///
/// @return 扫描器
+
(
instancetype
)
scanerWithView
:(
UIView
*
)
view
scanFrame
:(
CGRect
)
scanFrame
completion
:(
void
(
^
)(
NSString
*
stringValue
))
completion
;
/// 扫描图像
///
/// @param image 包含二维码的图像
/// @remark 目前只支持 64 位的 iOS 设备
+
(
void
)
scaneImage
:(
UIImage
*
)
image
completion
:(
void
(
^
)(
NSArray
*
values
))
completion
;
/// 使用 string / 头像 异步生成二维码图像
///
/// @param string 二维码图像的字符串
/// @param avatar 头像图像,默认比例 0.2
/// @param completion 完成回调
+
(
void
)
qrImageWithString
:(
NSString
*
)
string
avatar
:(
UIImage
*
)
avatar
completion
:(
void
(
^
)(
UIImage
*
image
))
completion
;
/// 使用 string / 头像 异步生成二维码图像,并且指定头像占二维码图像的比例
///
/// @param string 二维码图像的字符串
/// @param avatar 头像图像
/// @param scale 头像占二维码图像的比例
/// @param completion 完成回调
+
(
void
)
qrImageWithString
:(
NSString
*
)
string
avatar
:(
UIImage
*
)
avatar
scale
:(
CGFloat
)
scale
completion
:(
void
(
^
)(
UIImage
*
))
completion
;
/// 开始扫描
-
(
void
)
startScan
;
/// 停止扫描
-
(
void
)
stopScan
;
@end
Lighting/Tools/QRCode/HMScanner.m
deleted
100755 → 0
View file @
0889cec3
//
// HMScanner.m
// HMQRCodeScanner
//
// Created by 刘凡 on 16/1/2.
// Copyright © 2016年 itheima. All rights reserved.
//
#import "HMScanner.h"
#import <AVFoundation/AVFoundation.h>
/// 最大检测次数
#define kMaxDetectedCount 20
@interface
HMScanner
()
<
AVCaptureMetadataOutputObjectsDelegate
>
/// 父视图弱引用
@property
(
nonatomic
,
weak
)
UIView
*
parentView
;
/// 扫描范围
@property
(
nonatomic
)
CGRect
scanFrame
;
/// 完成回调
@property
(
nonatomic
,
copy
)
void
(
^
completionCallBack
)(
NSString
*
);
@end
@implementation
HMScanner
{
/// 拍摄会话
AVCaptureSession
*
session
;
/// 预览图层
AVCaptureVideoPreviewLayer
*
previewLayer
;
/// 绘制图层
CALayer
*
drawLayer
;
/// 当前检测计数
NSInteger
currentDetectedCount
;
}
#pragma mark - 生成二维码
+
(
void
)
qrImageWithString
:
(
NSString
*
)
string
avatar
:
(
UIImage
*
)
avatar
completion
:
(
void
(
^
)(
UIImage
*
))
completion
{
[
self
qrImageWithString
:
string
avatar
:
avatar
scale
:
0
.
20
completion
:
completion
];
}
+
(
void
)
qrImageWithString
:
(
NSString
*
)
string
avatar
:
(
UIImage
*
)
avatar
scale
:
(
CGFloat
)
scale
completion
:
(
void
(
^
)(
UIImage
*
))
completion
{
NSAssert
(
completion
!=
nil
,
@"必须传入完成回调"
);
dispatch_async
(
dispatch_get_global_queue
(
0
,
0
),
^
{
CIFilter
*
qrFilter
=
[
CIFilter
filterWithName
:
@"CIQRCodeGenerator"
];
[
qrFilter
setDefaults
];
[
qrFilter
setValue
:[
string
dataUsingEncoding
:
NSUTF8StringEncoding
]
forKey
:
@"inputMessage"
];
CIImage
*
ciImage
=
qrFilter
.
outputImage
;
CGAffineTransform
transform
=
CGAffineTransformMakeScale
(
10
,
10
);
CIImage
*
transformedImage
=
[
ciImage
imageByApplyingTransform
:
transform
];
CIContext
*
context
=
[
CIContext
contextWithOptions
:
nil
];
CGImageRef
cgImage
=
[
context
createCGImage
:
transformedImage
fromRect
:
transformedImage
.
extent
];
UIImage
*
qrImage
=
[
UIImage
imageWithCGImage
:
cgImage
scale
:[
UIScreen
mainScreen
].
scale
orientation
:
UIImageOrientationUp
];
CGImageRelease
(
cgImage
);
if
(
avatar
!=
nil
)
{
qrImage
=
[
self
qrcodeImage
:
qrImage
addAvatar
:
avatar
scale
:
scale
];
}
dispatch_async
(
dispatch_get_main_queue
(),
^
{
completion
(
qrImage
);
});
});
}
+
(
UIImage
*
)
qrcodeImage
:
(
UIImage
*
)
qrImage
addAvatar
:
(
UIImage
*
)
avatar
scale
:
(
CGFloat
)
scale
{
CGFloat
screenScale
=
[
UIScreen
mainScreen
].
scale
;
CGRect
rect
=
CGRectMake
(
0
,
0
,
qrImage
.
size
.
width
*
screenScale
,
qrImage
.
size
.
height
*
screenScale
);
UIGraphicsBeginImageContextWithOptions
(
rect
.
size
,
YES
,
screenScale
);
[
qrImage
drawInRect
:
rect
];
CGSize
avatarSize
=
CGSizeMake
(
rect
.
size
.
width
*
scale
,
rect
.
size
.
height
*
scale
);
CGFloat
x
=
(
rect
.
size
.
width
-
avatarSize
.
width
)
*
0
.
5
;
CGFloat
y
=
(
rect
.
size
.
height
-
avatarSize
.
height
)
*
0
.
5
;
[
avatar
drawInRect
:
CGRectMake
(
x
,
y
,
avatarSize
.
width
,
avatarSize
.
height
)];
UIImage
*
result
=
UIGraphicsGetImageFromCurrentImageContext
();
UIGraphicsEndImageContext
();
return
[
UIImage
imageWithCGImage
:
result
.
CGImage
scale
:
screenScale
orientation
:
UIImageOrientationUp
];
}
#pragma mark - 扫描图像方法
+
(
void
)
scaneImage
:
(
UIImage
*
)
image
completion
:
(
void
(
^
)(
NSArray
*
))
completion
{
NSAssert
(
completion
!=
nil
,
@"必须传入完成回调"
);
dispatch_async
(
dispatch_get_global_queue
(
0
,
0
),
^
{
CIDetector
*
detector
=
[
CIDetector
detectorOfType
:
CIDetectorTypeQRCode
context
:
nil
options
:
@{
CIDetectorAccuracy
:
CIDetectorAccuracyHigh
}];
CIImage
*
ciImage
=
[[
CIImage
alloc
]
initWithImage
:
image
];
NSArray
*
features
=
[
detector
featuresInImage
:
ciImage
];
NSMutableArray
*
arrayM
=
[
NSMutableArray
arrayWithCapacity
:
features
.
count
];
for
(
CIQRCodeFeature
*
feature
in
features
)
{
[
arrayM
addObject
:
feature
.
messageString
];
}
dispatch_async
(
dispatch_get_main_queue
(),
^
{
completion
(
arrayM
.
copy
);
});
});
}
#pragma mark - 构造函数
+
(
instancetype
)
scanerWithView
:
(
UIView
*
)
view
scanFrame
:
(
CGRect
)
scanFrame
completion
:
(
void
(
^
)(
NSString
*
))
completion
{
NSAssert
(
completion
!=
nil
,
@"必须传入完成回调"
);
return
[[
self
alloc
]
initWithView
:
view
scanFrame
:
scanFrame
completion
:
completion
];
}
-
(
instancetype
)
initWithView
:
(
UIView
*
)
view
scanFrame
:
(
CGRect
)
scanFrame
completion
:
(
void
(
^
)(
NSString
*
))
completion
{
self
=
[
super
init
];
if
(
self
)
{
self
.
parentView
=
view
;
self
.
scanFrame
=
scanFrame
;
self
.
completionCallBack
=
completion
;
[
self
setupSession
];
}
return
self
;
}
#pragma mark - 公共方法
/// 开始扫描
-
(
void
)
startScan
{
if
([
session
isRunning
])
{
return
;
}
currentDetectedCount
=
0
;
[
session
startRunning
];
}
-
(
void
)
stopScan
{
if
(
!
[
session
isRunning
])
{
return
;
}
[
session
stopRunning
];
}
#pragma mark - AVCaptureMetadataOutputObjectsDelegate
-
(
void
)
captureOutput
:
(
AVCaptureOutput
*
)
captureOutput
didOutputMetadataObjects
:
(
NSArray
*
)
metadataObjects
fromConnection
:
(
AVCaptureConnection
*
)
connection
{
[
self
clearDrawLayer
];
for
(
id
obj
in
metadataObjects
)
{
// 判断检测到的对象类型
if
(
!
[
obj
isKindOfClass
:[
AVMetadataMachineReadableCodeObject
class
]])
{
return
;
}
// 转换对象坐标
AVMetadataMachineReadableCodeObject
*
dataObject
=
(
AVMetadataMachineReadableCodeObject
*
)[
previewLayer
transformedMetadataObjectForMetadataObject
:
obj
];
// 判断扫描范围
if
(
!
CGRectContainsRect
(
self
.
scanFrame
,
dataObject
.
bounds
))
{
continue
;
}
if
(
currentDetectedCount
++
<
kMaxDetectedCount
)
{
// 绘制边角
[
self
drawCornersShape
:
dataObject
];
}
else
{
[
self
stopScan
];
// 完成回调
if
(
self
.
completionCallBack
!=
nil
)
{
self
.
completionCallBack
(
dataObject
.
stringValue
);
}
}
}
}
/// 清空绘制图层
-
(
void
)
clearDrawLayer
{
if
(
drawLayer
.
sublayers
.
count
==
0
)
{
return
;
}
[
drawLayer
.
sublayers
makeObjectsPerformSelector
:
@selector
(
removeFromSuperlayer
)];
}
/// 绘制条码形状
///
/// @param dataObject 识别到的数据对象
-
(
void
)
drawCornersShape
:
(
AVMetadataMachineReadableCodeObject
*
)
dataObject
{
if
(
dataObject
.
corners
.
count
==
0
)
{
return
;
}
CAShapeLayer
*
layer
=
[
CAShapeLayer
layer
];
layer
.
lineWidth
=
4
;
layer
.
strokeColor
=
[
UIColor
greenColor
].
CGColor
;
layer
.
fillColor
=
[
UIColor
clearColor
].
CGColor
;
layer
.
path
=
[
self
cornersPath
:
dataObject
.
corners
];
[
drawLayer
addSublayer
:
layer
];
}
/// 使用 corners 数组生成绘制路径
///
/// @param corners corners 数组
///
/// @return 绘制路径
-
(
CGPathRef
)
cornersPath
:
(
NSArray
*
)
corners
{
UIBezierPath
*
path
=
[
UIBezierPath
bezierPath
];
CGPoint
point
=
CGPointZero
;
// 1. 移动到第一个点
NSInteger
index
=
0
;
CGPointMakeWithDictionaryRepresentation
((
CFDictionaryRef
)
corners
[
index
++
],
&
point
);
[
path
moveToPoint
:
point
];
// 2. 遍历剩余的点
while
(
index
<
corners
.
count
)
{
CGPointMakeWithDictionaryRepresentation
((
CFDictionaryRef
)
corners
[
index
++
],
&
point
);
[
path
addLineToPoint
:
point
];
}
// 3. 关闭路径
[
path
closePath
];
return
path
.
CGPath
;
}
#pragma mark - 扫描相关方法
/// 设置绘制图层和预览图层
-
(
void
)
setupLayers
{
if
(
self
.
parentView
==
nil
)
{
NSLog
(
@"父视图不存在"
);
return
;
}
if
(
session
==
nil
)
{
NSLog
(
@"拍摄会话不存在"
);
return
;
}
// 绘制图层
drawLayer
=
[
CALayer
layer
];
drawLayer
.
frame
=
self
.
parentView
.
bounds
;
[
self
.
parentView
.
layer
insertSublayer
:
drawLayer
atIndex
:
0
];
// 预览图层
previewLayer
=
[[
AVCaptureVideoPreviewLayer
alloc
]
initWithSession
:
session
];
previewLayer
.
videoGravity
=
AVLayerVideoGravityResizeAspectFill
;
previewLayer
.
frame
=
self
.
parentView
.
bounds
;
[
self
.
parentView
.
layer
insertSublayer
:
previewLayer
atIndex
:
0
];
}
/// 设置扫描会话
-
(
void
)
setupSession
{
// 1> 输入设备
AVCaptureDevice
*
device
=
[
AVCaptureDevice
defaultDeviceWithMediaType
:
AVMediaTypeVideo
];
AVCaptureDeviceInput
*
videoInput
=
[
AVCaptureDeviceInput
deviceInputWithDevice
:
device
error
:
nil
];
if
(
videoInput
==
nil
)
{
NSLog
(
@"创建输入设备失败"
);
return
;
}
// 2> 数据输出
AVCaptureMetadataOutput
*
dataOutput
=
[[
AVCaptureMetadataOutput
alloc
]
init
];
// 3> 拍摄会话 - 判断能够添加设备
session
=
[[
AVCaptureSession
alloc
]
init
];
if
(
!
[
session
canAddInput
:
videoInput
])
{
NSLog
(
@"无法添加输入设备"
);
session
=
nil
;
return
;
}
if
(
!
[
session
canAddOutput
:
dataOutput
])
{
NSLog
(
@"无法添加输入设备"
);
session
=
nil
;
return
;
}
// 4> 添加输入/输出设备
[
session
addInput
:
videoInput
];
[
session
addOutput
:
dataOutput
];
// 5> 设置扫描类型
dataOutput
.
metadataObjectTypes
=
dataOutput
.
availableMetadataObjectTypes
;
[
dataOutput
setMetadataObjectsDelegate
:
self
queue
:
dispatch_get_main_queue
()];
// 6> 设置预览图层会话
[
self
setupLayers
];
}
@end
Lighting/Tools/QRCode/HMScannerBorder.h
deleted
100755 → 0
View file @
0889cec3
//
// HMScannerBorder.h
// HMQRCodeScanner
//
// Created by 刘凡 on 16/1/2.
// Copyright © 2016年 itheima. All rights reserved.
//
#import <UIKit/UIKit.h>
/// 扫描框视图
@interface
HMScannerBorder
:
UIView
/// 开始扫描动画
-
(
void
)
startScannerAnimating
;
/// 停止扫描动画
-
(
void
)
stopScannerAnimating
;
@end
Lighting/Tools/QRCode/HMScannerBorder.m
deleted
100755 → 0
View file @
0889cec3
//
// HMScannerBorder.m
// HMQRCodeScanner
//
// Created by 刘凡 on 16/1/2.
// Copyright © 2016年 itheima. All rights reserved.
//
#import "HMScannerBorder.h"
@implementation
HMScannerBorder
{
/// 冲击波图像
UIImageView
*
scannerLine
;
}
-
(
instancetype
)
initWithFrame
:
(
CGRect
)
frame
{
self
=
[
super
initWithFrame
:
frame
];
if
(
self
)
{
[
self
prepareUI
];
}
return
self
;
}
#pragma mark - 扫描动画方法
/// 开始扫描动画
-
(
void
)
startScannerAnimating
{
[
self
stopScannerAnimating
];
[
UIView
animateWithDuration
:
3
.
0
delay
:
0
.
0
options
:
UIViewAnimationOptionCurveEaseInOut
animations
:^
{
[
UIView
setAnimationRepeatCount
:
MAXFLOAT
];
scannerLine
.
center
=
CGPointMake
(
self
.
bounds
.
size
.
width
*
0
.
5
,
self
.
bounds
.
size
.
height
);
}
completion
:
nil
];
}
/// 停止扫描动画
-
(
void
)
stopScannerAnimating
{
[
scannerLine
.
layer
removeAllAnimations
];
scannerLine
.
center
=
CGPointMake
(
self
.
bounds
.
size
.
width
*
0
.
5
,
0
);
}
#pragma mark - 设置界面
-
(
void
)
prepareUI
{
self
.
clipsToBounds
=
YES
;
// 图像文件包
NSBundle
*
bundle
=
[
NSBundle
bundleForClass
:[
self
class
]];
NSURL
*
url
=
[
bundle
URLForResource
:
@"HMScanner"
withExtension
:
@"bundle"
];
NSBundle
*
imageBundle
=
[
NSBundle
bundleWithURL
:
url
];
// 冲击波图像
scannerLine
=
[[
UIImageView
alloc
]
initWithImage
:[
self
imageWithName
:
@"QRCodeScanLine"
bundle
:
imageBundle
]];
scannerLine
.
frame
=
CGRectMake
(
0
,
0
,
self
.
bounds
.
size
.
width
,
scannerLine
.
bounds
.
size
.
height
);
scannerLine
.
center
=
CGPointMake
(
self
.
bounds
.
size
.
width
*
0
.
5
,
0
);
[
self
addSubview
:
scannerLine
];
// 加载边框图像
for
(
NSInteger
i
=
1
;
i
<
5
;
i
++
)
{
NSString
*
imgName
=
[
NSString
stringWithFormat
:
@"ScanQR%zd"
,
i
];
UIImageView
*
img
=
[[
UIImageView
alloc
]
initWithImage
:[
self
imageWithName
:
imgName
bundle
:
imageBundle
]];
[
self
addSubview
:
img
];
CGFloat
offsetX
=
self
.
bounds
.
size
.
width
-
img
.
bounds
.
size
.
width
;
CGFloat
offsetY
=
self
.
bounds
.
size
.
height
-
img
.
bounds
.
size
.
height
;
switch
(
i
)
{
case
2
:
img
.
frame
=
CGRectOffset
(
img
.
frame
,
offsetX
,
0
);
break
;
case
3
:
img
.
frame
=
CGRectOffset
(
img
.
frame
,
0
,
offsetY
);
break
;
case
4
:
img
.
frame
=
CGRectOffset
(
img
.
frame
,
offsetX
,
offsetY
);
break
;
default
:
break
;
}
}
}
-
(
UIImage
*
)
imageWithName
:
(
NSString
*
)
imageName
bundle
:
(
NSBundle
*
)
imageBundle
{
NSString
*
fileName
=
[
NSString
stringWithFormat
:
@"%@@2x"
,
imageName
];
NSString
*
path
=
[
imageBundle
pathForResource
:
fileName
ofType
:
@"png"
];
return
[[
UIImage
imageWithContentsOfFile
:
path
]
imageWithRenderingMode
:
UIImageRenderingModeAlwaysTemplate
];
}
@end
Lighting/Tools/QRCode/HMScannerController.h
deleted
100755 → 0
View file @
0889cec3
//
// HMScannerController.h
// HMQRCodeScanner
//
// Created by 刘凡 on 16/1/2.
// Copyright © 2016年 itheima. All rights reserved.
//
#import <UIKit/UIKit.h>
/*!
扫描控制器
作用:
* 提供一个导航控制器,扫描 `二维码 / 条形码`
* 能够生成指定 `字符串` + `avatar(可选)` 的二维码名片
* 能够识别相册图片中的二维码(iOS 64 位设备)
使用:
@code
NSString *cardName = @"天涯刀哥 - 傅红雪";
UIImage *avatar = [UIImage imageNamed:@"avatar"];
// 实例化控制器,并指定完成回调
HMScannerController *scanner = [HMScannerController scannerWithCardName:cardName avatar:avatar completion:^(NSString *stringValue) {
self.scanResultLabel.text = stringValue;
}];
// 设置导航标题样式
[scanner setTitleColor:[UIColor whiteColor] tintColor:[UIColor greenColor]];
// 展现扫描控制器
[self showDetailViewController:scanner sender:nil];
@endcode
*/
@interface
HMScannerController
:
UINavigationController
/// 使用 `名片字符串` 实例化扫描导航控制器
///
/// @param cardName 名片字符串
/// @param avatar 头像图像
/// @param completion 完成回调
///
/// @return 扫描导航控制器
+
(
instancetype
)
scannerWithCardName
:(
NSString
*
)
cardName
avatar
:(
UIImage
*
)
avatar
completion
:(
void
(
^
)(
NSString
*
stringValue
))
completion
;
/// 使用 名片字符串 / 头像 异步生成二维码图像,并且指定头像占二维码图像的比例
///
/// @param string 名片字符串
/// @param avatar 头像图像
/// @param scale 头像占二维码图像的比例
/// @param completion 完成回调
+
(
void
)
cardImageWithCardName
:(
NSString
*
)
cardName
avatar
:(
UIImage
*
)
avatar
scale
:(
CGFloat
)
scale
completion
:(
void
(
^
)(
UIImage
*
image
))
completion
;
/// 设置导航栏标题颜色和 tintColor
///
/// @param titleColor 标题颜色
/// @param tintColor tintColor
-
(
void
)
setTitleColor
:(
UIColor
*
)
titleColor
tintColor
:(
UIColor
*
)
tintColor
;
@end
Lighting/Tools/QRCode/HMScannerController.m
deleted
100755 → 0
View file @
0889cec3
//
// HMScannerController.m
// HMQRCodeScanner
//
// Created by 刘凡 on 16/1/2.
// Copyright © 2016年 itheima. All rights reserved.
//
#import "HMScannerController.h"
#import "HMScannerViewController.h"
#import "HMScanner.h"
@implementation
HMScannerController
+
(
void
)
cardImageWithCardName
:(
NSString
*
)
cardName
avatar
:(
UIImage
*
)
avatar
scale
:(
CGFloat
)
scale
completion
:(
void
(
^
)(
UIImage
*
))
completion
{
[
HMScanner
qrImageWithString
:
cardName
avatar
:
avatar
scale
:
scale
completion
:
completion
];
}
+
(
instancetype
)
scannerWithCardName
:(
NSString
*
)
cardName
avatar
:(
UIImage
*
)
avatar
completion
:(
void
(
^
)(
NSString
*
))
completion
{
NSAssert
(
completion
!=
nil
,
@"必须传入完成回调"
);
return
[[
self
alloc
]
initWithCardName
:
cardName
avatar
:
avatar
completion
:
completion
];
}
-
(
instancetype
)
initWithCardName
:(
NSString
*
)
cardName
avatar
:(
UIImage
*
)
avatar
completion
:(
void
(
^
)(
NSString
*
))
completion
{
self
=
[
super
init
];
if
(
self
)
{
HMScannerViewController
*
scanner
=
[[
HMScannerViewController
alloc
]
initWithCardName
:
cardName
avatar
:
avatar
completion
:
completion
];
[
self
setTitleColor
:[
UIColor
whiteColor
]
tintColor
:[
UIColor
greenColor
]];
[
self
pushViewController
:
scanner
animated
:
NO
];
}
return
self
;
}
-
(
void
)
setTitleColor
:(
UIColor
*
)
titleColor
tintColor
:(
UIColor
*
)
tintColor
{
[
self
.
navigationBar
setTitleTextAttributes
:@{
NSForegroundColorAttributeName
:
titleColor
}];
self
.
navigationBar
.
tintColor
=
tintColor
;
}
-
(
UIInterfaceOrientationMask
)
supportedInterfaceOrientations
{
return
UIInterfaceOrientationMaskPortrait
;
}
-
(
UIStatusBarStyle
)
preferredStatusBarStyle
{
return
UIStatusBarStyleLightContent
;
}
@end
Lighting/Tools/QRCode/HMScannerMaskView.h
deleted
100755 → 0
View file @
0889cec3
//
// HMScannerMaskView.h
// HMQRCodeScanner
//
// Created by 刘凡 on 16/1/3.
// Copyright © 2016年 itheima. All rights reserved.
//
#import <UIKit/UIKit.h>
/// 扫描遮罩视图
@interface
HMScannerMaskView
:
UIView
/// 使用裁切区域实例化遮罩视图
///
/// @param frame 视图区域
/// @param cropRect 裁切区域
///
/// @return 遮罩视图
+
(
instancetype
)
maskViewWithFrame
:(
CGRect
)
frame
cropRect
:(
CGRect
)
cropRect
;
/// 裁切区域
@property
(
nonatomic
)
CGRect
cropRect
;
@end
Lighting/Tools/QRCode/HMScannerMaskView.m
deleted
100755 → 0
View file @
0889cec3
//
// HMScannerMaskView.m
// HMQRCodeScanner
//
// Created by 刘凡 on 16/1/3.
// Copyright © 2016年 itheima. All rights reserved.
//
#import "HMScannerMaskView.h"
@implementation
HMScannerMaskView
+
(
instancetype
)
maskViewWithFrame
:(
CGRect
)
frame
cropRect
:(
CGRect
)
cropRect
{
HMScannerMaskView
*
maskView
=
[[
self
alloc
]
initWithFrame
:
frame
];
maskView
.
backgroundColor
=
[
UIColor
clearColor
];
maskView
.
cropRect
=
cropRect
;
return
maskView
;
}
-
(
void
)
setCropRect
:(
CGRect
)
cropRect
{
_cropRect
=
cropRect
;
[
self
setNeedsDisplay
];
}
-
(
void
)
drawRect
:(
CGRect
)
rect
{
CGContextRef
ctx
=
UIGraphicsGetCurrentContext
();
[[
UIColor
colorWithWhite
:
0
.
0
alpha
:
0
.
4
]
setFill
];
CGContextFillRect
(
ctx
,
rect
);
CGContextClearRect
(
ctx
,
self
.
cropRect
);
[[
UIColor
colorWithWhite
:
0
.
95
alpha
:
1
.
0
]
setStroke
];
CGContextStrokeRectWithWidth
(
ctx
,
CGRectInset
(
_cropRect
,
1
,
1
),
1
);
}
@end
Lighting/Tools/QRCode/HMScannerViewController.h
deleted
100755 → 0
View file @
0889cec3
//
// HMScannerViewController.h
// HMQRCodeScanner
//
// Created by 刘凡 on 16/1/2.
// Copyright © 2016年 itheima. All rights reserved.
//
#import <UIKit/UIKit.h>
/// 扫描控制器
@interface
HMScannerViewController
:
UIViewController
/// 实例化扫描控制器
///
/// @param cardName 名片字符串
/// @param avatar 头像图片
/// @param completion 完成回调
///
/// @return 扫描控制器
-
(
instancetype
)
initWithCardName
:(
NSString
*
)
cardName
avatar
:(
UIImage
*
)
avatar
completion
:(
void
(
^
)(
NSString
*
stringValue
))
completion
;
@end
Lighting/Tools/QRCode/HMScannerViewController.m
deleted
100755 → 0
View file @
0889cec3
//
// HMScannerViewController.m
// HMQRCodeScanner
//
// Created by 刘凡 on 16/1/2.
// Copyright © 2016年 itheima. All rights reserved.
//
#import "HMScannerViewController.h"
#import "HMScanerCardViewController.h"
#import "HMScannerBorder.h"
#import "HMScannerMaskView.h"
#import "HMScanner.h"
/// 控件间距
#define kControlMargin 32.0
@interface
HMScannerViewController
()
<
UIImagePickerControllerDelegate
,
UINavigationControllerDelegate
>
/// 名片字符串
@property
(
nonatomic
)
NSString
*
cardName
;
/// 头像图片
@property
(
nonatomic
)
UIImage
*
avatar
;
/// 完成回调
@property
(
nonatomic
,
copy
)
void
(
^
completionCallBack
)(
NSString
*
);
@end
@implementation
HMScannerViewController
{
/// 扫描框
HMScannerBorder
*
scannerBorder
;
/// 扫描器
HMScanner
*
scanner
;
/// 提示标签
UILabel
*
tipLabel
;
}
-
(
instancetype
)
initWithCardName
:
(
NSString
*
)
cardName
avatar
:
(
UIImage
*
)
avatar
completion
:
(
void
(
^
)(
NSString
*
))
completion
{
self
=
[
super
init
];
if
(
self
)
{
self
.
cardName
=
cardName
;
self
.
avatar
=
avatar
;
self
.
completionCallBack
=
completion
;
}
return
self
;
}
-
(
void
)
viewDidLoad
{
[
super
viewDidLoad
];
[
self
prepareUI
];
// 实例化扫描器
__weak
typeof
(
self
)
weakSelf
=
self
;
scanner
=
[
HMScanner
scanerWithView
:
self
.
view
scanFrame
:
scannerBorder
.
frame
completion
:^
(
NSString
*
stringValue
)
{
// 完成回调
weakSelf
.
completionCallBack
(
stringValue
);
// 关闭
[
weakSelf
clickCloseButton
];
}];
}
-
(
void
)
viewWillAppear
:
(
BOOL
)
animated
{
[
super
viewWillAppear
:
animated
];
[
scannerBorder
startScannerAnimating
];
[
scanner
startScan
];
}
-
(
void
)
viewWillDisappear
:
(
BOOL
)
animated
{
[
super
viewWillDisappear
:
animated
];
[
scannerBorder
stopScannerAnimating
];
[
scanner
stopScan
];
}
#pragma mark - 监听方法
/// 点击关闭按钮
-
(
void
)
clickCloseButton
{
[
self
dismissViewControllerAnimated
:
YES
completion
:
nil
];
}
/// 点击相册按钮
-
(
void
)
clickAlbumButton
{
if
(
!
[
UIImagePickerController
isSourceTypeAvailable
:
UIImagePickerControllerSourceTypePhotoLibrary
])
{
tipLabel
.
text
=
@"无法访问相册"
;
return
;
}
UIImagePickerController
*
picker
=
[[
UIImagePickerController
alloc
]
init
];
picker
.
view
.
backgroundColor
=
[
UIColor
whiteColor
];
picker
.
delegate
=
self
;
[
self
showDetailViewController
:
picker
sender
:
nil
];
}
/// 点击名片按钮
-
(
void
)
clickCardButton
{
HMScanerCardViewController
*
vc
=
[[
HMScanerCardViewController
alloc
]
initWithCardName
:
self
.
cardName
avatar
:
self
.
avatar
];
[
self
showViewController
:
vc
sender
:
nil
];
}
#pragma mark - UIImagePickerControllerDelegate
-
(
void
)
imagePickerController
:
(
UIImagePickerController
*
)
picker
didFinishPickingMediaWithInfo
:
(
NSDictionary
<
NSString
*
,
id
>
*
)
info
{
// 扫描图像
[
HMScanner
scaneImage
:
info
[
UIImagePickerControllerOriginalImage
]
completion
:
^
(
NSArray
*
values
)
{
if
(
values
.
count
>
0
)
{
self
.
completionCallBack
(
values
.
firstObject
);
[
self
dismissViewControllerAnimated
:
NO
completion
:
^
{
[
self
clickCloseButton
];
}];
}
else
{
tipLabel
.
text
=
@"没有识别到二维码,请选择其他照片"
;
[
self
dismissViewControllerAnimated
:
YES
completion
:
nil
];
}
}];
}
#pragma mark - 设置界面
-
(
void
)
prepareUI
{
self
.
view
.
backgroundColor
=
[
UIColor
darkGrayColor
];
[
self
prepareNavigationBar
];
[
self
prepareScanerBorder
];
[
self
prepareOtherControls
];
}
/// 准备提示标签和名片按钮
-
(
void
)
prepareOtherControls
{
// 1> 提示标签
tipLabel
=
[[
UILabel
alloc
]
init
];
tipLabel
.
text
=
@"将二维码/条码放入框中,即可自动扫描"
;
tipLabel
.
font
=
[
UIFont
systemFontOfSize
:
12
];
tipLabel
.
textColor
=
[
UIColor
whiteColor
];
tipLabel
.
textAlignment
=
NSTextAlignmentCenter
;
[
tipLabel
sizeToFit
];
tipLabel
.
center
=
CGPointMake
(
scannerBorder
.
center
.
x
,
CGRectGetMaxY
(
scannerBorder
.
frame
)
+
kControlMargin
);
[
self
.
view
addSubview
:
tipLabel
];
// 2> 名片按钮
UIButton
*
cardButton
=
[[
UIButton
alloc
]
init
];
[
cardButton
setTitle
:
@"我的名片"
forState
:
UIControlStateNormal
];
cardButton
.
titleLabel
.
font
=
[
UIFont
systemFontOfSize
:
15
];
[
cardButton
setTitleColor
:
self
.
navigationController
.
navigationBar
.
tintColor
forState
:
UIControlStateNormal
];
[
cardButton
sizeToFit
];
cardButton
.
center
=
CGPointMake
(
tipLabel
.
center
.
x
,
CGRectGetMaxY
(
tipLabel
.
frame
)
+
kControlMargin
);
[
self
.
view
addSubview
:
cardButton
];
[
cardButton
addTarget
:
self
action
:
@selector
(
clickCardButton
)
forControlEvents
:
UIControlEventTouchUpInside
];
}
/// 准备扫描框
-
(
void
)
prepareScanerBorder
{
CGFloat
width
=
self
.
view
.
bounds
.
size
.
width
-
80
;
scannerBorder
=
[[
HMScannerBorder
alloc
]
initWithFrame
:
CGRectMake
(
0
,
0
,
width
,
width
)];
scannerBorder
.
center
=
self
.
view
.
center
;
scannerBorder
.
tintColor
=
self
.
navigationController
.
navigationBar
.
tintColor
;
[
self
.
view
addSubview
:
scannerBorder
];
HMScannerMaskView
*
maskView
=
[
HMScannerMaskView
maskViewWithFrame
:
self
.
view
.
bounds
cropRect
:
scannerBorder
.
frame
];
[
self
.
view
insertSubview
:
maskView
atIndex
:
0
];
}
/// 准备导航栏
-
(
void
)
prepareNavigationBar
{
// 1> 背景颜色
[
self
.
navigationController
.
navigationBar
setBarTintColor
:[
UIColor
colorWithWhite
:
0
.
1
alpha
:
1
.
0
]];
self
.
navigationController
.
navigationBar
.
translucent
=
YES
;
self
.
navigationController
.
navigationBar
.
shadowImage
=
[[
UIImage
alloc
]
init
];
// 2> 标题
self
.
title
=
@"扫一扫"
;
// 3> 左右按钮
self
.
navigationItem
.
leftBarButtonItem
=
[[
UIBarButtonItem
alloc
]
initWithTitle
:
@"关闭"
style
:
UIBarButtonItemStylePlain
target
:
self
action
:
@selector
(
clickCloseButton
)];
self
.
navigationItem
.
rightBarButtonItem
=
[[
UIBarButtonItem
alloc
]
initWithTitle
:
@"相册"
style
:
UIBarButtonItemStylePlain
target
:
self
action
:
@selector
(
clickAlbumButton
)];
}
@end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment