Skip to content

Instantly share code, notes, and snippets.

@albinoloverats
Last active August 29, 2015 14:18
Show Gist options
  • Save albinoloverats/12ce2d599f8e89ce94b4 to your computer and use it in GitHub Desktop.
Save albinoloverats/12ce2d599f8e89ce94b4 to your computer and use it in GitHub Desktop.
Terminal based QR code generator
/*
* gcc -lqrencode -Wall -Wextra -std=c99 -O3 -o qrcurses qrcurses.c
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <qrencode.h>
#define BLOCK "\x1b[47m \x1b[0m"
#define LINE(z) for (int y = 0; y < qr->width + z; y++) printf("%s", BLOCK); printf("\n")
static void qr_print(QRcode *qr)
{
if (!qr)
return;
LINE(2);
printf("%s", BLOCK);
for (int y = 0; y < qr->width; y += (printf("%s\n%s", BLOCK, BLOCK), 1))
for (int x = 0; x < qr->width; x++)
printf("%s", qr->data[y * qr->width + x] & 0x01 ? " " : BLOCK);
LINE(1);
return;
}
int main(int argc, char **argv)
{
char data[7089] = { 0x0 };
QRcode *qr = NULL;
if (argc > 1)
{
for (int i = 1; i < argc; i += (strcat(data, argv[i]), 1))
if (i > 1)
strcat(data, " ");
qr = QRcode_encodeString(data, 0, QR_ECLEVEL_L, QR_MODE_8, 1);
}
else
qr = QRcode_encodeData(read(STDIN_FILENO, data, sizeof data), (unsigned char *)data, 0, QR_ECLEVEL_L);
qr_print(qr);
QRcode_free(qr);
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment