Blame view

Pods/swiftScan/Source/LBXScanView.swift 14.1 KB
582f536d   yangbin   common:2022.1.28
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
  //
  //  LBXScanView.swift
  //  swiftScan
  //
  //  Created by xialibing on 15/12/8.
  //  Copyright © 2015 xialibing. All rights reserved.
  //
  
  import UIKit
  
  open class LBXScanView: UIView {
      
      // 扫码区域各种参数
      var viewStyle = LBXScanViewStyle()
  
      // 扫码区域
      var scanRetangleRect = CGRect.zero
  
      // 线条扫码动画封装
      var scanLineAnimation: LBXScanLineAnimation?
  
      // 网格扫码动画封装
      var scanNetAnimation: LBXScanNetAnimation?
      
      // 线条在中间位置不移动
      var scanLineStill: UIImageView?
  
      // 启动相机时 菊花等待
      var activityView: UIActivityIndicatorView?
  
      // 启动相机中的提示文字
      var labelReadying: UILabel?
  
      // 记录动画状态
      var isAnimationing = false
      
      /**
      初始化扫描界面
      - parameter frame:  界面大小一般为视频显示区域
      - parameter vstyle: 界面效果参数
      
      - returns: instancetype
      */
      public init(frame: CGRect, vstyle: LBXScanViewStyle) {
          viewStyle = vstyle
  
          switch viewStyle.anmiationStyle {
          case LBXScanViewAnimationStyle.LineMove:
              scanLineAnimation = LBXScanLineAnimation.instance()
          case LBXScanViewAnimationStyle.NetGrid:
              scanNetAnimation = LBXScanNetAnimation.instance()
          case LBXScanViewAnimationStyle.LineStill:
              scanLineStill = UIImageView()
              scanLineStill?.image = viewStyle.animationImage
          default:
              break
          }
  
          var frameTmp = frame
          frameTmp.origin = CGPoint.zero
  
          super.init(frame: frameTmp)
  
          backgroundColor = UIColor.clear
      }
      
      override init(frame: CGRect) {
          var frameTmp = frame
          frameTmp.origin = CGPoint.zero
  
          super.init(frame: frameTmp)
  
          backgroundColor = UIColor.clear
      }
      
      public required init?(coder aDecoder: NSCoder) {
          self.init()
      }
  
      deinit {
          if scanLineAnimation != nil {
              scanLineAnimation!.stopStepAnimating()
          }
          if scanNetAnimation != nil {
              scanNetAnimation!.stopStepAnimating()
          }
      }
      
      
      // 开始扫描动画
      func startScanAnimation() {
          guard !isAnimationing else {
              return
          }
          isAnimationing = true
  
          let cropRect = getScanRectForAnimation()
  
          switch viewStyle.anmiationStyle {
          case .LineMove:
              scanLineAnimation?.startAnimatingWithRect(animationRect: cropRect,
                                                        parentView: self,
                                                        image: viewStyle.animationImage)
          case .NetGrid:
              scanNetAnimation?.startAnimatingWithRect(animationRect: cropRect,
                                                       parentView: self,
                                                       image: viewStyle.animationImage)
          case .LineStill:
              let stillRect = CGRect(x: cropRect.origin.x + 20,
                                     y: cropRect.origin.y + cropRect.size.height / 2,
                                     width: cropRect.size.width - 40,
                                     height: 2)
              scanLineStill?.frame = stillRect
  
              addSubview(scanLineStill!)
              scanLineStill?.isHidden = false
          default: break
          }
      }
      
      // 开始扫描动画
      func stopScanAnimation() {
          isAnimationing = false
          switch viewStyle.anmiationStyle {
          case .LineMove:
              scanLineAnimation?.stopStepAnimating()
          case .NetGrid:
              scanNetAnimation?.stopStepAnimating()
          case .LineStill:
              scanLineStill?.isHidden = true
          default: break
          }
      }
      
      // Only override drawRect: if you perform custom drawing.
      // An empty implementation adversely affects performance during animation.
      open override func draw(_ rect: CGRect) {
          drawScanRect()
      }
      
      //MARK: ----- 绘制扫码效果-----
      func drawScanRect() {
          let XRetangleLeft = viewStyle.xScanRetangleOffset
          var sizeRetangle = CGSize(width: frame.size.width - XRetangleLeft * 2.0, height: frame.size.width - XRetangleLeft * 2.0)
          
          if viewStyle.whRatio != 1.0 {
              let w = sizeRetangle.width
              var h = w / viewStyle.whRatio
              h = CGFloat(Int(h))
              sizeRetangle = CGSize(width: w, height: h)
          }
          
          // 扫码区域Y轴最小坐标
          let YMinRetangle = frame.size.height / 2.0 - sizeRetangle.height / 2.0 - viewStyle.centerUpOffset
          let YMaxRetangle = YMinRetangle + sizeRetangle.height
          let XRetangleRight = frame.size.width - XRetangleLeft
          
          guard let context = UIGraphicsGetCurrentContext() else {
              return
          }
          
          // 非扫码区域半透明
          // 设置非识别区域颜色
          context.setFillColor(viewStyle.color_NotRecoginitonArea.cgColor)
          // 填充矩形
          // 扫码区域上面填充
          var rect = CGRect(x: 0, y: 0, width: frame.size.width, height: YMinRetangle)
          context.fill(rect)
  
          // 扫码区域左边填充
          rect = CGRect(x: 0, y: YMinRetangle, width: XRetangleLeft, height: sizeRetangle.height)
          context.fill(rect)
  
          // 扫码区域右边填充
          rect = CGRect(x: XRetangleRight, y: YMinRetangle, width: XRetangleLeft, height: sizeRetangle.height)
          context.fill(rect)
  
          // 扫码区域下面填充
          rect = CGRect(x: 0, y: YMaxRetangle, width: frame.size.width, height: frame.size.height - YMaxRetangle)
          context.fill(rect)
          // 执行绘画
          context.strokePath()
          
          if viewStyle.isNeedShowRetangle {
              // 中间画矩形(正方形)
              context.setStrokeColor(viewStyle.colorRetangleLine.cgColor)
              context.setLineWidth(1)
              context.addRect(CGRect(x: XRetangleLeft, y: YMinRetangle, width: sizeRetangle.width, height: sizeRetangle.height))
  
              // CGContextMoveToPoint(context, XRetangleLeft, YMinRetangle);
              // CGContextAddLineToPoint(context, XRetangleLeft+sizeRetangle.width, YMinRetangle);
  
              context.strokePath()
          }
  
          scanRetangleRect = CGRect(x: XRetangleLeft, y: YMinRetangle, width: sizeRetangle.width, height: sizeRetangle.height)
          
          
          // 画矩形框4格外围相框角
  
          // 相框角的宽度和高度
          let wAngle = viewStyle.photoframeAngleW
          let hAngle = viewStyle.photoframeAngleH
  
          // 4个角的 线的宽度
          let linewidthAngle = viewStyle.photoframeLineW // 经验参数6和4
  
          // 画扫码矩形以及周边半透明黑色坐标参数
          var diffAngle = linewidthAngle / 3
          diffAngle = linewidthAngle / 2 // 框外面4个角与框有缝隙
          diffAngle = linewidthAngle / 2 // 框4个角 在线上加4个角效果
          diffAngle = 0 // 与矩形框重合
          
          switch viewStyle.photoframeAngleStyle {
          case .Outer: diffAngle = linewidthAngle / 3 // 框外面4个角与框紧密联系在一起
          case .On: diffAngle = 0
          case .Inner: diffAngle = -viewStyle.photoframeLineW / 2
          }
          
          context.setStrokeColor(viewStyle.colorAngle.cgColor)
          context.setFillColor(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
  
          // Draw them with a 2.0 stroke width so they are a bit more visible.
          context.setLineWidth(linewidthAngle)
          
          
          //
          let leftX = XRetangleLeft - diffAngle
          let topY = YMinRetangle - diffAngle
          let rightX = XRetangleRight + diffAngle
          let bottomY = YMaxRetangle + diffAngle
  
          // 左上角水平线
          context.move(to: CGPoint(x: leftX - linewidthAngle / 2, y: topY))
          context.addLine(to: CGPoint(x: leftX + wAngle, y: topY))
          
          // 左上角垂直线
          context.move(to: CGPoint(x: leftX, y: topY - linewidthAngle / 2))
          context.addLine(to: CGPoint(x: leftX, y: topY + hAngle))
          
          // 左下角水平线
          context.move(to: CGPoint(x: leftX - linewidthAngle / 2, y: bottomY))
          context.addLine(to: CGPoint(x: leftX + wAngle, y: bottomY))
          
          // 左下角垂直线
          context.move(to: CGPoint(x: leftX, y: bottomY + linewidthAngle / 2))
          context.addLine(to: CGPoint(x: leftX, y: bottomY - hAngle))
  
          // 右上角水平线
          context.move(to: CGPoint(x: rightX + linewidthAngle / 2, y: topY))
          context.addLine(to: CGPoint(x: rightX - wAngle, y: topY))
          
          // 右上角垂直线
          context.move(to: CGPoint(x: rightX, y: topY - linewidthAngle / 2))
          context.addLine(to: CGPoint(x: rightX, y: topY + hAngle))
  
          // 右下角水平线
          context.move(to: CGPoint(x: rightX + linewidthAngle / 2, y: bottomY))
          context.addLine(to: CGPoint(x: rightX - wAngle, y: bottomY))
  
          // 右下角垂直线
          context.move(to: CGPoint(x: rightX, y: bottomY + linewidthAngle / 2))
          context.addLine(to: CGPoint(x: rightX, y: bottomY - hAngle))
          
          context.strokePath()
      }
      
      // 根据矩形区域获取识别区域
      static func getScanRectWithPreView(preView: UIView, style: LBXScanViewStyle) -> CGRect {
          let XRetangleLeft = style.xScanRetangleOffset
          let width = preView.frame.size.width - XRetangleLeft * 2
          let height = width
          var sizeRetangle = CGSize(width: width, height: height)
  
          if style.whRatio != 1 {
              let w = sizeRetangle.width
              var h = w / style.whRatio
  
              let hInt: Int = Int(h)
              h = CGFloat(hInt)
  
              sizeRetangle = CGSize(width: w, height: h)
          }
  
          // 扫码区域Y轴最小坐标
          let YMinRetangle = preView.frame.size.height / 2.0 - sizeRetangle.height / 2.0 - style.centerUpOffset
          // 扫码区域坐标
          let cropRect = CGRect(x: XRetangleLeft, y: YMinRetangle, width: sizeRetangle.width, height: sizeRetangle.height)
  
          // 计算兴趣区域
          var rectOfInterest: CGRect
  
          // ref:http://www.cocoachina.com/ios/20141225/10763.html
          let size = preView.bounds.size
          let p1 = size.height / size.width
  
          let p2: CGFloat = 1920.0 / 1080.0 // 使用了1080p的图像输出
          if p1 < p2 {
              let fixHeight = size.width * 1920.0 / 1080.0
              let fixPadding = (fixHeight - size.height) / 2
              rectOfInterest = CGRect(x: (cropRect.origin.y + fixPadding) / fixHeight,
                                      y: cropRect.origin.x / size.width,
                                      width: cropRect.size.height / fixHeight,
                                      height: cropRect.size.width / size.width)
  
          } else {
              let fixWidth = size.height * 1080.0 / 1920.0
              let fixPadding = (fixWidth - size.width) / 2
              rectOfInterest = CGRect(x: cropRect.origin.y / size.height,
                                      y: (cropRect.origin.x + fixPadding) / fixWidth,
                                      width: cropRect.size.height / size.height,
                                      height: cropRect.size.width / fixWidth)
          }
  
          return rectOfInterest
      }
      
      func getRetangeSize() -> CGSize {
          let XRetangleLeft = viewStyle.xScanRetangleOffset
          var sizeRetangle = CGSize(width: frame.size.width - XRetangleLeft * 2, height: frame.size.width - XRetangleLeft * 2)
  
          let w = sizeRetangle.width
          var h = w / viewStyle.whRatio
          h = CGFloat(Int(h))
          sizeRetangle = CGSize(width: w, height: h)
  
          return sizeRetangle
      }
      
      func deviceStartReadying(readyStr: String) {
          let XRetangleLeft = viewStyle.xScanRetangleOffset
          let sizeRetangle = getRetangeSize()
  
          // 扫码区域Y轴最小坐标
          let YMinRetangle = frame.size.height / 2.0 - sizeRetangle.height / 2.0 - viewStyle.centerUpOffset
  
          // 设备启动状态提示
          if activityView == nil {
  //            activityView = UIActivityIndicatorView(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
  //
  //            activityView?.center = CGPoint(x: XRetangleLeft + sizeRetangle.width / 2 - 50, y: YMinRetangle + sizeRetangle.height / 2)
  //            activityView?.style = UIActivityIndicatorView.Style.whiteLarge
  //
  //            addSubview(activityView!)
  //
  //            let labelReadyRect = CGRect(x: activityView!.frame.origin.x + activityView!.frame.size.width + 10,
  //                                        y: activityView!.frame.origin.y,
  //                                        width: 100,
  //                                        height: 30)
  //            labelReadying = UILabel(frame: labelReadyRect)
  //            labelReadying?.text = readyStr
  //            labelReadying?.backgroundColor = UIColor.clear
  //            labelReadying?.textColor = UIColor.white
  //            labelReadying?.font = UIFont.systemFont(ofSize: 18.0)
  //            addSubview(labelReadying!)
          }
  
  //        addSubview(labelReadying!)
  //        activityView?.startAnimating()
      }
      
      func deviceStopReadying() {
          if activityView != nil {
              activityView?.stopAnimating()
              activityView?.removeFromSuperview()
              labelReadying?.removeFromSuperview()
  
              activityView = nil
              labelReadying = nil
          }
      }
  
  }
  
  //MARK: - 公开方法
  public extension LBXScanView {
      
      /// 获取扫描动画的Rect
      func getScanRectForAnimation() -> CGRect {
          let XRetangleLeft = viewStyle.xScanRetangleOffset
          var sizeRetangle = CGSize(width: frame.size.width - XRetangleLeft * 2,
                                    height: frame.size.width - XRetangleLeft * 2)
          
          if viewStyle.whRatio != 1 {
              let w = sizeRetangle.width
              let h = w / viewStyle.whRatio
              sizeRetangle = CGSize(width: w, height: CGFloat(Int(h)))
          }
          
          // 扫码区域Y轴最小坐标
          let YMinRetangle = frame.size.height / 2.0 - sizeRetangle.height / 2.0 - viewStyle.centerUpOffset
          // 扫码区域坐标
          let cropRect = CGRect(x: XRetangleLeft, y: YMinRetangle, width: sizeRetangle.width, height: sizeRetangle.height)
          
          return cropRect
      }
      
  }