Qt+opencv+zbar条码识别

Qt+opencv+zbar条码识别,第1张

实现效果


 

 

#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:%02d\n", t->tm_year + 1900, t->tm_mon + 1, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec);
    

}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1325899.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-12
下一篇 2022-06-12

发表评论

登录后才能评论

评论列表(0条)

保存