QRCoders.jl Documentation
Module that can create QR codes as data or images using qrcode
or exportqrcode
.
Creating QR codes
QRCoders.qrcode
— Functionqrcode( message::AbstractString
; eclevel::ErrCorrLevel = Medium()
, version::Int = 0
, mode::Mode = Numeric()
, mask::Int = -1
, width::Int=0)
Create a BitArray{2}
with the encoded message
, with true
(1
) for the black areas and false
(0
) as the white ones.
The error correction level eclevel
can be picked from four values: Low()
(7% of missing data can be restored), Medium()
(15%), Quartile()
(25%) or High()
(30%). Higher levels make denser QR codes.
The version of the QR code can be picked from 1 to 40. If the assigned version is too small to contain the message, the first available version is used.
The encoding mode mode
can be picked from five values: Numeric()
, Alphanumeric()
, Byte()
, Kanji()
or UTF8()
. If the assigned mode
is nothing
or failed to contain the message, the mode is automatically picked.
The mask pattern mask
can be picked from 0 to 7. If the assigned mask
is nothing
, the mask pattern will picked by the penalty rules.
qrcode(code::QRCode)
Create a QR code matrix by the QRCode
object.
Note: It would raise an error if failed to use the specified mode
or
version`.
QRCoders.exportqrcode
— Functionexportqrcode( message::AbstractString
, path::AbstractString = "qrcode.png"
; eclevel::ErrCorrLevel = Medium()
, version::Int = 0
, mode::Mode = nothing
, width::int = 4
, pixels::Int = 160)
Create an image with the encoded message
of approximate size pixels x pixels
.
The error correction level eclevel
can be picked from four values: Low()
(7% of missing data can be restored), Medium()
(15%), Quartile()
(25%) or High()
(30%). Higher levels make denser QR codes.
The version of the QR code can be picked from 1 to 40. If the assigned version is too small to contain the message, the first available version is used.
The encoding mode mode
can be picked from four values: Numeric()
, Alphanumeric()
, Byte()
, Kanji()
or UTF8()
. If the assigned mode
is nothing
or failed to contain the message, the mode is automatically picked.
The mask pattern mask
can be picked from 0 to 7. If the assigned mask
is nothing
, the mask pattern will picked by the penalty rules.
exportqrcode( code::QRCode
, path::AbstractString = "qrcode.png"
; pixels::Int = 160)
Create an image with the encoded message
of approximate size targetsize
.
exportqrcode( codes::AbstractVector{QRCode}
, path::AbstractString = "qrcode.gif"
; pixels::Int = 160
, fps::Int = 2)
Create an animated gif with codes
of approximate size targetsize
.
The frame rate fps
is the number of frames per second.
Note: The codes
should have the same size while the other properties can be different.
exportqrcode( msgs::AbstractVector{<:AbstractString}
, path::AbstractString = "qrcode.gif"
; eclevel::ErrCorrLevel = Medium()
, version::Int = 0
, mode::Mode = Numeric()
, mask::Int = -1
, width::Int = 4
, targetsize::Int = 5
, pixels::Int = 160
, fps::Int = 2)
Create an animated gif with msgs
of approximate size pixels x pixels
.
The frame rate fps
is the number of frames per second.
QRCoders.QRCode
— TypeQRCode
A type that represents a QR code.
Fields
version::Int
: version of the QR codemode::Mode
: encoding mode of the QR codeeclevel::ErrCorrLevel
: error correction level of the QR codemask::Int
: mask pattern of the QR codemessage::String
: message to be encodedborder::Int
: width of the white border
Encoding modes
Currently, there are five supported encoding modes.
QRCoders.Mode
— TypeAbstract type that groups the five supported encoding modes Numeric
, Alphanumeric
, Byte
, Kanji
and UTF8
.
QRCoders.Numeric
— TypeEncoding mode for messages composed of digits only.
QRCoders.Alphanumeric
— TypeEncoding mode for messages composed of digits, characters A
-Z
(capital only) , space and %
*
+
-
.
/
:
$
.
QRCoders.Byte
— TypeEncoding mode for messages composed of one-byte characters(unicode range from 0x00 to 0xff, including ISO-8859-1 and undefined characters)
QRCoders.Kanji
— TypeEncoding mode for messages composed of Shift JIS(Shift Japanese Industrial Standards) characters.
QRCoders.UTF8
— TypeEncoding mode for messages composed of utf-8 characters.
Error Correction
There are four error correction levels you can choose from.
QRCoders.ErrCorrLevel
— TypeAbstract type that groups the four error correction levels Low
, Medium
, Quartile
and High
.
QRCoders.Low
— TypeError correction level that can restore up to 7% of missing codewords.
QRCoders.Medium
— TypeError correction level that can restore up to 15% of missing codewords.
QRCoders.Quartile
— TypeError correction level that can restore up to 25% of missing codewords.
QRCoders.High
— TypeError correction level that can restore up to 30% of missing codewords.
Reed Solomon code
QRCoders.Polynomial.Poly
— TypeData structure to encode polynomials to generate the error correction codewords.
QRCoders.Polynomial.generator_matrix
— Functiongenerator_matrix(msglen::Int, necwords::Int)
Create the generator matrix of size (msglen + necwords, msglen)
.
The generator matrix G is of the form
[ * ]
[ I ]
where I
is the identity matrix of size msglen
and *
is computed by the remainder polynomials.
Note that we use a reversed version of generator matrices, i.e. the coefficients is stored in the reverse order, e.g. a0, ..., an.
In this sense, we still have G⋅x = c, where x is the message polynomial and c is the received polynomial.
To get an ordinary generator matrix, just rotate it by 180 degrees, i.e. @view(G[end:-1:1, end:-1:1])
QRCoders.Polynomial.geterrcode
— Functiongeterrcode(f::Poly, n::Int)
Return a polynomial containing the n
error correction codewords of f
.
styled QR codes
Plot in REPL.
QRCoders.unicodeplot
— Functionunicodeplot(mat::AbstractMatrix{Bool}; border=:none)
Uses UnicodePlots.jl to draw the matrix.
Note: In UnicodePlots.jl, matrix index start from the left-down corner.
unicodeplot(message::AbstractString
; border=:none)
Uses UnicodePlots.jl to draw the QR code of message
.
QRCoders.unicodeplotbychar
— Functionunicodeplotbychar(mat::AbstractMatrix)
Plot of the QR code using Unicode characters.
The value 1(true)
represents a dark space and 0(false)
a white square. It is the same convention as QR code and is the opposite of general image settings.
unicodeplotbychar(message::AbstractString)
Plot of the QR code using Unicode characters.
Plot image in a QR code.
QRCoders.imageinqrcode
— Functionimageinqrcode( code::QRCode
, img::AbstractMatrix{Bool}
; rate::Real=1
, singlemask::Bool=true
, leftop::Tuple{Int, Int}=(-1, -1)
, fillalignment::Bool=false
) where T <: Union{Bool, Nothing}
Plot image inside QR code.
Arguments
code::QRCode
: QR codeimg::AbstractMatrix{Bool}
: image to be plottedrate::Real=1
: damage rate of the error correction codewordssinglemask::Bool=true
: use the default mask patternleftop::Tuple{Int,Int}=(-1, -1)
: left top corner of the image
Examples
Create a QR code matrix
julia> using QRCoders
julia> qrcode("Hello world!")
21×21 BitMatrix:
1 1 1 1 1 1 1 0 1 1 1 1 1 0 1 1 1 1 1 1 1
1 0 0 0 0 0 1 0 1 0 1 0 1 0 1 0 0 0 0 0 1
1 0 1 1 1 0 1 0 0 0 1 1 0 0 1 0 1 1 1 0 1
⋮ ⋮ ⋮ ⋮ ⋮
1 0 1 1 1 0 1 0 1 0 0 0 1 0 0 1 0 0 1 0 0
1 0 0 0 0 0 1 0 0 1 0 1 0 1 1 1 1 0 0 0 1
1 1 1 1 1 1 1 0 1 0 1 1 0 1 1 1 0 0 1 0 0
Export to a file
PNG file – default
julia> exportqrcode("Hello world!")
A file will be saved at ./qrcode.png
.
GIF file
Create a .gif
file from messages.
Use .gif
file to show QR codes with different masks.
julia> using QRCoders: penalty
julia> codes = [QRCode("Hello world!", mask = i) for i in 0:7]
julia> qrcode.(codes) .|> penalty |> print
[425, 485, 342, 318, 495, 562, 368, 415]
julia> exportqrcode(codes, fps=3)
Styled QR codes
This part is still under development, see issue#33 for more information. Feel free to contribute or propose more ideas!
Plot an image inside a QRCode.
using TestImages, ColorTypes, ImageTransformations
using QRCoders
oriimg = testimage("cameraman")
code = QRCode("Hello world!", version=16, width=4)
img = imresize(oriimg, 66, 66) .|> Gray .|> round .|> Bool .|> !
imageinqrcode(code, img; rate=0.9) |> exportbitmat("qrcode-camera.png")
Here rate
is the damage rate of error correction codewords, it should be no greater than 1.