栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

Qt+opencv+zbar条码识别

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Qt+opencv+zbar条码识别

实现效果


 

 

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include 
#include 
#include 
#include 
#include 

using namespace std;
using namespace zbar;

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    cam     = NULL;
    timer   = new QTimer(this);
    imag    = new QImage();
    connect(timer, SIGNAL(timeout()), this, SLOT(readFrame()));
    connect(timer, SIGNAL(timeout()), this, SLOT(scancode()));
    connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(on_pushButton_cliked()));
    connect(ui->pushButton_2, SIGNAL(clicked()), this, SLOT(on_pushButton_2_clicked()));
}

MainWindow::~MainWindow()
{
    delete ui;
    cvReleaseCapture(&cam);
}
void MainWindow::on_pushButton_clicked()
{
    cam = cvCreateCameraCapture(0);//打开摄像头,从摄像头中获取视频
    timer->start(33);
}

void MainWindow::on_pushButton_2_clicked()
{
    timer->stop();
     // 停止读取数据。
    cvReleaseCapture(&cam);
}

void MainWindow::readFrame()
{

    frame = cvQueryFrame(cam);
    cvCvtColor(frame, frame, CV_BGR2RGB);
    QImage image((const uchar*)frame->imageData, frame->width, frame->height, QImage::Format_RGB888);
    ui->label->setPixmap(QPixmap::fromImage(image));
}

void MainWindow::scancode()
{
    frame = cvQueryFrame(cam);
    IplImage *grayFrame = 0;
    //创建zbar图像扫描器
    ImageScanner scanner;
    //配置zbar图片扫描器
    scanner.set_config(ZBAR_NONE, ZBAR_CFG_ENABLE, 1);
    clock_t start, finish;
    //clock_t为CPU时钟计时单元数
    start = clock();
    if (frame)
    {
        if (!grayFrame)
        grayFrame = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U, 1);
        cvCvtColor(frame, grayFrame, CV_BGR2GRAY);
        //创建zbar图像
        Image image(frame->width, frame->height, "Y800", grayFrame->imageData, frame->width * frame->height);
        //扫描图像,识别条码,获取个数
        int symbolCount = scanner.scan(image);
        //获取第一个条码
        Image::SymbolIterator symbol = image.symbol_begin();
        if (image.symbol_begin() == image.symbol_end())
        {
            cout << "查询条码失败,请检查图片!" << endl;
        }
        while (symbolCount--)
        {
          cout << "条码类型:'" << symbol->get_type_name() << "'" << endl;
          cout << "条码内容:'" << symbol->get_data() << "'" << endl;
          string str_data1 = symbol->get_type_name()+symbol->get_data();
          QString qstr1 = QString::fromStdString(str_data1) ;
          ui->plainTextEdit->setPlainText(qstr1);
          //获取定位点个数
          int pointCount = symbol->get_location_size();
          for (int i = 0; i < pointCount; i++)
          // cout << '(' << symbol->get_location_x(i) << ',' << symbol->get_location_y(i) << ")" << endl;
          ++symbol;
          }
    }
    finish = clock();
    cout <lineEdit->setText(str);

    //获取系统时间戳
    time_t timeReal;
    time(&timeReal);
    timeReal = timeReal + 8*3600;
    tm* t = gmtime(&timeReal);
    cout << timeReal << endl;
    printf("%d-%02d-%02d %02d:%02d:%02dn", t->tm_year + 1900, t->tm_mon + 1, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec);
    

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/955424.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号