RingDotNetfor Ring

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#

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:

LevelPart 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:

  1. a project file names the package, QRCoder here,
  2. dotnet publish Qr -c Release -o Qr/libs copies it,
  3. wrapassembly.ring writes the Ring classes,
  4. dnLoad and dnImportFrom, 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:

samples/UsingDotNet/tutorials/Qr/Qr.csproj
<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#

samples/UsingDotNet/tutorials/ch13_qr.ring
# 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)
Output
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: 1

How 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:

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#

What you learned#

Exercises#

  1. Make a code for a Wi-Fi network: the text is WIFI:T:WPA;S:name;P:password;;. Change the level to H and compare the sizes.
  2. Print the codes of three different texts one after the other.
  3. Write the PNG with GetGraphic(20) and open the file with the picture viewer of your system.
  4. Show the picture in a window: write the file, then load it into a PictureBox as in the tutorial Images.

Next#

Excel files

RingDotNet — use .NET classes and methods from Ring programs. Screenshots and output on these pages come from the real samples.