二维条码/二维码(2-dimensional bar code)是用某种特定的几何图形按一定规律在平面(二维方向上)分布的黑白相间的图形记录数据符号信息的,其应用广泛,如:产品防伪/溯源、广告推送、网站链接、数据下载、商品交易、定位/导航、电子凭证、车辆管理、信息传递、名片交流、wifi共享等。
一维码是用条空在水平方向上表达信息的条码,外形更接近矩形;二维码可以说是正方形,在其内部有三个“回”字型的定位点,可以帮助条码设备对焦,便于读取数据。一维码的信息部分只能是字母和数字,它的数据容量较小一般只可容纳30个字符左右。二维码的信息承载量很大,最大数据含量可达1850个字符,信息内容可包含,字母,数字,汉字,字符,片假名等。一维码的常用码制包括:EAN码、39码、交叉25码、UPC码、128码、93码,ISBN码,及Codabar(库德巴码)等;二维码常用的码制有:PDF417二维条码,Datamatrix二维条码,QR Code,Code 49,Code 16K,Code one等。
(1)qrcodewidget.h
#ifndef QRCODE_H #define QRCODE_H #include <QWidget> #include <QTextCodec> namespace Ui { class QRCode; } class QRCodeWidget : public QWidget { Q_OBJECT public: explicit QRCodeWidget(QWidget *parent = 0); ~QRCodeWidget(); private slots: void on_pushButton_clicked(); private: Ui::QRCode *ui; }; #endif // QRCODE_H
(2)qrcodewidget.cpp
#include <QPicture> #include "qrcodewidget.h" #include "ui_qrcode.h" #include "qrencode.h" #include "qrenc.c" QRCodeWidget::QRCodeWidget(QWidget *parent) : QWidget(parent, Qt::Dialog), ui(new Ui::QRCode) { QTextCodec *codec = QTextCodec::codecForName("utf8"); QTextCodec::setCodecForLocale(codec); QTextCodec::setCodecForCStrings(codec); QTextCodec::setCodecForTr(codec); ui->setupUi(this); } QRCodeWidget::~QRCodeWidget() { delete ui; } void QRCodeWidget::on_pushButton_clicked() { QString info = ui->lineEdit->text(); if (info.isEmpty()) { return; } char outfile[] = "output.png"; margin = 2; ::size = 7; version = 2; //QRcode *qrcode = QRcode_encodeString(info.toStdString().data(), 2, QR_ECLEVEL_L, QR_MODE_8, 0); //writePNG(qrcode, outfile); qrencode((unsigned char *)info.toStdString().data(), info.length(), outfile); ui->label_3->setPixmap(QPixmap(outfile)); }
(3)main.cpp
#include "qrcodewidget.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); QRCodeWidget w; w.show(); return a.exec(); }
(4)运行
也可是使用QrenCode 的命令行工具生成二维码,在centos6.6下执行# yum install qrencode(或下载qrencode-3.4.4.tar.gz使用./configure、make、make install安装);使用qrencode -o output.png https://www.baidu.com/即可在当前目录下生成百度的二维码图片output.png;若想自定义尺寸的话,加上 -s 参数,比如 -s 6 表示尺寸为 6x6 平方像表大小,qrencode -s 6 -o output.png https://www.baidu.com/。命令行下识别二维码的是#yum install libdecodeqr-examples,libdecodeqr-simpletest <二维码图片>。安装zbar-0.10.tar.bz2,也可一识别二维码。
(5)源码可从csdn上下载:http://download.csdn.net/detail/taiyang1987912/8991975。
#ifndef QRCODEZBAR_H #define QRCODEZBAR_H #include <QWidget> #include <QTextCodec> #include <QFileDialog> #include <zbar/QZBar.h> namespace Ui { class QZbar; } class QRCodeZbar : public QWidget { Q_OBJECT public: QRCodeZbar(QWidget *parent = 0); ~QRCodeZbar(); private: private slots: void on_pushButton_clicked(); private: Ui::QZbar *ui; zbar::QZBar *qz; }; #endif // QRCODEZBAR_H(2) qrcodezbar.cpp
#include "qrcodezbar.h" #include "ui_qzbar.h" #include "scanimage.h" #ifdef QRDECODE #include "scanimagemagick.h" #endif QRCodeZbar::QRCodeZbar(QWidget *parent) : QWidget(parent, Qt::Dialog) , ui(new Ui::QZbar) { QTextCodec *codec = QTextCodec::codecForName("utf8"); QTextCodec::setCodecForLocale(codec); QTextCodec::setCodecForCStrings(codec); QTextCodec::setCodecForTr(codec); ui->setupUi(this); } QRCodeZbar::~QRCodeZbar() { } void QRCodeZbar::on_pushButton_clicked() { QString fileName = QFileDialog::getOpenFileName(this, tr("choose a picture"), QApplication::applicationDirPath(), tr("all Files (*.*)")); ui->lineEdit->setText(fileName); ui->label_3->setPixmap(QPixmap(ui->lineEdit->text())); char result[1024] = {0}; #ifdef QRDECODE scanimagemagick(ui->lineEdit->text().toStdString().data(), result); #else scanimage(ui->lineEdit->text().toStdString().data(), result); #endif ui->textEdit->setText(result); }(3)scanimagemagick.h
#ifndef SCANIMAGEMAGICK_H #define SCANIMAGEMAGICK_H #include <iostream> #include <Magick++.h> #include <zbar.h> #define STR(s) #s /***************** * yum install ImageMagick ***********/ using namespace std; using namespace zbar; int scanimagemagick (const char *filename, char *result); #endif // SCANIMAGE_H(4)scanimagemagick.cpp
#include <stdio.h> #include <stdlib.h> #include <string.h> #include "scanimagemagick.h" int scanimagemagick(const char *filename, char *result) { #ifdef MAGICK_HOME // http://www.imagemagick.org/Magick++/ // under Windows it is necessary to initialize the ImageMagick // library prior to using the Magick++ library Magick::InitializeMagick(MAGICK_HOME); #endif // create a reader ImageScanner scanner; // configure the reader scanner.set_config(ZBAR_NONE, ZBAR_CFG_ENABLE, 1); // obtain image data Magick::Image magick(filename); // read an image file int width = magick.columns(); // extract dimensions int height = magick.rows(); Magick::Blob blob; // extract the raw data magick.modifyImage(); magick.write(&blob, "GRAY", 8); const void *raw = blob.data(); // wrap image data Image image(width, height, "Y800", raw, width * height); // scan the image for barcodes int n = scanner.scan(image); // extract results for(Image::SymbolIterator symbol = image.symbol_begin(); symbol != image.symbol_end(); ++symbol) { // do something useful with results // cout << "decoded " << symbol->get_type_name() // << " symbol \"" << symbol->get_data() << '"' << endl; sprintf(result, "%s:%s", symbol->get_type_name().data(), symbol->get_data().data()); } // clean up image.set_data(NULL, 0); return(0); }(5)运行结果
从http://sourceforge.net/projects/qzxing/files/下载QZxing的decoding library的源码,在工程中加入include(QZXing_sourceV2.3/QZXing.pri)。
(1)qrcondezxing.h
#ifndef QRCONDEZXING_H #define QRCONDEZXING_H #include <QWidget> #include <QTextCodec> #include <QFileDialog> namespace Ui { class QRCondeZxing; } class QRCondeZxing : public QWidget { Q_OBJECT public: explicit QRCondeZxing(QWidget *parent = 0); ~QRCondeZxing(); private slots: void on_pushButton_clicked(); private: Ui::QRCondeZxing *ui; }; #endif // QRCONDEZXING_H(2)qrcondezxing.cpp
#include "qrcondezxing.h" #include "ui_qrcondezxing.h" #include "QZXing.h" QRCondeZxing::QRCondeZxing(QWidget *parent) : QWidget(parent), ui(new Ui::QRCondeZxing) { QTextCodec *codec = QTextCodec::codecForName("utf8"); QTextCodec::setCodecForLocale(codec); QTextCodec::setCodecForCStrings(codec); QTextCodec::setCodecForTr(codec); ui->setupUi(this); } QRCondeZxing::~QRCondeZxing() { delete ui; } void QRCondeZxing::on_pushButton_clicked() { QString fileName = QFileDialog::getOpenFileName(this, tr("choose a picture"), QApplication::applicationDirPath(), tr("all Files (*.*)")); ui->lineEdit->setText(fileName); ui->label_3->setPixmap(QPixmap(ui->lineEdit->text())); QZXing decoder; QString qrmsg = decoder.decodeImageFromFile(ui->lineEdit->text()); ui->textEdit->setText(qrmsg); }(3)运行结果:
(4)源码可从csdn上下载:http://download.csdn.net/detail/taiyang1987912/8991903。