Cadoiz
Cadoiz

Reputation: 1676

What is an easy way to generate a QR-Code in a console

My use case is the following: I wanted to show my OMEMO-Fingerprint in Gajim in Windows to verify it, but wasn't able to get it to work there. So I was looking for an easy way to generate a specific QR-Code from console. Doing that, should (I expect) be as easy as the xmpp-URI-format, which is pretty easy.

Upvotes: 0

Views: 2669

Answers (3)

matheusjulio96
matheusjulio96

Reputation: 13

Using a C# app you can use the NuGet QRCoder to generate the QRCode and then print in a console like this:

static void Main(string[] args)
{
    var qrGenerator = new QRCodeGenerator();
    var qrCodeData = qrGenerator.CreateQrCode("http://www.stackoverflow.com", QRCodeGenerator.ECCLevel.Q);            
    ConsoleWriteQrCode(qrCodeData.ModuleMatrix);
}

public static void ConsoleWriteQrCode(List<BitArray> moduleData)
{
    var platte = new
    {
        WHITE_ALL = Encoding.UTF8.GetString(Encoding.Convert(Encoding.Unicode, Encoding.UTF8, Encoding.Unicode.GetBytes("\u2588"))),
        WHITE_BLACK = Encoding.UTF8.GetString(Encoding.Convert(Encoding.Unicode, Encoding.UTF8, Encoding.Unicode.GetBytes("\u2580"))),
        BLACK_WHITE = Encoding.UTF8.GetString(Encoding.Convert(Encoding.Unicode, Encoding.UTF8, Encoding.Unicode.GetBytes("\u2584"))),
        BLACK_ALL = " ",
    };

    var WHITE = false;
    var BLACK = true;

    var oddRow = moduleData.Count % 2 == 1;
    if (oddRow)
        moduleData.Add(new System.Collections.BitArray(moduleData[0].Count));

    Console.ForegroundColor = ConsoleColor.White;
    for (int row = 0; row < moduleData.Count; row+=2)
    {
        for (int col = 0; col < moduleData[row].Count; col++)
        {
            if (moduleData[row][col] == WHITE && moduleData[row + 1][col] == WHITE)
                Console.Write(platte.WHITE_ALL);
            else if (moduleData[row][col] == WHITE && moduleData[row + 1][col] == BLACK)
                Console.Write(platte.WHITE_BLACK);
            else if (moduleData[row][col] == BLACK && moduleData[row + 1][col] == WHITE)
                Console.Write(platte.BLACK_WHITE);
            else
                Console.Write(platte.BLACK_ALL);
        }
        Console.WriteLine();
    }
}

Upvotes: 0

Rico Lee
Rico Lee

Reputation: 11

I'm the author of this java/kotlin library for users who want to print a qrcode in the console.

To use it, add this maven dependency in pom.xml:

<dependency>
  <groupId>pro.leaco.qrcode</groupId>
  <artifactId>console-qrcode</artifactId>
  <version>1.0.1</version>
</dependency>

then use this code to get the following example:

ConsoleQrcode.print("https://www.github.com");

example screenshoot

Upvotes: 1

Cadoiz
Cadoiz

Reputation: 1676

I found that with Python and python3-qrcode, this is manageable like this:

Install Python and the needed modules:

pip install --upgrade pip
pip install cryptography cryptography pillow qrcode setuptools axolotl  

Use this onliner batch or bash file - it works not only in windows but also in Linux and more:

python -c "import qrcode; import sys; qrcode.make(sys.argv[1]).save(sys.argv[2])" %1 %2

It takes as the first input (you can replace %1 of course) the string that should be converted to QR, for example xmpp:[email protected]?omemo-sid-123456789=cf4d1558a8b872e6d8d213be2d3ac447663b3d516509f56536d80b70ca0e8e89 and as %2 the destination file as e.g. result.png. That's basically it.

If you want to generate a QR-Code out of the content of a file, you can use this:

python -c "import qrcode; import sys; qrcode.make(open(sys.argv[1], 'r').read()).save(sys.argv[2])" %1 %2

Upvotes: 0

Related Questions