|
| 1 | +// |
| 2 | +// QRCode+Conveniences.swift |
| 3 | +// |
| 4 | +// Copyright © 2023 Darren Ford. All rights reserved. |
| 5 | +// |
| 6 | +// MIT license |
| 7 | +// |
| 8 | +// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated |
| 9 | +// documentation files (the "Software"), to deal in the Software without restriction, including without limitation the |
| 10 | +// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to |
| 11 | +// permit persons to whom the Software is furnished to do so, subject to the following conditions: |
| 12 | +// |
| 13 | +// The above copyright notice and this permission notice shall be included in all copies or substantial |
| 14 | +// portions of the Software. |
| 15 | +// |
| 16 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE |
| 17 | +// WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS |
| 18 | +// OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 19 | +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 20 | +// |
| 21 | + |
| 22 | +// Some conveniences for creating and detecting QR codes |
| 23 | + |
| 24 | +import Foundation |
| 25 | +import CoreGraphics |
| 26 | + |
| 27 | +public extension CGImage { |
| 28 | + /// Create a CGImage containing a qr code |
| 29 | + /// - Parameters: |
| 30 | + /// - text: The text to encode in the qr code |
| 31 | + /// - dimension: The size in pixels of the output image |
| 32 | + /// - errorCorrection: The error correction |
| 33 | + /// - shape: The shape to use, or nil for default |
| 34 | + /// - style: The style to use, or nil for default |
| 35 | + /// - Returns: The image representation of the qr code, or nil if an error occurred |
| 36 | + static func qrCode( |
| 37 | + _ text: String, |
| 38 | + dimension: Int, |
| 39 | + errorCorrection: QRCode.ErrorCorrection = .high, |
| 40 | + shape: QRCode.Shape? = nil, |
| 41 | + style: QRCode.Style? = nil |
| 42 | + ) -> CGImage? { |
| 43 | + let doc = QRCode.Document(utf8String: text, errorCorrection: errorCorrection) |
| 44 | + if let shape = shape { doc.design.shape = shape } |
| 45 | + if let style = style { doc.design.style = style } |
| 46 | + return doc.cgImage(dimension: dimension) |
| 47 | + } |
| 48 | + |
| 49 | + /// Create a CGImage containing a qr code |
| 50 | + /// - Parameters: |
| 51 | + /// - text: The text to encode in the qr code |
| 52 | + /// - dimension: The size in pixels of the output image |
| 53 | + /// - foregroundColor: The foreground color |
| 54 | + /// - foregroundColor: The background color, or nil for default |
| 55 | + /// - errorCorrection: The error correction |
| 56 | + /// - Returns: The image representation of the qr code, or nil if an error occurred |
| 57 | + static func qrCode( |
| 58 | + _ text: String, |
| 59 | + dimension: Int, |
| 60 | + foregroundColor: CGColor, |
| 61 | + backgroundColor: CGColor? = nil, |
| 62 | + errorCorrection: QRCode.ErrorCorrection = .high, |
| 63 | + shape: QRCode.Style? = nil |
| 64 | + ) -> CGImage? { |
| 65 | + let doc = QRCode.Document(utf8String: text, errorCorrection: errorCorrection) |
| 66 | + doc.design.foregroundColor(foregroundColor) |
| 67 | + doc.design.backgroundColor(backgroundColor) |
| 68 | + return doc.cgImage(dimension: dimension) |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +public extension CGPath { |
| 73 | + /// Simple convenience for creating a CGPath representation of a qr code |
| 74 | + /// - Parameters: |
| 75 | + /// - text: The text to encode in the qr code |
| 76 | + /// - dimension: The size in pixels of the output image |
| 77 | + /// - errorCorrection: The error correction |
| 78 | + /// - shape: The shape to use, or nil for default |
| 79 | + /// - Returns: The path representation of the qr code, or nil if an error occurred |
| 80 | + static func qrCode( |
| 81 | + _ text: String, |
| 82 | + dimension: Int, |
| 83 | + errorCorrection: QRCode.ErrorCorrection = .high, |
| 84 | + shape: QRCode.Shape? = nil |
| 85 | + ) -> CGPath? { |
| 86 | + let doc = QRCode.Document(utf8String: text, errorCorrection: errorCorrection) |
| 87 | + if let shape = shape { doc.design.shape = shape } |
| 88 | + return doc.path(dimension: dimension) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +#if !os(watchOS) |
| 93 | +public extension CGImage { |
| 94 | + /// Returns all qrcode messages that were encoded in this image |
| 95 | + /// - Returns: An array of detected qr code strings |
| 96 | + func qrCodedMessages() -> [String] { |
| 97 | + let features = QRCode.DetectQRCodes(self) |
| 98 | + return features.compactMap { $0.messageString } |
| 99 | + } |
| 100 | +} |
| 101 | +#endif |
0 commit comments