QRgen
QRgen copied to clipboard
A QR code generation library.
QRgen - A QR generation library
QRgen is a QR generation library fully written in Nim that only uses a small amount of pure-nim stdlib modules.
Prerequisites
nim --version
>= 1.6.0
Installation
nimble install qrgen
Features
- Supports all QR versions: from
1
to40
. - Supports all EC (Error Correction) levels:
L
,M
,Q
andH
. - Supports
numeric mode
,alphanumeric mode
andbyte mode
. - Supports printing a QR code on your terminal via standard output.
- Supports printing a QR code to SVG, with custom colors, using circles, embedding SVG logos etc.
- Supports rendering a QR code to pixie's
Image
, with the same features as SVG (but can embed more image formats).Image
can be exported to various formats, like PNG.
Usage
import QRgen
let myQR = newQR("https://github.com/aruZeta/QRgen")
Terminal
myQR.printTerminal
SVG
Generic QR with white background and black foreground
myQR.printSvg
Changing the background and foreground colors
myQR.printSvg("#1d2021","#98971a")
"#1d2021"
sets the "light" or "background" color.
"#98971a"
sets the "dark" or "foreground" color.
Making the alignment patterns rounded
myQR.printSvg("#1d2021","#98971a",60)
60
sets the alignment patterns' roundness to 60%.
Making the modules rounded
myQR.printSvg("#1d2021","#98971a",100,100)
The first 100
sets the alignment patterns' roundness to 100%
while the second 100
sets the module's roundness to 100% too.
Changing the separation of the modules
myQR.printSvg("#1d2021","#98971a",100,100,25)
The last 25
sets the module's separation to 25%.
Embedding another SVG in the generated QR code
Here we will need the highest EC level, for a better result (bigger logo):
let myQR = newQR("https://github.com/aruZeta/QRgen", ecLevel=qrECH)
myQR.printSvg("#1d2021","#98971a",100,100,25,svgImg=readFile("QRgen-logo.svg"))
svgImg
adds an SVG image embed in the center of generated
QR code, so we can pass it the contents of an SVG file, here a logo, and
the result as you can see is the actual QRgen logo.
Since the generated SVGs have css classes, we can do stuff like this:
https://user-images.githubusercontent.com/68018085/190470749-66090814-08fe-45b5-881d-a96b272374be.mp4
https://user-images.githubusercontent.com/68018085/190470760-8a5b5a30-5812-4777-8e05-8d2b250a9113.mp4
PNG
Note: The PNG renderer is not exported with QRgen
since it depends on pixie
(check pixie here). To use it you will need
to add this import:
import QRgen/renderer
And obviously install and import pixie
too.
Note: renderImg
returns an Image
which to the save as let's say a PNG, you
will need to do:
let myQRImg = renderImg(...)
writeFile(myQRImg, "path/to/save/it.png")
You can check pixie to learn about more
formats you can save an Image
as.
Generic QR with white background and black foreground
myQR.renderImg
Changing the background and foreground colors
myQR.renderImg("#1d2021","#98971a")
"#1d2021"
sets the "light" or "background" color.
"#98971a"
sets the "dark" or "foreground" color.
Making the alignment patterns rounded
myQR.renderImg("#1d2021","#98971a",60)
60
sets the alignment patterns' roundness to 60%.
Making the modules rounded
myQR.renderImg("#1d2021","#98971a",100,100)
The first 100
sets the alignment patterns' roundness to 100%
while the second 100
sets the module's roundness to 100% too.
Changing the separation of the modules
myQR.renderImg("#1d2021","#98971a",100,100,25)
The last 25
sets the module's separation to 25%.
Embedding another PNG in the generated QR code
Here we will need the highest EC level, for a better result (bigger logo):
let myQR = newQR("https://github.com/aruZeta/QRgen", ecLevel=qrECH)
myQR.renderImg("#1d2021","#98971a",100,100,25,img=readImage("QRgen-logo.png"))
img
embeds an Image
in the center of the generated QR code,
so we can use pixie's readImage
to read a PNG file, here a logo,
and the result as you can see is the actual QRgen logo.
Note that you can change the resolution of the generated image by setting
pixels
to a higher value, by default it's set to 512 pixels
(both width and height).
Documentation
Check the docs to know more about the main API.
More examples
Check my simple terminal app's code, QRterm, which uses this library to generate QR codes from your terminal, and also it's logo generator.
Also check this repo's logo and its README images generator.
License
Distributed under the MIT License. See LICENSE
for more information.