转自: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 plaincopy
  1. #ifndef QRCODE_H
  2. #define QRCODE_H
  3. #include <QWidget>
  4. #include <QTextCodec>
  5. namespace Ui {
  6. class QRCode;
  7. }
  8. class QRCodeWidget : public QWidget
  9. {
  10. Q_OBJECT
  11. public:
  12. explicit QRCodeWidget(QWidget *parent = 0);
  13. ~QRCodeWidget();
  14. private slots:
  15. void on_pushButton_clicked();
  16. private:
  17. Ui::QRCode *ui;
  18. };
  19. #endif // QRCODE_H

(2)qrcodewidget.cpp

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

(3)main.cpp

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

(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 plaincopy
  1. #ifndef QRCODEZBAR_H
  2. #define QRCODEZBAR_H
  3. #include <QWidget>
  4. #include <QTextCodec>
  5. #include <QFileDialog>
  6. #include <zbar/QZBar.h>
  7. namespace Ui {
  8. class QZbar;
  9. }
  10. class QRCodeZbar : public QWidget
  11. {
  12. Q_OBJECT
  13. public:
  14. QRCodeZbar(QWidget *parent = 0);
  15. ~QRCodeZbar();
  16. private:
  17. private slots:
  18. void on_pushButton_clicked();
  19. private:
  20. Ui::QZbar *ui;
  21. zbar::QZBar *qz;
  22. };
  23. #endif // QRCODEZBAR_H

(2) qrcodezbar.cpp

[html] view plaincopy
  1. #include "qrcodezbar.h"
  2. #include "ui_qzbar.h"
  3. #include "scanimage.h"
  4. #ifdef QRDECODE
  5. #include "scanimagemagick.h"
  6. #endif
  7. QRCodeZbar::QRCodeZbar(QWidget *parent)
  8. : QWidget(parent, Qt::Dialog)
  9. , ui(new Ui::QZbar)
  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. QRCodeZbar::~QRCodeZbar()
  18. {
  19. }
  20. void QRCodeZbar::on_pushButton_clicked()
  21. {
  22. QString fileName = QFileDialog::getOpenFileName(this, tr("choose a picture"),
  23. QApplication::applicationDirPath(),
  24. tr("all Files (*.*)"));
  25. ui->lineEdit->setText(fileName);
  26. ui->label_3->setPixmap(QPixmap(ui->lineEdit->text()));
  27. char result[1024] = {0};
  28. #ifdef QRDECODE
  29. scanimagemagick(ui->lineEdit->text().toStdString().data(), result);
  30. #else
  31. scanimage(ui->lineEdit->text().toStdString().data(), result);
  32. #endif
  33. ui->textEdit->setText(result);
  34. }

(3)scanimagemagick.h

[html] view plaincopy
  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. int scanimagemagick (const char *filename, char *result);
  13. #endif // SCANIMAGE_H

(4)scanimagemagick.cpp

[html] view plaincopy
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "scanimagemagick.h"
  5. int scanimagemagick(const char *filename, char *result)
  6. {
  7. #ifdef MAGICK_HOME
  8. // http://www.imagemagick.org/Magick++/
  9. //    under Windows it is necessary to initialize the ImageMagick
  10. //    library prior to using the Magick++ library
  11. Magick::InitializeMagick(MAGICK_HOME);
  12. #endif
  13. // create a reader
  14. ImageScanner scanner;
  15. // configure the reader
  16. scanner.set_config(ZBAR_NONE, ZBAR_CFG_ENABLE, 1);
  17. // obtain image data
  18. Magick::Image magick(filename);  // read an image file
  19. int width = magick.columns();   // extract dimensions
  20. int height = magick.rows();
  21. Magick::Blob blob;              // extract the raw data
  22. magick.modifyImage();
  23. magick.write(&blob, "GRAY", 8);
  24. const void *raw = blob.data();
  25. // wrap image data
  26. Image image(width, height, "Y800", raw, width * height);
  27. // scan the image for barcodes
  28. int n = scanner.scan(image);
  29. // extract results
  30. for(Image::SymbolIterator symbol = image.symbol_begin();
  31. symbol != image.symbol_end();
  32. ++symbol) {
  33. // do something useful with results
  34. //        cout << "decoded " << symbol->get_type_name()
  35. //             << " symbol \"" << symbol->get_data() << '"' << endl;
  36. sprintf(result, "%s:%s", symbol->get_type_name().data(), symbol->get_data().data());
  37. }
  38. // clean up
  39. image.set_data(NULL, 0);
  40. return(0);
  41. }

(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 plaincopy
  1. #ifndef QRCONDEZXING_H
  2. #define QRCONDEZXING_H
  3. #include <QWidget>
  4. #include <QTextCodec>
  5. #include <QFileDialog>
  6. namespace Ui {
  7. class QRCondeZxing;
  8. }
  9. class QRCondeZxing : public QWidget
  10. {
  11. Q_OBJECT
  12. public:
  13. explicit QRCondeZxing(QWidget *parent = 0);
  14. ~QRCondeZxing();
  15. private slots:
  16. void on_pushButton_clicked();
  17. private:
  18. Ui::QRCondeZxing *ui;
  19. };
  20. #endif // QRCONDEZXING_H

(2)qrcondezxing.cpp

[html] view plaincopy
  1. #include "qrcondezxing.h"
  2. #include "ui_qrcondezxing.h"
  3. #include "QZXing.h"
  4. QRCondeZxing::QRCondeZxing(QWidget *parent) :
  5. QWidget(parent),
  6. ui(new Ui::QRCondeZxing)
  7. {
  8. QTextCodec *codec = QTextCodec::codecForName("utf8");
  9. QTextCodec::setCodecForLocale(codec);
  10. QTextCodec::setCodecForCStrings(codec);
  11. QTextCodec::setCodecForTr(codec);
  12. ui->setupUi(this);
  13. }
  14. QRCondeZxing::~QRCondeZxing()
  15. {
  16. delete ui;
  17. }
  18. void QRCondeZxing::on_pushButton_clicked()
  19. {
  20. QString fileName = QFileDialog::getOpenFileName(this, tr("choose a picture"),
  21. QApplication::applicationDirPath(),
  22. tr("all Files (*.*)"));
  23. ui->lineEdit->setText(fileName);
  24. ui->label_3->setPixmap(QPixmap(ui->lineEdit->text()));
  25. QZXing decoder;
  26. QString qrmsg = decoder.decodeImageFromFile(ui->lineEdit->text());
  27. ui->textEdit->setText(qrmsg);
  28. }

(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-

四、总结

(1)二维码和条形码的开源软件种类很多,可针对C、Java、ios等不同接口来选择自己的需求。
(2)二维码的库在中文方面的也是有支持的。
(3)源码使用中仅仅进行了简单的调用,没有实现更深层次的开发,主要是自己没有项目需求,其他的开发读者可继续开发。
(4)若有建议,请留言,在此先感谢!

Qt浅谈之三十二二维码条形码解析相关推荐

  1. Qt浅谈之三十系统托盘(QSystemTrayIcon)

    一.简介 Qt自带的例子/usr/lib64/qt4/examples/desktop/systray中详尽介绍了系统托盘的功能,在其基础上进行拓展,定制适合自己的系统托盘.        托盘菜单实 ...

  2. Qt浅谈之三十九圆形进度条

    一.简介 Qt下进度条一般都是水平或垂直的,有时需要一个椭圆或圆来动态显示进度,或用此来显示存储百分比,都是比较适用的. 二.详解 1.代码 (1)widgetdisplay.h #ifndef WI ...

  3. Qt浅谈之三十六仿360设置中心

    一.简介 模仿360设置中心面板的功能:左侧导航使用QTreeWidget,右侧的显示区域使用QScrollArea控件:主要使用垂直滚动条的valueChanged事件和QTreeWidget的it ...

  4. 使用JS调用手机本地摄像头或者相册图片识别二维码/条形码

    接着昨天的需求,不过这次不依赖微信,使用纯js唤醒手机本地摄像头或者选择手机相册图片,识别其中的二维码或者是条形码.昨天,我使用微信扫一扫识别,效果超棒的.不过如果依赖微信的话,又怎么实现呢,这里介绍 ...

  5. 浅谈压缩感知(二十八):压缩感知重构算法之广义正交匹配追踪(gOMP)

    浅谈压缩感知(二十八):压缩感知重构算法之广义正交匹配追踪(gOMP) 主要内容: gOMP的算法流程 gOMP的MATLAB实现 一维信号的实验与结果 稀疏度K与重构成功概率关系的实验与结果 一.g ...

  6. 浅谈自考学习方法(二)

    自考的<管理经济学>从发给我们到晚上上课就一天的时间,米老师一位年近五十的老人仅用了两个的小时就读完了,并且没有任何的遗漏,课堂讲的依然妙趣横生,欢声笑语,短短两个小时的时间不禁让人惊叹, ...

  7. Python实例浅谈之三Python与C/C++相互调用

    参考:http://blog.csdn.net/taiyang1987912/article/details/44779719 Python实例浅谈之三Python与C/C++相互调用 二.Pytho ...

  8. [开源]C#二维码生成解析工具,可添加自定义Logo

    原文:[开源]C#二维码生成解析工具,可添加自定义Logo 二维码又称 QR Code,QR 全称 Quick Response,是一个近几年来移动设备上超流行的一种编码方式,它比传统的 Bar Co ...

  9. iOS 生成二维码/条形码

    级别:★★☆☆☆ 标签:「iOS CIFilter」「CIQRCodeGenerator」「CICode128BarcodeGenerator」「二维码加logo」 作者: Xs·H 审校: QiSh ...

  10. h5端呼起摄像头扫描二维码并解析

    2016年6月29日补充: 最近做了一些与表单相关的项目,使用了h5的input控件,在使用过程中遇到了很多的坑.也包括与这篇文章相关的. 首先我们应该知道使用h5新提供的属性getUserMedia ...

最新文章

  1. 《自然》深度:中国AI能在2030年制霸世界吗?
  2. ajax传递对象数组
  3. 递归,记忆化搜索,(棋盘分割)
  4. Shell Sort 希尔排序 收藏
  5. 酒店差异战打响,“设计标签”能实现突围吗?
  6. 表的连接方式:NESTED LOOP、HASH JOIN、SORT MERGE JOIN
  7. python简短语法_写出优雅简洁的 python 语法(二)函数传参
  8. 51nod1134最长递增子序列(dp)
  9. 个人比较喜欢的一个故事,与大家分享一下
  10. 【渝粤教育】国家开放大学2018年秋季 0570-21T摄像、录像制作 参考试题
  11. 经典机器学习系列(二)【线性判别分析LDA】
  12. 几款免费开源的企业管理软件
  13. [rtsp]海康IPC监控摄像头远程外网监控配置(DDNS)
  14. android中图标怎么改,android的软件图标怎么改
  15. 互联网晚报 |10/12 |中国汽车出口量跃居全球第二;统一充电接口或让苹果每年损失百亿;《财富》杂志公布“改变世界的公司”榜单...
  16. ssm教务系统网站毕业设计源码290915
  17. signature=3d7534face990de7e25e7438440abe49,Designing the User Interface 5e
  18. 全新解密域名防红系统源码,支持分站
  19. Zig-Zag模式填充矩阵
  20. eccv 2018 image caption generation论文导读

热门文章

  1. 关于sql中处理日期的相关函数
  2. 【富文本】解决会声会影、PR、AE处理视频后过大的问题(三款工具)专业视频压制软件|专业视频压制神器下载
  3. c语言自己建个题库随机出题,自己建个题库随机出题 让学习效率大大提高
  4. 基于springBoot的社区信息管理系统
  5. 插个“USB”就能无线投影,DispalyTen想借传屏切入企业级会议市场
  6. K60学习笔记(二)——FTM多功能定时器模块
  7. 1、随机中心裁剪 transforms.CenterCrop(size)
  8. nekohtml和htmlparser
  9. PYTHON 获取淘宝服务器时间 显示在tkinter的UI
  10. 软件产品需求分析报告