A QR code
A QR code is a picture that holds a text, such as a web address or the name and password of a Wi-Fi network. This lesson uses the QRCoder package from nuget.org to make one, prints it as text in the console and writes it as a PNG picture. It is the second lesson on NuGet packages: the first one explained the steps, this one uses them.
What you will learn#
- how a QR code is made of modules and what the levels of error correction mean,
- the steps of the previous lesson on a second package: project file,
dotnet publish, wrap, load, - a method chosen by the names of its parameters, constructors with
@New, and enums as texts, - reading a value from a result that has no Ring class (
getProp), - writing the bytes of a PNG picture to a file.
What a QR code is#
A QR code is a square grid of dark and light modules. The three big squares in the corners tell a camera where the code is and how it is turned. Between them the text is stored as bits, together with extra data that lets a camera read the code even when a part of it is dirty or covered. The amount of extra data is the level of error correction:
| Level | Part of the code that can be lost |
|---|---|
L (low) | about 7% |
M (medium) | about 15% |
Q (quartile) | about 25% |
H (high) | about 30% |
A higher level needs more modules for the same text, so the code gets bigger. The empty border around the code (the quiet zone) is part of the picture, because a camera needs it.
The steps#
They are the steps of the lesson A NuGet package:
- a project file names the package,
QRCoderhere, dotnet publish Qr -c Release -o Qr/libscopies it,wrapassembly.ringwrites the Ring classes,dnLoadanddnImportFrom, then the classes are used.
The project file is in samples/UsingDotNet/tutorials/Qr, and it is the same as before with another name and version:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<AssemblyName>Qr</AssemblyName>
<DebugType>none</DebugType>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="QRCoder" Version="1.6.0" />
</ItemGroup>
</Project>The program#
# a QR code with a NuGet package - QRCoder makes the code, and the program prints it as text and writes it as a PNG picture
# (the project is in the folder Qr: Qr.csproj names QRCoder, and Empty.cs is the one source file a project needs)
load "dotnet.ring"
using { "System.IO" }
cHere = currentdir()
cDll = cHere + "/Qr/libs/QRCoder.dll"
cClasses = cHere + "/Qr/classes"
# STEP 1. Get the package (nuget.org the first time) with dotnet publish
if not fexists(cDll)
? "getting the package..."
system("dotnet publish Qr -c Release -o Qr/libs -nologo -v q > nul")
ok
if not fexists(cDll)
? "The package could not be restored. It is downloaded from nuget.org the first time, so the computer needs the internet once."
bye
ok
# STEP 2. Make Ring classes for the DLL
chdir("../../../tools/dotnettools")
system(exefilename() + " wrapassembly.ring " + cDll + " " + cClasses + " > nul")
chdir(cHere)
# STEP 3. Load the assembly and the classes
dnLoad(cDll)
dnImportFrom(cClasses + "/","QRCoder")
func main
oGenerator = new QRCodeGenerator()
# 1. The code of a text. CreateQrCode has several versions, so it takes the names of its parameters; the level of error
# correction is an enum, so it is a text: L, M, Q or H.
oCode = oGenerator.CreateQrCode([ :plainText = "RING", :eccLevel = "L" ])
? "1. a QR code for RING, as text (a block is a dark module):"
# The class that draws it as text takes the code (a constructor chosen by the name of its parameter)
oAscii = new AsciiQRCode().@New([ :data = oCode ])
? oAscii.GetGraphic(1)
# 2. A higher level of error correction repeats more data, so the code is bigger. The answer is a List<BitArray> that has no
# class, so its Count is read with getProp.
cText = "https://ring-lang.github.io"
? "2. the size of the code for " + cText + " (modules on a side, with the empty border):"
for cLevel in ["L","M","Q","H"]
oSized = oGenerator.CreateQrCode([ :plainText = cText, :eccLevel = cLevel ])
? " level " + cLevel + ": " + oSized.ModuleMatrix.Count
next
# 3. A picture: PngByteQRCode gives the bytes of a PNG file, and a byte[] that .NET returns is a Ring text
cPng = new PngByteQRCode().@New([ :data = oCode ]).GetGraphic(8) # 8 pixels for each module
cFile = new Path().Combine([new Path().GetTempPath(),"ringdotnet-qr.png"])
new File().WriteAllBytes(cFile,cPng)
? nl + "3. the picture is a PNG file: " + (substr(cPng,2,3) = "PNG") + ", written to a file: " + new File().Exists(cFile)
new File().Delete(cFile)1. a QR code for RING, as text (a block is a dark module):
██████████████ ██ ████ ██████████████
██ ██ ████ ██ ██ ██
██ ██████ ██ ████ ██ ██ ██████ ██
██ ██████ ██ ██ ██ ██ ██████ ██
██ ██████ ██ ██ ██ ██ ██████ ██
██ ██ ██ ████ ██ ██
██████████████ ██ ██ ██ ██████████████
██████████
████ ██ ████ ████ ██████ ████
██ ██ ████ ██ ████ ██ ████
██ ████ ██ ████ ██████
██ ██ ████ ██ ██ ██
████████████ ██ ██ ██ ██ ████ ██
████ ██ ████
██████████████ ██ ██ ██ ██ ██
██ ██ ██████████ ██████ ████
██ ██████ ██ ██ ██ ██████ ██████
██ ██████ ██ ██ ████ ████████
██ ██████ ██ ██ ██ ██ ██
██ ██ ██ ████ ██ ██ ██
██████████████ ██ ██████ ██████
2. the size of the code for https://ring-lang.github.io (modules on a side, with the empty border):
level L: 33
level M: 37
level Q: 37
level H: 41
3. the picture is a PNG file: 1, written to a file: 1How it works#
Making the code#
QRCodeGenerator makes the data of a code: the grid of modules. Its method CreateQrCode has several versions, so it takes a list of the names of the parameters: [ :plainText = "RING", :eccLevel = "L" ]. The level is an enum of the package, and an enum in Ring is its name as a text, "L".
The data is not a picture yet. Other classes of the package turn it into a picture of one kind or another:
AsciiQRCodedraws it with text characters.GetGraphic(1)returns a text of lines, and the program prints it. A console shows the dark modules as blocks.PngByteQRCodedraws it as the bytes of a PNG file.GetGraphic(8)uses 8 pixels for each module.
Both classes take the data in their constructor. They have more than one constructor, so the program writes new AsciiQRCode().@New([ :data = oCode ]), the form of the lesson The generic functions.
A result that has no class#
oSized.ModuleMatrix is a list of rows, a List<BitArray>. The package has no Ring class for that type, so a property of it, such as Count, is read with the generic getProp("Count"). The grid is as high as it is wide, so the count is the number of modules on a side: 33 for the longer text at level L, and 41 at level H.
The picture#
A byte[] that .NET returns is a Ring text of bytes (a copy), so the PNG is a Ring text. Its second to fourth bytes are the letters PNG, which is how the program tests that it is a picture, and File.WriteAllBytes(path, bytes) writes it to a file. The program writes the file to the folder of temporary files and removes it at the end; in your program you would keep it, or show it in a PictureBox (the tutorial Images).
Using a package safely#
- The program was tested to make a code and a PNG. A real use of a code should be tried with a camera of the kind that your users have, with the size and the colors that you chose.
- The text of a code is not secret: anybody who scans it reads it. A code with a password for a Wi-Fi network shows the password to everybody who sees the code.
- Look at the license of a package before you give your program to others (the page of the package at nuget.org shows it).
What you learned#
- a QR code is a grid of modules, and the level of error correction trades size for safety,
- the same steps bring any package: project file, publish, wrap, load,
- parameters by name, enums as texts,
@Newfor a class with several constructors, getPropfor a value of a type with no class, and bytes written to a file.
Exercises#
- Make a code for a Wi-Fi network: the text is
WIFI:T:WPA;S:name;P:password;;. Change the level toHand compare the sizes. - Print the codes of three different texts one after the other.
- Write the PNG with
GetGraphic(20)and open the file with the picture viewer of your system. - Show the picture in a window: write the file, then load it into a
PictureBoxas in the tutorial Images.