QT 二維碼庫

一、簡介

        二維條碼/二維碼(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、libqrencode庫(生成二維碼)

(1)qrcodewidget.h

[html] view plain copy
  1. #ifndef QRCODE_H  
  2. #define QRCODE_H  
  3.   
  4. #include <QWidget>  
  5. #include <QTextCodec>  
  6.   
  7. namespace Ui {  
  8. class QRCode;  
  9. }  
  10.   
  11. class QRCodeWidget : public QWidget  
  12. {  
  13.     Q_OBJECT  
  14.   
  15. public:  
  16.     explicit QRCodeWidget(QWidget *parent = 0);  
  17.     ~QRCodeWidget();  
  18.   
  19. private slots:  
  20.     void on_pushButton_clicked();  
  21.   
  22. private:  
  23.     Ui::QRCode *ui;  
  24. };  
  25.   
  26. #endif // QRCODE_H  

(2)qrcodewidget.cpp

[html] view plain copy
  1. #include <QPicture>  
  2. #include "qrcodewidget.h"  
  3. #include "ui_qrcode.h"  
  4. #include "qrencode.h"  
  5. #include "qrenc.c"  
  6.   
  7. QRCodeWidget::QRCodeWidget(QWidget *parent) :  
  8.     QWidget(parent, Qt::Dialog),  
  9.     ui(new Ui::QRCode)  
  10. {  
  11.     QTextCodec *codec = QTextCodec::codecForName("utf8");  
  12.     QTextCodec::setCodecForLocale(codec);  
  13.     QTextCodec::setCodecForCStrings(codec);  
  14.     QTextCodec::setCodecForTr(codec);  
  15.     ui->setupUi(this);  
  16. }  
  17.   
  18. QRCodeWidget::~QRCodeWidget()  
  19. {  
  20.     delete ui;  
  21. }  
  22.   
  23. void QRCodeWidget::on_pushButton_clicked()  
  24. {  
  25.     QString info = ui->lineEdit->text();  
  26.     if (info.isEmpty()) {  
  27.         return;  
  28.     }  
  29.     char outfile[] = "output.png";  
  30.     margin = 2;  
  31.     ::size = 7;  
  32.     version = 2;  
  33.     //QRcode *qrcode = QRcode_encodeString(info.toStdString().data(), 2, QR_ECLEVEL_L, QR_MODE_8, 0);  
  34.     //writePNG(qrcode, outfile);  
  35.   
  36.     qrencode((unsigned char *)info.toStdString().data(), info.length(), outfile);  
  37.     ui->label_3->setPixmap(QPixmap(outfile));  
  38. }  

(3)main.cpp

[html] view plain copy
  1. #include "qrcodewidget.h"  
  2. #include <QApplication>  
  3.   
  4. int main(int argc, char *argv[])  
  5. {  
  6.     QApplication a(argc, argv);  
  7.     QRCodeWidget w;  
  8.     w.show();  
  9.   
  10.     return a.exec();  
  11. }  

(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

2、zbar庫(識別二維碼)

      下載http://sourceforge.net/projects/zbar/files/?source=navbar的源碼:zbar-0.10.tar.bz2,執行./configure和make和make install安裝zbar,默認安裝頭文件在/usr/local/include下,庫文件在/usr/local/lib。
(1)qrcodezbar.h
[html] view plain copy
  1. #ifndef QRCODEZBAR_H  
  2. #define QRCODEZBAR_H  
  3.   
  4. #include <QWidget>  
  5. #include <QTextCodec>  
  6. #include <QFileDialog>  
  7. #include <zbar/QZBar.h>  
  8.   
  9. namespace Ui {  
  10. class QZbar;  
  11. }  
  12.   
  13.   
  14. class QRCodeZbar : public QWidget  
  15. {  
  16.     Q_OBJECT  
  17.   
  18. public:  
  19.     QRCodeZbar(QWidget *parent = 0);  
  20.     ~QRCodeZbar();  
  21.   
  22. private:  
  23.   
  24.   
  25. private slots:  
  26.     void on_pushButton_clicked();  
  27.   
  28. private:  
  29.     Ui::QZbar *ui;  
  30.     zbar::QZBar *qz;  
  31. };  
  32.   
  33. #endif // QRCODEZBAR_H  
(2)qrcodezbar.cpp
[html] view plain copy
  1. #include "qrcodezbar.h"  
  2. #include "ui_qzbar.h"  
  3. #include "scanimage.h"  
  4. #ifdef QRDECODE  
  5.     #include "scanimagemagick.h"  
  6. #endif  
  7.   
  8. QRCodeZbar::QRCodeZbar(QWidget *parent)  
  9.     : QWidget(parent, Qt::Dialog)  
  10.     , ui(new Ui::QZbar)  
  11. {  
  12.     QTextCodec *codec = QTextCodec::codecForName("utf8");  
  13.     QTextCodec::setCodecForLocale(codec);  
  14.     QTextCodec::setCodecForCStrings(codec);  
  15.     QTextCodec::setCodecForTr(codec);  
  16.     ui->setupUi(this);  
  17. }  
  18.   
  19. QRCodeZbar::~QRCodeZbar()  
  20. {  
  21.   
  22. }  
  23.   
  24. void QRCodeZbar::on_pushButton_clicked()  
  25. {  
  26.     QString fileName = QFileDialog::getOpenFileName(this, tr("choose a picture"),  
  27.                                                     QApplication::applicationDirPath(),  
  28.                                                     tr("all Files (*.*)"));  
  29.     ui->lineEdit->setText(fileName);  
  30.     ui->label_3->setPixmap(QPixmap(ui->lineEdit->text()));  
  31.     char result[1024] = {0};  
  32.     #ifdef QRDECODE  
  33.         scanimagemagick(ui->lineEdit->text().toStdString().data(), result);  
  34.     #else  
  35.         scanimage(ui->lineEdit->text().toStdString().data(), result);  
  36.     #endif  
  37.     ui->textEdit->setText(result);  
  38. }  
(3)scanimagemagick.h
[html] view plain copy
  1. #ifndef SCANIMAGEMAGICK_H  
  2. #define SCANIMAGEMAGICK_H  
  3. #include <iostream>  
  4. #include <Magick++.h>  
  5. #include <zbar.h>  
  6. #define STR(s) #s  
  7. /*****************  
  8.  * yum install ImageMagick  
  9.  ***********/  
  10. using namespace std;  
  11. using namespace zbar;  
  12.   
  13. int scanimagemagick (const char *filename, char *result);  
  14.   
  15. #endif // SCANIMAGE_H  
(4)scanimagemagick.cpp
[html] view plain copy
  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <string.h>  
  4. #include "scanimagemagick.h"  
  5.   
  6. int scanimagemagick(const char *filename, char *result)  
  7. {  
  8. #ifdef MAGICK_HOME  
  9.     // http://www.imagemagick.org/Magick++/  
  10.     //    under Windows it is necessary to initialize the ImageMagick  
  11.     //    library prior to using the Magick++ library  
  12.     Magick::InitializeMagick(MAGICK_HOME);  
  13. #endif  
  14.   
  15.     // create a reader  
  16.     ImageScanner scanner;  
  17.   
  18.     // configure the reader  
  19.     scanner.set_config(ZBAR_NONE, ZBAR_CFG_ENABLE, 1);  
  20.   
  21.     // obtain image data  
  22.     Magick::Image magick(filename);  // read an image file  
  23.     int width = magick.columns();   // extract dimensions  
  24.     int height = magick.rows();  
  25.     Magick::Blob blob;              // extract the raw data  
  26.     magick.modifyImage();  
  27.     magick.write(&blob, "GRAY", 8);  
  28.     const void *raw = blob.data();  
  29.   
  30.     // wrap image data  
  31.     Image image(width, height, "Y800", raw, width * height);  
  32.   
  33.     // scan the image for barcodes  
  34.     int n = scanner.scan(image);  
  35.   
  36.     // extract results  
  37.     for(Image::SymbolIterator symbol = image.symbol_begin();  
  38.         symbol != image.symbol_end();  
  39.         ++symbol) {  
  40.         // do something useful with results  
  41. //        cout << "decoded " << symbol->get_type_name()  
  42. //             << " symbol \"" << symbol->get_data() << '"' << endl;  
  43.         sprintf(result, "%s:%s", symbol->get_type_name().data(), symbol->get_data().data());  
  44.     }  
  45.   
  46.     // clean up  
  47.     image.set_data(NULL, 0);  
  48.   
  49.     return(0);  
  50. }  
(5)運行結果
  
(6)源碼可從csdn上下載:http://download.csdn.net/detail/taiyang1987912/9001133
(7)提示
       打開編譯選項DEFINES *= QRDECODE,則使用了ImageMagick,則需要安裝#yum install ImageMagick,並加入頭文件路徑/usr/include/ImageMagick/。若不是用QRDECODE,用C庫,再識別有些png文件時會出現段錯誤。上述zbar有中文亂碼,可參考網上修改。

3、QZxing庫(識別二維碼)

        從http://sourceforge.net/projects/qzxing/files/下載QZxing的decoding library的源碼,在工程中加入include(QZXing_sourceV2.3/QZXing.pri)。
(1)qrcondezxing.h

[html] view plain copy
  1. #ifndef QRCONDEZXING_H  
  2. #define QRCONDEZXING_H  
  3.   
  4. #include <QWidget>  
  5. #include <QTextCodec>  
  6. #include <QFileDialog>  
  7.   
  8. namespace Ui {  
  9. class QRCondeZxing;  
  10. }  
  11.   
  12. class QRCondeZxing : public QWidget  
  13. {  
  14.     Q_OBJECT  
  15.   
  16. public:  
  17.     explicit QRCondeZxing(QWidget *parent = 0);  
  18.     ~QRCondeZxing();  
  19.   
  20. private slots:  
  21.     void on_pushButton_clicked();  
  22.   
  23. private:  
  24.     Ui::QRCondeZxing *ui;  
  25. };  
  26.   
  27. #endif // QRCONDEZXING_H  
(2)qrcondezxing.cpp
[html] view plain copy
  1. #include "qrcondezxing.h"  
  2. #include "ui_qrcondezxing.h"  
  3. #include "QZXing.h"  
  4.   
  5. QRCondeZxing::QRCondeZxing(QWidget *parent) :  
  6.     QWidget(parent),  
  7.     ui(new Ui::QRCondeZxing)  
  8. {  
  9.     QTextCodec *codec = QTextCodec::codecForName("utf8");  
  10.     QTextCodec::setCodecForLocale(codec);  
  11.     QTextCodec::setCodecForCStrings(codec);  
  12.     QTextCodec::setCodecForTr(codec);  
  13.     ui->setupUi(this);  
  14. }  
  15.   
  16. QRCondeZxing::~QRCondeZxing()  
  17. {  
  18.     delete ui;  
  19. }  
  20.   
  21. void QRCondeZxing::on_pushButton_clicked()  
  22. {  
  23.     QString fileName = QFileDialog::getOpenFileName(this, tr("choose a picture"),  
  24.                                                     QApplication::applicationDirPath(),  
  25.                                                     tr("all Files (*.*)"));  
  26.     ui->lineEdit->setText(fileName);  
  27.     ui->label_3->setPixmap(QPixmap(ui->lineEdit->text()));  
  28.     QZXing decoder;  
  29.     QString qrmsg = decoder.decodeImageFromFile(ui->lineEdit->text());  
  30.     ui->textEdit->setText(qrmsg);  
  31. }  
(3)運行結果:

 

(4)源碼可從csdn上下載:http://download.csdn.net/detail/taiyang1987912/8991903

三、條形碼/二維碼開源軟件

(1)二維碼掃描工具和開發包ZBar
        ZBar是款桌面電腦用條形碼/二維碼掃描工具,支持攝像頭及圖片掃描,支持多平臺包括 iPhone 手機。同時 ZBar 提供了二維碼掃描的 API 開發包。 ZBar 目前支持掃描,除了 Windows 平臺外,還支持 Linux 及 iPhone 平臺。網址:http://zbar.sourceforge.net
(2)C語言二維條形碼解析庫libqrencode
        libqrencode (QRencode) 是一個用C語言編寫的用來解析二維條形碼(QR Code)的程序庫,libqrencode通過手機的CCD攝像頭來掃描二維條形碼。網址:http://zbar.sourceforge.net
(3)條形碼掃描軟件 Zebra barcode reader
        Zebra barcode reader是一個小型的,分層次的條形碼掃描軟件。它支持許多流行的條碼符號:EAN,UPC,128碼,39碼等。能抓獲條碼解碼圖像和使用視頻設備(如攝像頭)作爲一個條形碼掃描儀。
(4)QZXing
        Qt包裝ZXing的解碼庫。ZXing是條形碼處理類庫,它是一個開源Java類庫用於解析多種格式的1D/2D條形碼。目標是能夠對QR編碼、Data Matrix、UPC的1D條形碼進行解碼。 其提供了多種平臺下的客戶端包括:J2ME、J2SE和Android。網址:http://sourceforge.net/projects/qzxing
       其他參考:http://www.oschina.net/project/tag/238/barcode
http://www.oschina.net/project/tag/238/barcode?lang=21&sort=v-
發佈了70 篇原創文章 · 獲贊 11 · 訪問量 22萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章