Exemplo n.º 1
0
int
main(int argc, char* argv[])
{
    QRcode * qrcode = QRcode_encodeString8bit(argv[1], 4 , QR_ECLEVEL_L);

    int width = qrcode->width;
    int x,y,i = 0;
    unsigned char * data=qrcode->data;

    printf("P1\n %d %d\n", width, width);

    for(y=0; y < width; y++)
    {
        for(x =0; x <width; x++, i++)
        {
            printf((data[i] & 1)?"1 " : "0 ");
        }


        printf("\n");
    }

    QRcode_free(qrcode);

    return 0;

}
Exemplo n.º 2
0
void qrcode_clock::updateTime()
{
    // get time
    QDateTime time = QDateTime::currentDateTime();
    QString timeStr(QString::number(time.toTime_t()));

    // encode time as QR code
    QRcode *qrCodeMatrix(QRcode_encodeString8bit(timeStr.toStdString().c_str(), 0, QR_ECLEVEL_H));

    if (!qrCodeMatrix) {
        qDebug() << "kein QrCode erzeugt";
        return;
    }

    // transfer QR code into picture
    m_qrImage = new QImage(qrCodeMatrix->width, qrCodeMatrix->width, QImage::Format_Mono);
    for (int i = 0; i < qrCodeMatrix->width; ++i) {
        for (int j = 0; j < qrCodeMatrix->width; ++j) {
            m_qrImage->setPixel(j, i, qrCodeMatrix->data[i * qrCodeMatrix->width + j] & 1);
        }
    }

    QRcode_free(qrCodeMatrix);

    m_qrImage->setColor(0, qRgb(255,255,255));
    m_qrImage->setColor(1, qRgb(0,0,0));

    m_qrClock->setPixmap(QPixmap::fromImage(*m_qrImage));

    // show human readable time
    m_humanClock->setText(time.toUTC().toString() + " UTC");
}
Exemplo n.º 3
0
static gboolean qr_avatar_render_hook(gpointer source, gpointer data)
{
	AvatarRender *ar = (AvatarRender *)source;
	GtkWidget *image = NULL;
	gchar * text;

	QRcode *qrcode;
	unsigned char * qrdata;
	GdkPixbuf *pixbuf, *pixbuf_scaled;
	int i, j, k, rowstride, channels;
	guchar *pixel;

	debug_print("qr-avatar qr_avatar_render_hook() invoked\n");

	if ((text = procmsg_msginfo_get_avatar(ar->full_msginfo, QR_AVATAR_QR_AVATAR)) != NULL) {

		if ((qrcode = QRcode_encodeString8bit(text, 0, QR_ECLEVEL_L)) == NULL)
			return FALSE;

		qrdata = qrcode->data;

		pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8,
				qrcode->width, qrcode->width);

		pixel = gdk_pixbuf_get_pixels(pixbuf);
		rowstride = gdk_pixbuf_get_rowstride(pixbuf);
		channels = gdk_pixbuf_get_n_channels(pixbuf);

		gdk_pixbuf_fill(pixbuf, 0xffffffff);

		for (i = 0; i < qrcode->width; i++)
			for (j = 0; j < qrcode->width; j++) {
				for (k = 0; k < channels; k++)
					pixel[i * rowstride + j * channels + k] = !(*qrdata & 0x1) * 0xff;
				qrdata++;
			}

		/* claws-mail is limited to avatar with 48x48 pixels
		 * TODO: skip scaling as soon as different sizes are supported */
		pixbuf_scaled = gdk_pixbuf_scale_simple(pixbuf, QR_AVATAR_SIZE, QR_AVATAR_SIZE, GDK_INTERP_BILINEAR);

		QRcode_free(qrcode);
		g_object_unref(pixbuf);

		image = gtk_image_new_from_pixbuf(pixbuf_scaled);

		g_object_unref(pixbuf_scaled);

		if (ar->image != NULL) /* previous plugin set one */
			gtk_widget_destroy(ar->image);

		ar->image = image;

                return TRUE;
        }

        return FALSE; /* keep rendering */
}
Exemplo n.º 4
0
void test_encodeEmpty8(void)
{
	QRcode *qrcode;

	testStart("Test encode an empty string.");
	qrcode = QRcode_encodeString8bit("", 0, QR_ECLEVEL_H);
	assert_null(qrcode, "QRcode_encodeString8bit() returned something.\n");
	testFinish();
	if(qrcode != NULL) QRcode_free(qrcode);
}
Exemplo n.º 5
0
static QRcode *encode(const char *intext)
{
	QRcode *code;

	if(eightbit) {
		code = QRcode_encodeString8bit(intext, version, level);
	} else {
		code = QRcode_encodeString(intext, version, level, hint, casesensitive);
	}

	return code;
}
Exemplo n.º 6
0
static void displayQRCode(const char *secret, const char *label,
                          const int use_totp) {
  if (qr_mode == QR_NONE) {
    return;
  }
  char *encoderURL;
  const char *url = getURL(secret, label, &encoderURL, use_totp);
  /* ROCKS 
     Don't Display Google Chart URL. This is a bad idea for production use
  puts(encoderURL); 
     ROCKS */

  // Only newer systems have support for libqrencode. So, instead of requiring
  // it at build-time, we look for it at run-time. If it cannot be found, the
  // user can still type the code in manually, or he can copy the URL into
  // his browser.
  if (isatty(1)) {
    void *qrencode = dlopen("libqrencode.so.2", RTLD_NOW | RTLD_LOCAL);
    if (!qrencode) {
      qrencode = dlopen("libqrencode.so.3", RTLD_NOW | RTLD_LOCAL);
    }
    if (qrencode) {
      typedef struct {
        int version;
        int width;
        unsigned char *data;
      } QRcode;
      QRcode *(*QRcode_encodeString8bit)(const char *, int, int) =
        (QRcode *(*)(const char *, int, int))
        dlsym(qrencode, "QRcode_encodeString8bit");
      void (*QRcode_free)(QRcode *qrcode) =
        (void (*)(QRcode *))dlsym(qrencode, "QRcode_free");
      if (QRcode_encodeString8bit && QRcode_free) {
        QRcode *qrcode = QRcode_encodeString8bit(url, 0, 1);
        char *ptr = (char *)qrcode->data;
        // Output QRCode using ANSI colors. Instead of black on white, we
        // output black on grey, as that works independently of whether the
        // user runs his terminals in a black on white or white on black color
        // scheme.
        // But this requires that we print a border around the entire QR Code.
        // Otherwise, readers won't be able to recognize it.
        if (qr_mode != QR_UTF8) {
          for (int i = 0; i < 2; ++i) {
            printf(ANSI_BLACKONGREY);
            for (int x = 0; x < qrcode->width + 4; ++x) printf("  ");
            puts(ANSI_RESET);
          }
          for (int y = 0; y < qrcode->width; ++y) {
            printf(ANSI_BLACKONGREY"    ");
            int isBlack = 0;
            for (int x = 0; x < qrcode->width; ++x) {
              if (*ptr++ & 1) {
                if (!isBlack) {
                  printf(ANSI_BLACK);
                }
                isBlack = 1;
              } else {
                if (isBlack) {
                  printf(ANSI_WHITE);
                }
                isBlack = 0;
              }
              printf("  ");
            }
            if (isBlack) {
              printf(ANSI_WHITE);
            }
            puts("    "ANSI_RESET);
          }
          for (int i = 0; i < 2; ++i) {
            printf(ANSI_BLACKONGREY);
            for (int x = 0; x < qrcode->width + 4; ++x) printf("  ");
            puts(ANSI_RESET);
          }
        } else {
          // Drawing the QRCode with Unicode block elements is desirable as
          // it makes the code much smaller, which is often easier to scan.
          // Unfortunately, many terminal emulators do not display these
          // Unicode characters properly.
          printf(ANSI_BLACKONGREY);
          for (int i = 0; i < qrcode->width + 4; ++i) {
            printf(" ");
          }
          puts(ANSI_RESET);
          for (int y = 0; y < qrcode->width; y += 2) {
            printf(ANSI_BLACKONGREY"  ");
            for (int x = 0; x < qrcode->width; ++x) {
              int top = qrcode->data[y*qrcode->width + x] & 1;
              int bottom = 0;
              if (y+1 < qrcode->width) {
                bottom = qrcode->data[(y+1)*qrcode->width + x] & 1;
              }
              if (top) {
                if (bottom) {
                  printf(UTF8_BOTH);
                } else {
                  printf(UTF8_TOPHALF);
                }
              } else {
                if (bottom) {
                  printf(UTF8_BOTTOMHALF);
                } else {
                  printf(" ");
                }
              }
            }
            puts("  "ANSI_RESET);
          }
          printf(ANSI_BLACKONGREY);
          for (int i = 0; i < qrcode->width + 4; ++i) {
            printf(" ");
          }
          puts(ANSI_RESET);
        }
        QRcode_free(qrcode);
      }
      dlclose(qrencode);
    }
  }

  free((char *)url);
  free(encoderURL);
}
Exemplo n.º 7
0
Arquivo: qrview.c Projeto: n13l/qrview
int
main(int argc, char *argv[]) 
{
	QRcode *qr = NULL;
#ifdef CONFIG_WINDOWS
	win32_io_init();
#endif

	while ((opt = getopt_long(argc, argv, opt_cmd, opt_long, NULL)) >= 0) {
		switch (opt) {
		case OPT_HELP:
		case '?':
			usage(0);
			break;
                case OPT_NAME:
                        name = strdup(optarg);
			break;
		case OPT_VERSION:
			version();
			exit(EXIT_SUCCESS);
		break;
		case OPT_QR_SIZE:
			qr_size = atoi(optarg);
		break;
		case OPT_TIMEOUT:
			timeout = atoi(optarg);
			break;
		case OPT_ALPHA:
			alpha_max = atoi(optarg);
			if (alpha_max > 255)
				alpha_max = 255;
			if (alpha_max < 0)
				alpha_max = 0;
		break;
		default:
		if (app->num_cmds >= MAX_CMDS)
			giveup("Too many commands specified");
			app->cmd[app->num_cmds] = opt;
			app->arg[app->num_cmds] = optarg;
			app->num_cmds++;
		}
	}

	if (optind + 1 != argc)
		usage(2);

	if (!(uri = argv[argc - 1]))
		usage(2);

	if (lock_instance())
		goto exit;

	qr = QRcode_encodeString8bit(uri , QR_MODE_8, QR_ECLEVEL_H);
	if (qr == NULL)
		goto exit;

        struct surface surface = (struct surface) {
		.width = qr->width,
		.data  = qr->data
	};

	main_window (argc, argv, &surface);

exit:
	if (qr)
		QRcode_free(qr);
	unlock_instance();
	return EXIT_SUCCESS;
}