First commit
This commit is contained in:
@@ -0,0 +1,216 @@
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <QImage>
|
||||
#include <getopt.h>
|
||||
#include <qrencode.h>
|
||||
#include <qdebug.h>
|
||||
#include "mainwindow.h"
|
||||
#include <QApplication>
|
||||
#include "ui_mainwindow.h"
|
||||
|
||||
char *intext = " ";
|
||||
static int casesensitive = 0;
|
||||
static int eightbit = 0;
|
||||
static int kanji = 0;
|
||||
static int version = 0;
|
||||
static int Size = 3;
|
||||
static int margin = 4;
|
||||
static QRecLevel level = QR_ECLEVEL_L;
|
||||
|
||||
MainWindow::MainWindow(QWidget *parent) :
|
||||
QMainWindow(parent),
|
||||
ui(new Ui::MainWindow)
|
||||
{
|
||||
ui->setupUi(this);
|
||||
ui->textEdit->setText(tr("1234567893233695"));
|
||||
}
|
||||
|
||||
MainWindow::~MainWindow()
|
||||
{
|
||||
delete ui;
|
||||
}
|
||||
|
||||
enum {
|
||||
O_HELP,
|
||||
O_OUTPUT,
|
||||
O_SIZE,
|
||||
O_VERSION,
|
||||
O_LEVEL,
|
||||
O_MARGIN,
|
||||
O_KANJI,
|
||||
O_CASE,
|
||||
O_8BIT,
|
||||
};
|
||||
|
||||
static const struct option options[] = {
|
||||
{"h", no_argument , NULL, O_HELP},
|
||||
{"o", required_argument, NULL, O_OUTPUT},
|
||||
{"l", required_argument, NULL, O_LEVEL},
|
||||
{"s", required_argument, NULL, O_SIZE},
|
||||
{"v", required_argument, NULL, O_VERSION},
|
||||
{"m", required_argument, NULL, O_MARGIN},
|
||||
{"k", no_argument , NULL, O_KANJI},
|
||||
{"c", no_argument , NULL, O_CASE},
|
||||
{"8", no_argument , NULL, O_8BIT},
|
||||
{NULL, 0, NULL, 0}
|
||||
};
|
||||
|
||||
static void usage(void)
|
||||
{
|
||||
qDebug()<<(
|
||||
"qrencode version %s\n\n"
|
||||
"Usage: qrencode [OPTION]... [STRING]\n"
|
||||
"Encode input data in a QR Code and save as a PNG image.\n\n"
|
||||
" -h display this message.\n"
|
||||
" -o FILENAME write PNG image to FILENAME. If '-' is specified, the result\n"
|
||||
" will be output to standard output.\n"
|
||||
" -s NUMBER specify the size of dot (pixel). (default=3)\n"
|
||||
" -l {LMQH} specify error collectin level from L (lowest) to H (highest).\n"
|
||||
" (default=L)\n"
|
||||
" -v NUMBER specify the version of the symbol. (default=auto)\n"
|
||||
" -m NUMBER specify the width of margin. (default=4)\n"
|
||||
" -k assume that the input text contains kanji (shift-jis).\n"
|
||||
" -c encode alphabet characters in 8-bit mode. If your application\n"
|
||||
" is case-sensitive, choose this.\n"
|
||||
" -8 encode entire data in 8-bit mode. -c and -k will be ignored.\n"
|
||||
" [STRING] input data. If it is not specified, data will be taken from\n"
|
||||
" standard input.\n"
|
||||
);
|
||||
}
|
||||
|
||||
#define MAX_DATA_SIZE 7090 /* from the specification */
|
||||
static char *readStdin(void)
|
||||
{
|
||||
char *buffer;
|
||||
int ret;
|
||||
|
||||
buffer = (char *)malloc(MAX_DATA_SIZE);
|
||||
ret = fread(buffer, 1, MAX_DATA_SIZE, stdin);
|
||||
if(ret == 0) {
|
||||
fprintf(stderr, "No input data.\n");
|
||||
exit(1);
|
||||
}
|
||||
if(!feof(stdin)) {
|
||||
fprintf(stderr, "Input data is too large.\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
buffer[ret] = '\0';
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
static QRcode *encode(const char *intext)
|
||||
{
|
||||
QRencodeMode hint;
|
||||
QRcode *code;
|
||||
|
||||
if(kanji) {
|
||||
hint = QR_MODE_KANJI;
|
||||
} else {
|
||||
hint = QR_MODE_8;
|
||||
}
|
||||
|
||||
if(eightbit) {
|
||||
code = QRcode_encodeString8bit(intext, version, level);
|
||||
} else {
|
||||
code = QRcode_encodeString(intext, version, level, hint, casesensitive);
|
||||
}
|
||||
|
||||
return code;
|
||||
}
|
||||
|
||||
static void qrencode(const char *intext, const char *outfile)
|
||||
{
|
||||
FILE *fp;
|
||||
QRcode *qrcode;
|
||||
//png_structp png_ptr;
|
||||
//png_infop info_ptr;
|
||||
unsigned char *row, *p, *q;
|
||||
int x, y, xx, yy, bit;
|
||||
int realwidth;
|
||||
|
||||
qrcode = encode(intext);
|
||||
if(qrcode == NULL) {
|
||||
fprintf(stderr, "Failed to encode the input data.\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
realwidth = (qrcode->width + margin * 2) * Size;
|
||||
|
||||
row = (unsigned char *)malloc(realwidth);
|
||||
|
||||
// top margin
|
||||
memset(row, 0x0, realwidth );
|
||||
|
||||
qDebug()<<"1: "<< qrcode->width<<realwidth<<margin<<Size<<sizeof(intext);
|
||||
|
||||
// realwidth = (qrcode->width + margin * 2) * Size;
|
||||
QImage QRImage(realwidth,qrcode->width*Size,QImage::Format_ARGB32 );
|
||||
QImage *img;
|
||||
img = &QRImage;
|
||||
img->fill(0x00ffffff);
|
||||
|
||||
/* data */
|
||||
p = qrcode->data;
|
||||
int ypos=0;//margin * Size;
|
||||
int xpos=0;
|
||||
for(y=0; y<qrcode->width; y++) {
|
||||
bit = 7;
|
||||
bit = 7 - (margin * Size % 8);
|
||||
xpos=margin * Size;
|
||||
for(x=0; x<qrcode->width; x++) {
|
||||
for(xx=0; xx<Size; xx++) {
|
||||
if((*p & 1) << bit){
|
||||
for(yy=0; yy<Size; yy++) {
|
||||
img->setPixel(xpos,ypos+yy,0xff000000);
|
||||
}
|
||||
}
|
||||
xpos++;
|
||||
|
||||
}
|
||||
p++;
|
||||
bit--;
|
||||
if(bit < 0) {
|
||||
//q++;
|
||||
bit = 7;
|
||||
}
|
||||
}
|
||||
ypos+=Size;
|
||||
}
|
||||
img->save("Myqr.bmp");
|
||||
|
||||
free(row);
|
||||
QRcode_free(qrcode);
|
||||
}
|
||||
|
||||
|
||||
void MainWindow::on_pushButton_clicked()
|
||||
{
|
||||
QString in;
|
||||
//usage();
|
||||
in = ui->textEdit->toPlainText();
|
||||
//intext = in.toLatin1().data();
|
||||
intext = (char *)malloc(in.length()*2);
|
||||
memset(intext,0,sizeof(intext));
|
||||
//intext =in.unicode();
|
||||
//memcpy(&intext[0], in.unicode (),2*in.length());
|
||||
memcpy(intext,in.toLocal8Bit().data(),2*in.length());
|
||||
|
||||
qDebug()<<in<< intext;
|
||||
Size = ui->comboBox_size->currentIndex()+3;
|
||||
qrencode(intext,outfile);
|
||||
QImage img("Myqr.bmp",0);
|
||||
ui->labQRImage->setPixmap( QPixmap::fromImage(img,Qt::AutoColor));
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
|
||||
QApplication a(argc, argv);
|
||||
MainWindow w;
|
||||
w.show();
|
||||
return a.exec();
|
||||
}
|
||||
Reference in New Issue
Block a user