I have long int number and I want to encode it to QRCode with exactly numeric type of encoding. Now I use CIFilter(name: "CIQRCodeGenerator"), but in this case I don't know how to choose type of encoding. Is there a way to create QR code with chosen type of encoding?
Asked
Active
Viewed 1,759 times
1
-
please check this url: https://stackoverflow.com/questions/48944184/swift-generate-a-qrcode/48945637#48945637 – aBilal17 Mar 21 '18 at 11:01
2 Answers
0
The Apple documentation for CIQRCodeGenerator indicates that you should use NSISOLatin1StringEncoding
(Objective-C) or String.Encoding.isoLatin1
(Swift) for encoding QR codes.
Here is a quick example for generating a UIImage from a given input integer in Swift 4.
enum QRInputEncoding: Int {
case octal = 8
case decimal = 10
case hexadecimal = 16
}
func makeQRImage(input: Int, encoding: QRInputEncoding) -> UIImage? {
let encoded = String(input, radix: encoding.rawValue)
print(encoded)
guard let data = encoded.data(using: .isoLatin1) else {
return nil
}
let filter = CIFilter(name: "CIQRCodeGenerator", withInputParameters: ["inputMessage": data])
return filter?.outputImage.map(UIImage.init(ciImage:))
}
makeQRImage(input: 100000, encoding: .octal) // prints 303240
makeQRImage(input: 100000, encoding: .decimal) // prints 100000
makeQRImage(input: 100000, encoding: .hexadecimal) // prints 186a0
This allows you to change the generated QR code output so that readers can read less characters in, resulting in a smaller QR code for the same number.

Jake
- 545
- 2
- 6
-
Now I do the same. But I am not sure what type of encoding use in this case, numeric or alphabetic or 8-bit. – FuzzzzyBoy Mar 21 '18 at 10:48
-
I have edited my answer to allow customising the output encoding of the number passed in for your QR code. The previous example only output decimal numbers, now base 8 and 16 are options as well. – Jake Mar 21 '18 at 11:18
0
This will generate QRCode image and return
func makeQRImage(inputText: String) -> UIImage {
let data = inputText.dataUsingEncoding(NSISOLatin1StringEncoding, allowLossyConversion: false)
let filter = CIFilter(name: "CIQRCodeGenerator")
filter.setValue(data, forKey: "inputMessage")
filter.setValue("Q", forKey: "inputCorrectionLevel")
return filter.outputImage
}
Anything else you can follow this code: https://github.com/appcoda/QR-Code-Generator
Also the tutorial : https://www.appcoda.com/qr-code-generator-tutorial

Faysal Ahmed
- 7,501
- 5
- 28
- 50