Guest User

Untitled

a guest
Oct 20th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.21 KB | None | 0 0
  1. /*
  2. Copyright (c) 2017 Zhiyu Zhu/朱智语
  3.  
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10.  
  11. The above copyright notice and this permission notice shall be included in all
  12. copies or substantial portions of the Software.
  13.  
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. SOFTWARE.
  21. */
  22.  
  23. #if canImport(UIKit)
  24. import UIKit
  25. extension UIColor {
  26. convenience init(rgb: Int) {
  27. self.init(red: CGFloat((rgb >> 16) & 0xFF) / 255,
  28. green: CGFloat((rgb >> 8) & 0xFF) / 255,
  29. blue: CGFloat(rgb & 0xFF) / 255,
  30. alpha: 1)
  31. }
  32. }
  33. #elseif canImport(AppKit)
  34. import AppKit
  35. #endif
  36.  
  37. #if canImport(WatchKit)
  38. import WatchKit
  39. #endif
  40.  
  41. #if canImport(CoreGraphics)
  42. extension CGColor {
  43. static func fromRGB(_ rgb: Int) -> CGColor! {
  44. #if canImport(UIKit)
  45. return UIColor(rgb: rgb).cgColor
  46. #else
  47. return nil
  48. #endif
  49. }
  50.  
  51. #if canImport(UIKit)
  52. static let clear: CGColor = UIColor.clear.cgColor
  53. #elseif canImport(AppKit)
  54. static let clear: CGColor = NSColor.clear.cgColor
  55. #endif
  56. }
  57.  
  58. struct QRCodeRenderer {
  59. private static func inContext(
  60. size: CGSize, _ action: (CGContext) -> Void
  61. ) -> CGImage? {
  62. #if canImport(UIKit)
  63. UIGraphicsBeginImageContext(size)
  64. defer { UIGraphicsEndImageContext() }
  65. guard let ctx = UIGraphicsGetCurrentContext() else {
  66. return nil
  67. }
  68. action(ctx)
  69. return UIGraphicsGetImageFromCurrentImageContext()?.cgImage
  70. #else
  71. return nil
  72. #endif
  73. }
  74.  
  75. static func generate(
  76. model: [[Bool]],
  77. size: CGSize = CGSize(width: 256, height: 256),
  78. colorDark: Int = 0x000000,
  79. colorLight: Int = 0xFFFFFF,
  80. errorCorrectLevel: QRErrorCorrectLevel = .H
  81. ) -> CGImage? {
  82. let count = model.count
  83. guard count > 0 else { return nil }
  84. let total = min(size.width, size.height)
  85. let side = total / CGFloat(count)
  86. let xOffset = (size.width - total) / 2
  87. let yOffset = (size.height - total) / 2
  88. let dark = CGColor.fromRGB(colorDark)!
  89. let light = CGColor.fromRGB(colorLight)!
  90.  
  91. return inContext(size: size) { context in
  92. context.setStrokeColor(.clear)
  93. context.setLineWidth(0)
  94. for x in 0..<count {
  95. for y in 0..<count {
  96. context.setFillColor(model[x][y] ? dark : light)
  97. context.fill(CGRect(x: xOffset + CGFloat(x) * side,
  98. y: yOffset + CGFloat(y) * side,
  99. width: side,
  100. height: side))
  101. }
  102. }
  103. }
  104. }
  105. }
  106.  
  107. extension QRCode {
  108. public var cgImage: CGImage! {
  109. guard let codes = imageCodes else { return nil }
  110. return QRCodeRenderer.generate(
  111. model: codes,
  112. size: size,
  113. colorDark: colorDark, colorLight: colorLight,
  114. errorCorrectLevel: correctLevel
  115. )
  116. }
  117.  
  118. #if canImport(UIKit)
  119. public var image: UIImage? {
  120. guard let cgImage = cgImage else { return nil }
  121. return UIImage(cgImage: cgImage)
  122. }
  123. #elseif canImport(AppKit)
  124. public var image: NSImage? {
  125. guard let cgImage = cgImage else { return nil }
  126. return NSImage(
  127. cgImage: cgImage,
  128. size: NSSize(width: size.width, height: size.height)
  129. )
  130. }
  131. #endif
  132. }
  133. #endif
Add Comment
Please, Sign In to add comment