Qt浅谈之三十二二维码条形码解析
转自: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
- #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。
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
- #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)运行结果
(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
- #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。
三、条形码/二维码开源软件
(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
其他参考: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浅谈之三十二二维码条形码解析相关推荐
- Qt浅谈之三十系统托盘(QSystemTrayIcon)
一.简介 Qt自带的例子/usr/lib64/qt4/examples/desktop/systray中详尽介绍了系统托盘的功能,在其基础上进行拓展,定制适合自己的系统托盘. 托盘菜单实 ...
- Qt浅谈之三十九圆形进度条
一.简介 Qt下进度条一般都是水平或垂直的,有时需要一个椭圆或圆来动态显示进度,或用此来显示存储百分比,都是比较适用的. 二.详解 1.代码 (1)widgetdisplay.h #ifndef WI ...
- Qt浅谈之三十六仿360设置中心
一.简介 模仿360设置中心面板的功能:左侧导航使用QTreeWidget,右侧的显示区域使用QScrollArea控件:主要使用垂直滚动条的valueChanged事件和QTreeWidget的it ...
- 使用JS调用手机本地摄像头或者相册图片识别二维码/条形码
接着昨天的需求,不过这次不依赖微信,使用纯js唤醒手机本地摄像头或者选择手机相册图片,识别其中的二维码或者是条形码.昨天,我使用微信扫一扫识别,效果超棒的.不过如果依赖微信的话,又怎么实现呢,这里介绍 ...
- 浅谈压缩感知(二十八):压缩感知重构算法之广义正交匹配追踪(gOMP)
浅谈压缩感知(二十八):压缩感知重构算法之广义正交匹配追踪(gOMP) 主要内容: gOMP的算法流程 gOMP的MATLAB实现 一维信号的实验与结果 稀疏度K与重构成功概率关系的实验与结果 一.g ...
- 浅谈自考学习方法(二)
自考的<管理经济学>从发给我们到晚上上课就一天的时间,米老师一位年近五十的老人仅用了两个的小时就读完了,并且没有任何的遗漏,课堂讲的依然妙趣横生,欢声笑语,短短两个小时的时间不禁让人惊叹, ...
- Python实例浅谈之三Python与C/C++相互调用
参考:http://blog.csdn.net/taiyang1987912/article/details/44779719 Python实例浅谈之三Python与C/C++相互调用 二.Pytho ...
- [开源]C#二维码生成解析工具,可添加自定义Logo
原文:[开源]C#二维码生成解析工具,可添加自定义Logo 二维码又称 QR Code,QR 全称 Quick Response,是一个近几年来移动设备上超流行的一种编码方式,它比传统的 Bar Co ...
- iOS 生成二维码/条形码
级别:★★☆☆☆ 标签:「iOS CIFilter」「CIQRCodeGenerator」「CICode128BarcodeGenerator」「二维码加logo」 作者: Xs·H 审校: QiSh ...
- h5端呼起摄像头扫描二维码并解析
2016年6月29日补充: 最近做了一些与表单相关的项目,使用了h5的input控件,在使用过程中遇到了很多的坑.也包括与这篇文章相关的. 首先我们应该知道使用h5新提供的属性getUserMedia ...
最新文章
- 《自然》深度:中国AI能在2030年制霸世界吗?
- ajax传递对象数组
- 递归,记忆化搜索,(棋盘分割)
- Shell Sort 希尔排序 收藏
- 酒店差异战打响,“设计标签”能实现突围吗?
- 表的连接方式:NESTED LOOP、HASH JOIN、SORT MERGE JOIN
- python简短语法_写出优雅简洁的 python 语法(二)函数传参
- 51nod1134最长递增子序列(dp)
- 个人比较喜欢的一个故事,与大家分享一下
- 【渝粤教育】国家开放大学2018年秋季 0570-21T摄像、录像制作 参考试题
- 经典机器学习系列(二)【线性判别分析LDA】
- 几款免费开源的企业管理软件
- [rtsp]海康IPC监控摄像头远程外网监控配置(DDNS)
- android中图标怎么改,android的软件图标怎么改
- 互联网晚报 |10/12 |中国汽车出口量跃居全球第二;统一充电接口或让苹果每年损失百亿;《财富》杂志公布“改变世界的公司”榜单...
- ssm教务系统网站毕业设计源码290915
- signature=3d7534face990de7e25e7438440abe49,Designing the User Interface 5e
- 全新解密域名防红系统源码,支持分站
- Zig-Zag模式填充矩阵
- eccv 2018 image caption generation论文导读
热门文章
- 关于sql中处理日期的相关函数
- 【富文本】解决会声会影、PR、AE处理视频后过大的问题(三款工具)专业视频压制软件|专业视频压制神器下载
- c语言自己建个题库随机出题,自己建个题库随机出题 让学习效率大大提高
- 基于springBoot的社区信息管理系统
- 插个“USB”就能无线投影,DispalyTen想借传屏切入企业级会议市场
- K60学习笔记(二)——FTM多功能定时器模块
- 1、随机中心裁剪 transforms.CenterCrop(size)
- nekohtml和htmlparser
- PYTHON 获取淘宝服务器时间 显示在tkinter的UI
- 软件产品需求分析报告