Created
December 30, 2017 00:43
-
-
Save AmatsuZero/d79eb1170423bfe2827d9e7f35faeac7 to your computer and use it in GitHub Desktop.
Generate Terminal Printable QRCode by Swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
enum ANSIColor: UInt8 { | |
enum Platte: String { | |
case WHITE_ALL = "\u{2588}" | |
case WHITE_BLACK = "\u{2580}" | |
case BLACK_WHITE = "\u{2584}" | |
case BLACK_ALL = "" | |
} | |
case Black = 30, Red, Green, Yellow, Blue, Magenta, Cyan, White | |
case BackgroundBlack = 40, BackgroundRed, BackgroundGreen, BackgroundYellow, BackgroundBlue, BackgroundMagenta, BackgroundCyan, BackgroundWhite | |
case BrightBlack = 90, BrightRed, BrightGreen, BrightYellow, BrightBlue, BrightMagenta, BrightCyan, BrightWhite | |
private static let Reset = "\u{001B}[0m" | |
private var color: String { | |
return "\u{001B}[\(rawValue)m" | |
} | |
private var bgColor: String { | |
return "\u{001B}[\(rawValue+10)m" | |
} | |
var fgOutput: String { | |
return "\(color) \(ANSIColor.Reset)" | |
} | |
var bgOutput: String { | |
return "\(bgColor) \(ANSIColor.Reset)" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import AppKit | |
extension String { | |
func createQRCodeANSI(isSmall small: Bool = false, | |
background backgroundColor: ANSIColor = .White, | |
foreground foregroundColor: ANSIColor = .Black) -> String? { | |
guard let filter = CIFilter(name: "CIQRCodeGenerator") else { | |
return nil | |
} | |
filter.setDefaults() | |
guard let data = data(using: .utf8) else { | |
return nil | |
} | |
filter.setValue(data, forKey: "inputMessage") | |
var outputImg = filter.outputImage | |
let colorFilter = CIFilter(name: "CIFalseColor") | |
colorFilter?.setDefaults() | |
colorFilter?.setValue(outputImg, forKey: "inputImage") | |
colorFilter?.setValue(CIColor(color: .white), forKey: "inputColor0") | |
colorFilter?.setValue(CIColor(color: .black), forKey: "inputColor1") | |
outputImg = colorFilter?.outputImage | |
guard let ciImage = outputImg else { | |
return nil | |
} | |
let context = CIContext() | |
guard let cgImage = context.createCGImage(ciImage, from: ciImage.extent) else { | |
return nil | |
} | |
let bmpImgRef = NSBitmapImageRep(cgImage: cgImage) | |
let w = bmpImgRef.pixelsWide | |
let h = bmpImgRef.pixelsHigh | |
let blackColor = NSColor.black.usingColorSpace(.genericRGB) | |
var result = "" | |
for x in 0..<w { | |
for y in 0..<h { | |
if let color = bmpImgRef.colorAt(x: x, y: y) { | |
if color.usingColorSpace(.genericRGB) == blackColor { | |
result += backgroundColor.bgOutput | |
} else { | |
result += foregroundColor.bgOutput | |
} | |
} | |
} | |
result += "\n" | |
} | |
return result | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment