From 395f608cee14cada9b92d1d04a79577c97a8fa5c Mon Sep 17 00:00:00 2001 From: Haik Aslanyan Date: Sun, 19 Apr 2020 16:47:53 +0400 Subject: [PATCH 1/4] changed pixelThreshold from constant to range --- Sources/Internal/RCConstants.swift | 2 +- Sources/Internal/RCError.swift | 3 +++ Sources/Internal/RCImageDecoder.swift | 6 +++--- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/Sources/Internal/RCConstants.swift b/Sources/Internal/RCConstants.swift index c8a9010..7706d2a 100644 --- a/Sources/Internal/RCConstants.swift +++ b/Sources/Internal/RCConstants.swift @@ -28,5 +28,5 @@ struct RCConstants { static let dotSizeScale: CGFloat = 0.08 static let dotPatterns: [CGFloat] = [6, 4, 2] static let dotPointRange = (Float(1.3)...Float(2.5)) - static let pixelThreshold = 180 + static let pixelThreshold = (70...180) } diff --git a/Sources/Internal/RCError.swift b/Sources/Internal/RCError.swift index 944a6a0..5855777 100644 --- a/Sources/Internal/RCError.swift +++ b/Sources/Internal/RCError.swift @@ -27,6 +27,7 @@ public enum RCError: String, LocalizedError { case longText case decoding case wrongImageSize + case wrongColors public var errorDescription: String? { switch self { @@ -38,6 +39,8 @@ public enum RCError: String, LocalizedError { return "Error decoding" case .wrongImageSize: return "Error decoding. Image width and height must be a equal" + case .wrongColors: + return "The tint colors should be in a range of \(RCConstants.pixelThreshold) grayscale with 1.0 alpha" } } } diff --git a/Sources/Internal/RCImageDecoder.swift b/Sources/Internal/RCImageDecoder.swift index a8f46cd..5a743f7 100644 --- a/Sources/Internal/RCImageDecoder.swift +++ b/Sources/Internal/RCImageDecoder.swift @@ -57,7 +57,7 @@ extension RCImageDecoder { let transform = calculateTransform(from: points) let mapper = RCPointMapper(transform: transform, size: size) let locations = mapper.map(points: calculateBitLocations()) - let bits = locations.map { data[Int($0.x), Int($0.y)] > RCConstants.pixelThreshold ? RCBit.zero : RCBit.one } + let bits = locations.map { RCConstants.pixelThreshold.contains(Int(data[Int($0.x), Int($0.y)])) ? RCBit.one : RCBit.zero } return bits } @@ -75,13 +75,13 @@ extension RCImageDecoder { private func scanControlPoint(for data: PixelContainer, region: (x: Int, y: Int), side: Side) throws -> CGPoint { func scan(region: (x: Int, y: Int, size: Int), data: PixelContainer, coordinate: (Int) -> (x: Int, y: Int), comparison: (PixelPattern, (x: Int, y: Int)) -> Bool) -> [PixelPattern] { - var lastPattern = PixelPattern(bit: data[region.x, region.y] > RCConstants.pixelThreshold ? RCBit.zero : RCBit.one, x: region.x, y: region.y, count: 0) + var lastPattern = PixelPattern(bit: RCConstants.pixelThreshold.contains(Int(data[region.x, region.y])) ? RCBit.one : RCBit.zero, x: region.x, y: region.y, count: 0) var pixelPatterns = [lastPattern] var count = 0 let maxSize = region.size * region.size while count < maxSize { let coordinate = coordinate(count) - let bit = data[coordinate.x, coordinate.y] > RCConstants.pixelThreshold ? RCBit.zero : RCBit.one + let bit = RCConstants.pixelThreshold.contains(Int(data[coordinate.x, coordinate.y])) ? RCBit.one : RCBit.zero if comparison(lastPattern, coordinate), lastPattern.bit == bit { lastPattern.count += 1 pixelPatterns[pixelPatterns.count - 1] = lastPattern From 5f88ff991e459b702acce2596bd33ab9058ead02 Mon Sep 17 00:00:00 2001 From: Haik Aslanyan Date: Sun, 19 Apr 2020 16:50:14 +0400 Subject: [PATCH 2/4] added color color validation for black background --- Sources/Public/RCCoder.swift | 10 ++++++++++ Sources/Public/RCImage.swift | 2 +- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/Sources/Public/RCCoder.swift b/Sources/Public/RCCoder.swift index 25d0c79..6579d3f 100644 --- a/Sources/Public/RCCoder.swift +++ b/Sources/Public/RCCoder.swift @@ -54,6 +54,16 @@ public extension RCCoder { func validate(_ text: String) -> Bool { configuration.validate(text) } + + func validateForBlackBackground(colors: [UIColor]) -> Bool { + colors.allSatisfy { color in + var white: CGFloat = 0 + var alpha: CGFloat = 0 + guard color.getWhite(&white, alpha: &alpha) else { return false } + guard alpha == 1.0 else { return false } + return RCConstants.pixelThreshold.contains(Int(white * 255)) + } + } } extension RCCoder { diff --git a/Sources/Public/RCImage.swift b/Sources/Public/RCImage.swift index 5dec568..8cb4f08 100644 --- a/Sources/Public/RCImage.swift +++ b/Sources/Public/RCImage.swift @@ -28,7 +28,7 @@ public struct RCImage { public var size = CGFloat(300) public var tintColors: [UIColor] = [.orange, .magenta] public var attachmentImage: UIImage? - public var isTransparent = false + public var isTransparent = true public var gradientType = GradientType.linear(angle: CGFloat.pi / 4) public var contentInsets = UIEdgeInsets(top: 8, left: 8, bottom: 8, right: 8) From 843df78755004efa8222c81c5457a2d2c61d9d1e Mon Sep 17 00:00:00 2001 From: Haik Aslanyan Date: Sun, 19 Apr 2020 16:51:02 +0400 Subject: [PATCH 3/4] updated example project --- Example/Example/Base.lproj/Main.storyboard | 2 +- Example/Example/ViewController.swift | 7 +------ 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/Example/Example/Base.lproj/Main.storyboard b/Example/Example/Base.lproj/Main.storyboard index f4a2b79..bbbc792 100644 --- a/Example/Example/Base.lproj/Main.storyboard +++ b/Example/Example/Base.lproj/Main.storyboard @@ -43,7 +43,7 @@ - + diff --git a/Example/Example/ViewController.swift b/Example/Example/ViewController.swift index 1831243..61d7aa3 100644 --- a/Example/Example/ViewController.swift +++ b/Example/Example/ViewController.swift @@ -30,17 +30,12 @@ class ViewController: UIViewController { @IBOutlet weak var messageLabel: UILabel! var coder = RCCoder() var image = RCImage(message: "") - let colors: [[UIColor]] = [[.orange, .gray], [.orange, .magenta], [.systemGreen, .systemBlue], [.orange, .brown], [.purple, .cyan]] + let colors: [[UIColor]] = [[.orange, .magenta], [.systemPink, .systemTeal], [.systemTeal, .systemGreen], [.orange, .brown], [.purple, .cyan]] var isScanningFromLibrary = false } extension ViewController { - override func viewDidLoad() { - super.viewDidLoad() - image.isTransparent = true - } - @IBAction func scanImage(_ sender: Any) { isScanningFromLibrary = true let vc = UIImagePickerController() From 795bed4c5d8640174b70ed30d67a50fab88cc269 Mon Sep 17 00:00:00 2001 From: Haik Aslanyan Date: Sun, 19 Apr 2020 16:51:02 +0400 Subject: [PATCH 4/4] updated to 1.1.1 version --- RoundCode.podspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/RoundCode.podspec b/RoundCode.podspec index ad2b283..ec8a173 100644 --- a/RoundCode.podspec +++ b/RoundCode.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = 'RoundCode' - s.version = '1.1.0' + s.version = '1.1.1' s.summary = 'Facebook messenger style custom barcode.' s.description = <<-DESC Encode and decode data into custom stylish barcode.