EFQRCode.swift 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. //
  2. // EFQRCode.swift
  3. // EFQRCode
  4. //
  5. // Created by EyreFree on 2017/3/28.
  6. //
  7. // Copyright (c) 2017 EyreFree <eyrefree@eyrefree.org>
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining a copy
  10. // of this software and associated documentation files (the "Software"), to deal
  11. // in the Software without restriction, including without limitation the rights
  12. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. // copies of the Software, and to permit persons to whom the Software is
  14. // furnished to do so, subject to the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be included in
  17. // all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. // THE SOFTWARE.
  26. import Foundation
  27. import CoreGraphics
  28. #if os(iOS) || os(tvOS) || os(macOS)
  29. import CoreImage
  30. #endif
  31. @objcMembers
  32. public class EFQRCode: NSObject {
  33. // MARK: - Recognizer
  34. #if os(iOS) || os(tvOS) || os(macOS)
  35. public static func recognize(image: CGImage) -> [String]? {
  36. return EFQRCodeRecognizer(image: image).recognize()
  37. }
  38. #endif
  39. // MARK: - Generator
  40. public static func generate(
  41. content: String,
  42. size: EFIntSize = EFIntSize(width: 600, height: 600),
  43. backgroundColor: CGColor = .EFWhite(),
  44. foregroundColor: CGColor = .EFBlack(),
  45. watermark: CGImage? = nil,
  46. watermarkMode: EFWatermarkMode = .scaleAspectFill,
  47. inputCorrectionLevel: EFInputCorrectionLevel = .h,
  48. icon: CGImage? = nil,
  49. iconSize: EFIntSize? = nil,
  50. allowTransparent: Bool = true,
  51. pointShape: EFPointShape = .square,
  52. mode: EFQRCodeMode = .none,
  53. binarizationThreshold: CGFloat = 0.5,
  54. magnification: EFIntSize? = nil,
  55. foregroundPointOffset: CGFloat = 0
  56. ) -> CGImage? {
  57. let generator = EFQRCodeGenerator(content: content, size: size)
  58. generator.setWatermark(watermark: watermark, mode: watermarkMode)
  59. generator.setColors(backgroundColor: backgroundColor, foregroundColor: foregroundColor)
  60. generator.setInputCorrectionLevel(inputCorrectionLevel: inputCorrectionLevel)
  61. generator.setIcon(icon: icon, size: iconSize ?? EFIntSize(width: size.width / 5, height: size.height / 5))
  62. generator.setAllowTransparent(allowTransparent: allowTransparent)
  63. generator.setPointShape(pointShape: pointShape)
  64. generator.setMode(mode: mode)
  65. generator.setBinarizationThreshold(binarizationThreshold: binarizationThreshold)
  66. generator.setMagnification(magnification: magnification)
  67. generator.setForegroundPointOffset(foregroundPointOffset: foregroundPointOffset)
  68. return generator.generate()
  69. }
  70. public static func generateWithGIF(
  71. content: String,
  72. size: EFIntSize = EFIntSize(width: 600, height: 600),
  73. backgroundColor: CGColor = .EFWhite(),
  74. foregroundColor: CGColor = .EFBlack(),
  75. watermark: Data,
  76. watermarkMode: EFWatermarkMode = .scaleAspectFill,
  77. inputCorrectionLevel: EFInputCorrectionLevel = .h,
  78. icon: CGImage? = nil,
  79. iconSize: EFIntSize? = nil,
  80. allowTransparent: Bool = true,
  81. pointShape: EFPointShape = .square,
  82. mode: EFQRCodeMode = .none,
  83. binarizationThreshold: CGFloat = 0.5,
  84. magnification: EFIntSize? = nil,
  85. foregroundPointOffset: CGFloat = 0
  86. ) -> Data? {
  87. let generator = EFQRCodeGenerator(content: content, size: size)
  88. generator.setWatermark(watermark: nil, mode: watermarkMode)
  89. generator.setColors(backgroundColor: backgroundColor, foregroundColor: foregroundColor)
  90. generator.setInputCorrectionLevel(inputCorrectionLevel: inputCorrectionLevel)
  91. generator.setIcon(icon: icon, size: iconSize ?? EFIntSize(width: size.width / 5, height: size.height / 5))
  92. generator.setAllowTransparent(allowTransparent: allowTransparent)
  93. generator.setPointShape(pointShape: pointShape)
  94. generator.setMode(mode: mode)
  95. generator.setBinarizationThreshold(binarizationThreshold: binarizationThreshold)
  96. generator.setMagnification(magnification: magnification)
  97. generator.setForegroundPointOffset(foregroundPointOffset: foregroundPointOffset)
  98. return EFQRCode.generateWithGIF(data: watermark, generator: generator)
  99. }
  100. }