-
Notifications
You must be signed in to change notification settings - Fork 0
/
QRGenerator.swift
92 lines (73 loc) · 3.72 KB
/
QRGenerator.swift
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
import Cocoa
import Foundation
struct QRGenerator {
static func create(text: String, background: QRCode.BackgroundColor, color: QRCode.PointColor, size: CGFloat, correction: QRCode.Quality) -> NSImage? {
guard
let background = createBackgroundImage(color: background, size: size),
let pixel = createForegroundImage(text: text, color: color, correction: correction, size: size) else {
return nil
}
// Merge back and foreground
let result = background.merge(with: pixel)
// Make sure the result image has the output size
return result?.resize(w: size, h: size)
}
static func createBackgroundImage(color: QRCode.BackgroundColor, size: CGFloat) -> NSImage? {
// create a view with of desired size
let view = NSView(frame: NSRect(origin: .zero, size: CGSize(width: size, height: size)))
// apply the color
view.layer?.backgroundColor = color.start.cgColor
// draw the background
let image = NSImage(size: NSSize(width: size, height: size))
image.lockFocus()
color.start.drawSwatch(in: NSRect(origin: .zero, size: CGSize(width: size, height: size)))
image.unlockFocus()
// If there's an end color, it means, a gradient is required.
if let backgroundEndColor = color.end {
return image.maskWithGradient(start: color.start, end: backgroundEndColor)
}
// If not gradient is required, return the image with the solid background
return image
}
static func createForegroundImage(text: String, color: QRCode.PointColor, correction: QRCode.Quality, size: CGFloat) -> NSImage? {
// Create the points of the QR code that will hold the encoded string
guard let qr = generateQrImage(from: text, pixelColor: color.start, correction: correction) else {
return nil
}
// scale the image keeping the original ratio.
let ciImageSize = qr.extent.size
let widthRatio = size / ciImageSize.width
let transform = CGAffineTransform(scaleX: widthRatio, y: widthRatio)
let resizedQr = qr.transformed(by: transform)
// create an image representation to be able to
let rep = NSCIImageRep(ciImage: resizedQr)
let image = NSImage(size: rep.size)
image.addRepresentation(rep)
// if there's an end color, it means a gradient mask is required
if let pointEndColor = color.end {
return image.maskWithGradient(start: color.start, end: pointEndColor)
}
return image
}
static func generateQrImage(from string: String, pixelColor: NSColor, correction: QRCode.Quality) -> CIImage? {
// create the CIFilter
guard let qrFilter = CIFilter(name: "CIQRCodeGenerator") else {
return nil
}
// encode the data
let qrData = string.data(using: String.Encoding.utf8)
qrFilter.setDefaults()
qrFilter.setValue(qrData, forKey: "inputMessage")
qrFilter.setValue(correction.singleLetter, forKey: "inputCorrectionLevel")
// use the filter CIFalseColor to give color to the pixels
guard let colorFilter = CIFilter(name: "CIFalseColor") else {
return nil
}
// set the color values to the qr color filter
colorFilter.setDefaults()
colorFilter.setValue(qrFilter.outputImage, forKey: "inputImage")
colorFilter.setValue(CIColor(color: pixelColor), forKey: "inputColor0")
colorFilter.setValue(CIColor(color: .clear), forKey: "inputColor1")
return colorFilter.outputImage
}
}