Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 118dcd6752 |
@@ -64,7 +64,5 @@
|
|||||||
<string>UIInterfaceOrientationLandscapeLeft</string>
|
<string>UIInterfaceOrientationLandscapeLeft</string>
|
||||||
<string>UIInterfaceOrientationLandscapeRight</string>
|
<string>UIInterfaceOrientationLandscapeRight</string>
|
||||||
</array>
|
</array>
|
||||||
<key>UIUserInterfaceStyle</key>
|
|
||||||
<string>Light</string>
|
|
||||||
</dict>
|
</dict>
|
||||||
</plist>
|
</plist>
|
||||||
|
|||||||
@@ -75,7 +75,6 @@ extension ViewController {
|
|||||||
|
|
||||||
@IBAction func scan(_ sender: Any) {
|
@IBAction func scan(_ sender: Any) {
|
||||||
let vc = RCCameraViewController()
|
let vc = RCCameraViewController()
|
||||||
coder.scanningMode = .lightBackground
|
|
||||||
vc.coder = coder
|
vc.coder = coder
|
||||||
vc.delegate = self
|
vc.delegate = self
|
||||||
present(vc, animated: true)
|
present(vc, animated: true)
|
||||||
@@ -83,10 +82,12 @@ extension ViewController {
|
|||||||
|
|
||||||
@IBAction func share(_ sender: Any) {
|
@IBAction func share(_ sender: Any) {
|
||||||
image.size = 1000
|
image.size = 1000
|
||||||
|
image.isTransparent = false
|
||||||
image.contentInsets = UIEdgeInsets(top: 50, left: 50, bottom: 50, right: 50)
|
image.contentInsets = UIEdgeInsets(top: 50, left: 50, bottom: 50, right: 50)
|
||||||
let uiImage = try? coder.encode(image)
|
let uiImage = try? coder.encode(image)
|
||||||
image.size = 300
|
image.size = 300
|
||||||
image.contentInsets = UIEdgeInsets(top: 8, left: 8, bottom: 8, right: 8)
|
image.contentInsets = UIEdgeInsets(top: 8, left: 8, bottom: 8, right: 8)
|
||||||
|
image.isTransparent = true
|
||||||
let vc = UIActivityViewController.init(activityItems: [uiImage!], applicationActivities: nil)
|
let vc = UIActivityViewController.init(activityItems: [uiImage!], applicationActivities: nil)
|
||||||
present(vc, animated: true)
|
present(vc, animated: true)
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
Pod::Spec.new do |s|
|
Pod::Spec.new do |s|
|
||||||
s.name = 'RoundCode'
|
s.name = 'RoundCode'
|
||||||
s.version = '1.3.0'
|
s.version = '1.2.0'
|
||||||
s.summary = 'Facebook messenger style custom barcode.'
|
s.summary = 'Facebook messenger style custom barcode.'
|
||||||
s.description = <<-DESC
|
s.description = <<-DESC
|
||||||
Encode and decode data into custom stylish barcode.
|
Encode and decode data into custom stylish barcode.
|
||||||
|
|||||||
@@ -28,6 +28,5 @@ struct RCConstants {
|
|||||||
static let dotSizeScale: CGFloat = 0.08
|
static let dotSizeScale: CGFloat = 0.08
|
||||||
static let dotPatterns: [CGFloat] = [6, 4, 2]
|
static let dotPatterns: [CGFloat] = [6, 4, 2]
|
||||||
static let dotPointRange = (Float(1.3)...Float(2.5))
|
static let dotPointRange = (Float(1.3)...Float(2.5))
|
||||||
static let lightBackgroundRange = UInt8(0)...UInt8(180)
|
static let pixelThreshold = (UInt8(0)...UInt8(180))
|
||||||
static let darkBackgroundRange = UInt8(100)...UInt8(255)
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,6 +27,7 @@ public enum RCError: String, LocalizedError {
|
|||||||
case longText
|
case longText
|
||||||
case decoding
|
case decoding
|
||||||
case wrongImageSize
|
case wrongImageSize
|
||||||
|
case wrongColors
|
||||||
|
|
||||||
public var errorDescription: String? {
|
public var errorDescription: String? {
|
||||||
switch self {
|
switch self {
|
||||||
@@ -38,6 +39,8 @@ public enum RCError: String, LocalizedError {
|
|||||||
return "Error decoding"
|
return "Error decoding"
|
||||||
case .wrongImageSize:
|
case .wrongImageSize:
|
||||||
return "Error decoding. Image width and height must be a equal"
|
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"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,7 +27,6 @@ struct RCImageDecoder {
|
|||||||
internal let configuration: RCCoderConfiguration
|
internal let configuration: RCCoderConfiguration
|
||||||
internal var size = 720
|
internal var size = 720
|
||||||
internal var bytesPerRow = 720
|
internal var bytesPerRow = 720
|
||||||
internal var pixelThreshold = RCConstants.lightBackgroundRange
|
|
||||||
}
|
}
|
||||||
|
|
||||||
extension RCImageDecoder {
|
extension RCImageDecoder {
|
||||||
@@ -38,7 +37,7 @@ extension RCImageDecoder {
|
|||||||
let transform = calculateTransform(from: points)
|
let transform = calculateTransform(from: points)
|
||||||
let mapper = RCPointMapper(transform: transform, size: size)
|
let mapper = RCPointMapper(transform: transform, size: size)
|
||||||
let locations = mapper.map(points: calculateBitLocations())
|
let locations = mapper.map(points: calculateBitLocations())
|
||||||
let bits = locations.map { pixelThreshold.contains(data[Int($0.x), Int($0.y)]) ? RCBit.one : RCBit.zero }
|
let bits = locations.map { RCConstants.pixelThreshold.contains(data[Int($0.x), Int($0.y)]) ? RCBit.one : RCBit.zero }
|
||||||
return bits
|
return bits
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -76,7 +75,7 @@ extension RCImageDecoder {
|
|||||||
|
|
||||||
|
|
||||||
private func scanPixelPattern(for mode: ScanMode, data: PixelContainer) -> [PixelPattern] {
|
private func scanPixelPattern(for mode: ScanMode, data: PixelContainer) -> [PixelPattern] {
|
||||||
var lastPattern = PixelPattern.init(bit: pixelThreshold.contains((data[0, 0])) ? RCBit.one : RCBit.zero, x: 0, y: 0, count: 0)
|
var lastPattern = PixelPattern.init(bit: RCConstants.pixelThreshold.contains((data[0, 0])) ? RCBit.one : RCBit.zero, x: 0, y: 0, count: 0)
|
||||||
var pixelPatterns = [lastPattern]
|
var pixelPatterns = [lastPattern]
|
||||||
var count = 0
|
var count = 0
|
||||||
let maxSize = size * size
|
let maxSize = size * size
|
||||||
@@ -85,7 +84,7 @@ extension RCImageDecoder {
|
|||||||
while count < maxSize {
|
while count < maxSize {
|
||||||
let x = count % size
|
let x = count % size
|
||||||
let y = count / size
|
let y = count / size
|
||||||
let bit = pixelThreshold.contains(data[x, y]) ? RCBit.one : RCBit.zero
|
let bit = RCConstants.pixelThreshold.contains(data[x, y]) ? RCBit.one : RCBit.zero
|
||||||
if lastPattern.y == y, lastPattern.bit == bit {
|
if lastPattern.y == y, lastPattern.bit == bit {
|
||||||
lastPattern.count += 1
|
lastPattern.count += 1
|
||||||
pixelPatterns[pixelPatterns.count - 1] = lastPattern
|
pixelPatterns[pixelPatterns.count - 1] = lastPattern
|
||||||
@@ -99,7 +98,7 @@ extension RCImageDecoder {
|
|||||||
while count < maxSize {
|
while count < maxSize {
|
||||||
let x = count / size
|
let x = count / size
|
||||||
let y = count % size
|
let y = count % size
|
||||||
let bit = pixelThreshold.contains(data[x, y]) ? RCBit.one : RCBit.zero
|
let bit = RCConstants.pixelThreshold.contains(data[x, y]) ? RCBit.one : RCBit.zero
|
||||||
if lastPattern.x == x, lastPattern.bit == bit {
|
if lastPattern.x == x, lastPattern.bit == bit {
|
||||||
lastPattern.count += 1
|
lastPattern.count += 1
|
||||||
pixelPatterns[pixelPatterns.count - 1] = lastPattern
|
pixelPatterns[pixelPatterns.count - 1] = lastPattern
|
||||||
|
|||||||
@@ -160,7 +160,7 @@ extension RCCameraViewController {
|
|||||||
input.device.activeVideoMaxFrameDuration = CMTimeMake(value: 1, timescale: 30)
|
input.device.activeVideoMaxFrameDuration = CMTimeMake(value: 1, timescale: 30)
|
||||||
captureSession.addInput(input)
|
captureSession.addInput(input)
|
||||||
let videoOutput = AVCaptureVideoDataOutput()
|
let videoOutput = AVCaptureVideoDataOutput()
|
||||||
videoOutput.setSampleBufferDelegate(self, queue: DispatchQueue.global(qos: .userInteractive))
|
videoOutput.setSampleBufferDelegate(self, queue: .main)
|
||||||
videoOutput.videoSettings = [kCVPixelBufferPixelFormatTypeKey as String: NSNumber(value: kCVPixelFormatType_420YpCbCr8BiPlanarFullRange)]
|
videoOutput.videoSettings = [kCVPixelBufferPixelFormatTypeKey as String: NSNumber(value: kCVPixelFormatType_420YpCbCr8BiPlanarFullRange)]
|
||||||
captureSession.addOutput(videoOutput)
|
captureSession.addOutput(videoOutput)
|
||||||
} catch {
|
} catch {
|
||||||
@@ -197,10 +197,8 @@ extension RCCameraViewController: AVCaptureVideoDataOutputSampleBufferDelegate {
|
|||||||
coder.imageDecoder.bytesPerRow = bytesPerRow
|
coder.imageDecoder.bytesPerRow = bytesPerRow
|
||||||
if let message = try? coder.decode(buffer: lumaCopy.assumingMemoryBound(to: UInt8.self)) {
|
if let message = try? coder.decode(buffer: lumaCopy.assumingMemoryBound(to: UInt8.self)) {
|
||||||
captureSession.stopRunning()
|
captureSession.stopRunning()
|
||||||
DispatchQueue.main.async {[weak self] in
|
delegate?.cameraViewController(didFinishScanning: message)
|
||||||
self?.delegate?.cameraViewController(didFinishScanning: message)
|
dismiss(animated: true)
|
||||||
self?.dismiss(animated: true)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
lumaCopy.deallocate()
|
lumaCopy.deallocate()
|
||||||
CVPixelBufferUnlockBaseAddress(pixelBuffer, .readOnly)
|
CVPixelBufferUnlockBaseAddress(pixelBuffer, .readOnly)
|
||||||
|
|||||||
@@ -25,11 +25,6 @@ import UIKit
|
|||||||
public final class RCCoder {
|
public final class RCCoder {
|
||||||
|
|
||||||
public let configuration: RCCoderConfiguration
|
public let configuration: RCCoderConfiguration
|
||||||
public var scanningMode = ScanningMode.lightBackground {
|
|
||||||
didSet {
|
|
||||||
imageDecoder.pixelThreshold = scanningMode == .lightBackground ? RCConstants.lightBackgroundRange : RCConstants.darkBackgroundRange
|
|
||||||
}
|
|
||||||
}
|
|
||||||
internal lazy var imageDecoder = RCImageDecoder(configuration: self.configuration)
|
internal lazy var imageDecoder = RCImageDecoder(configuration: self.configuration)
|
||||||
internal lazy var imageEncoder = RCImageEncoder(configuration: self.configuration)
|
internal lazy var imageEncoder = RCImageEncoder(configuration: self.configuration)
|
||||||
internal lazy var bitCoder = RCBitCoder(configuration: self.configuration)
|
internal lazy var bitCoder = RCBitCoder(configuration: self.configuration)
|
||||||
@@ -58,12 +53,15 @@ public extension RCCoder {
|
|||||||
func validate(_ text: String) -> Bool {
|
func validate(_ text: String) -> Bool {
|
||||||
configuration.validate(text)
|
configuration.validate(text)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public extension RCCoder {
|
func validateForBlackBackground(colors: [UIColor]) -> Bool {
|
||||||
enum ScanningMode {
|
colors.allSatisfy { color in
|
||||||
case lightBackground
|
var white: CGFloat = 0
|
||||||
case darkBackground
|
var alpha: CGFloat = 0
|
||||||
|
guard color.getWhite(&white, alpha: &alpha) else { return false }
|
||||||
|
guard alpha == 1.0 else { return false }
|
||||||
|
return RCConstants.pixelThreshold.contains(UInt8(white * 255))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -75,4 +73,3 @@ extension RCCoder {
|
|||||||
return message
|
return message
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user