基于QT/C++图片文字提取器(免费+开源)

一、实现背景

        国外一开源,国内就自主,甚至最著名的大学,也这么NO FACE,真的让人无语至极。图片文字提取功能,如此简单功能,10多年前就有人实现了,而且开源让大家免费使用,有些人屏蔽国外下载地址,然后自己换皮,挂在网上就开始收费。基于这个背景,怎么能忍,果断开源所有源代码。

二、实现平台

        1、win10-64

        2、Qt 5.14.1 (MSVC 2017, 64 bit)

        3、引用开源库tessdata

三、运行程序(绿色安装包下载地址)演示

在文本类型种选择“中文/英文”,然后选择图片文件,即可完成图片问题提取

四、核心源码(源码包下载地址

1、mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

#include "tesseract/baseapi.h"
#include "tesseract/strngs.h"
#include "leptonica/allheaders.h"

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

protected:
    void paintEvent(QPaintEvent *event) override;

private:
    Ui::MainWindow *ui;
    tesseract::TessBaseAPI *api = nullptr;

    QPixmap pixmap;

private slots:
    void on_comboBox_currentIndexChanged(int index);
    void on_pushButton_clicked();

};
#endif // MAINWINDOW_H

2、mainwindow.cpp

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

#include <QFileDialog>
#include <QMessageBox>
#include <QTextCodec>
#include <QPixmap>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    api = new tesseract::TessBaseAPI();
    if (api->Init("D:/mycode/testDemo/ocrtest/ocrlib/tessdata", "eng"))
    {
        QMessageBox::warning(0,"Error","Could not initialize tesseract");
        exit(1);
    }
}

MainWindow::~MainWindow()
{
    api->End();

    delete ui;
}

void MainWindow::on_comboBox_currentIndexChanged(int index)
{
    QString language;
    if(index == 1) language = "chi_sim";
    else language = "eng";

    if (api->Init("D:/mycode/testDemo/ocrtest/ocrlib/tessdata", language.toStdString().data()))
    {
        QMessageBox::warning(0,"Error","Could not initialize tesseract");
        exit(1);
    }
}

void MainWindow::on_pushButton_clicked()
{
    QString filename = QFileDialog::getOpenFileName(nullptr, "Image", "", "");
    if(filename.isEmpty()){
        QMessageBox::warning(0,"Error","no find image");
        return ;
    }
    ui->lineEdit_path->setText(filename);
    // 加载图片,创建一个QPixmap对象
    pixmap.load(filename);
    //QPixmap pixmap(filename); // 替换为你的图片路径
    // 在QLabel上显示图片
    ui->label_picture->setPixmap(pixmap.scaled(ui->label_picture->size(), Qt::IgnoreAspectRatio));
    //ui->label_picture->setPixmap(pixmap);
    // 显示标签
    ui->label_picture->show();

    QTextCodec *code = QTextCodec::codecForName("GB2312");
    std::string str = code->fromUnicode(filename).data();

    const char* ch = str.c_str();

    Pix *image = pixRead(ch);
    api->SetImage(image);

    char *outText = api->GetUTF8Text();

    ui->textEdit_text->setText(QString(outText));

    delete [] outText;
    pixDestroy(&image);
}

void MainWindow::paintEvent(QPaintEvent *event)
{
    if(!pixmap.isNull()){
        QSize centralSize = ui->centralwidget->size();
        int height = centralSize.height()-ui->statusbar->height()-ui->comboBox->height()-18;
        int width = centralSize.width()/2-9;
        // 在QLabel上显示图片
        ui->label_picture->setPixmap(pixmap.scaled(QSize(width,height), Qt::IgnoreAspectRatio));
        // 显示标签
        ui->label_picture->show();
    }
}


 3、mainwindow.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>522</width>
    <height>453</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>图片文字提取</string>
  </property>
  <property name="windowIcon">
   <iconset resource="png.qrc">
    <normaloff>:/camel.png</normaloff>:/camel.png</iconset>
  </property>
  <widget class="QWidget" name="centralwidget">
   <property name="sizePolicy">
    <sizepolicy hsizetype="Preferred" vsizetype="Preferred">
     <horstretch>100</horstretch>
     <verstretch>0</verstretch>
    </sizepolicy>
   </property>
   <layout class="QGridLayout" name="gridLayout_2">
    <item row="0" column="0">
     <layout class="QHBoxLayout" name="horizontalLayout" stretch="100,100">
      <item>
       <widget class="QWidget" name="widget" native="true">
        <property name="styleSheet">
         <string notr="true"/>
        </property>
        <layout class="QGridLayout" name="gridLayout">
         <property name="leftMargin">
          <number>0</number>
         </property>
         <property name="topMargin">
          <number>0</number>
         </property>
         <property name="rightMargin">
          <number>0</number>
         </property>
         <property name="bottomMargin">
          <number>0</number>
         </property>
         <property name="verticalSpacing">
          <number>6</number>
         </property>
         <item row="0" column="0">
          <widget class="QLabel" name="label_picture">
           <property name="sizePolicy">
            <sizepolicy hsizetype="Expanding" vsizetype="Expanding">
             <horstretch>0</horstretch>
             <verstretch>0</verstretch>
            </sizepolicy>
           </property>
           <property name="styleSheet">
            <string notr="true">background-color: rgb(197, 197, 197);</string>
           </property>
           <property name="text">
            <string/>
           </property>
           <property name="scaledContents">
            <bool>true</bool>
           </property>
           <property name="alignment">
            <set>Qt::AlignCenter</set>
           </property>
          </widget>
         </item>
        </layout>
       </widget>
      </item>
      <item>
       <widget class="QTextEdit" name="textEdit_text"/>
      </item>
     </layout>
    </item>
    <item row="1" column="0">
     <layout class="QHBoxLayout" name="horizontalLayout_2">
      <item>
       <widget class="QLabel" name="label_2">
        <property name="text">
         <string>文本类型</string>
        </property>
       </widget>
      </item>
      <item>
       <widget class="QComboBox" name="comboBox">
        <item>
         <property name="text">
          <string>英文</string>
         </property>
        </item>
        <item>
         <property name="text">
          <string>中文</string>
         </property>
        </item>
       </widget>
      </item>
      <item>
       <widget class="QLineEdit" name="lineEdit_path"/>
      </item>
      <item>
       <widget class="QPushButton" name="pushButton">
        <property name="text">
         <string>选择文件</string>
        </property>
       </widget>
      </item>
     </layout>
    </item>
   </layout>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources>
  <include location="png.qrc"/>
 </resources>
 <connections/>
</ui>

  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: 实现基于Qt/C的音频转换程序可以使用Qt的多媒体框架和音频处理库。具体步骤如下: 1. 首先,选择Qt的多媒体框架,以便处理音频文件。可以在Qt Creator中创建一个基于Qt的桌面应用程序项目。 2. 在Qt的项目中引入Qt Multimedia库,该库提供了对音频的处理功能。可以通过在.pro文件中添加`QT += multimedia`来引入该库。 3. 在Qt的项目中创建一个界面,用于用户选择待转换的音频文件和转换参数。可以使用Qt的UI设计来创建用户界面,例如添加文件选择按钮和转换按钮。 4. 当用户点击转换按钮时,程序将执行音频转换的操作。在转换操作中,首先需要读取待转换的音频文件。 5. 若要实现PCM到WAV的转换,需要将PCM音频数据封装为WAV文件格式。使用Qt的QAudioFormat类来设置WAV格式的音频参数,例如采样率、位深度等。 6. 创建一个QFile对象并打开一个.wave文件,在文件中写入WAV文件头部信息。 7. 读取PCM音频数据,并根据WAV的音频参数将数据写入.wave文件。 8. 如果需要实现WAV到PCM的转换,可以使用Qt的QAudioDecoder类来解码WAV文件,提取出PCM数据。 9. 在PCM互转WAV和位数转换的情况下,可以使用Qt的QAudioConverter类来实现。 10. 最后,将转换后的音频文件保存到指定位置。 以上就是基于Qt/C的音频转换程序的实现步骤。通过以上方法,可以轻松地实现PCM和WAV文件的互相转换和位数的转换。 ### 回答2: 基于QT/C的音频转换程序可以实现PCM格式音频文件与WAV格式音频文件的互相转换和位数转换。 PCM(脉冲编码调制)是一种无压缩的音频编码格式,它将模拟音频信号数字化,采样率和位深度可以自定义。WAV是一种开放的音频文件格式,常用于存储无压缩的音频数据。PCM转换为WAV格式可以使音频文件能够在各种播放和设备中播放。 首先,程序需要读取PCM格式音频文件,并解析其采样率、位深度等参数。这些参数将在转换过程中用于重构WAV文件头。 接下来,程序根据WAV格式的规范,创建一个新的WAV文件,并将PCM音频数据写入。同时,需要根据PCM音频数据的位深度,进行相应的位数转换。例如,将16位的PCM音频数据转换为8位的WAV音频数据。 转换完成后,程序会保存新生成的WAV文件,该文件可以被各种音频播放或设备直接播放。 同样地,该音频转换程序也可以将WAV格式音频文件转换为PCM格式。程序会读取WAV文件的文件头信息,并解析其中的采样率、位深度等参数。然后,程序将WAV文件中的音频数据进行位数转换,并写入一个新的PCM文件。 通过该基于QT/C的音频转换程序,我们可以方便地将PCM格式音频文件与WAV格式音频文件进行互相转换,同时实现位数转换。这样,我们就可以根据实际需求,对音频文件进行灵活的处理和使用。 ### 回答3: 基于Qt/C的音频转换程序可以实现PCM到WAV的互转,以及位数转换。PCM是一种原始的音频格式,而WAV是一种常用的音频文件格式。 PCM到WAV的转换可以通过读取PCM文件的原始数据,然后添加WAV文件头部信息的方式实现。WAV文件头部包含了音频的采样率、位数、通道数等信息。程序可以根据用户选择的参数生成WAV文件头部,并将PCM数据和WAV头部写入新的WAV文件。 位数转换可以将16位的PCM数据转换为8位的PCM数据,或者将8位的PCM数据转换为16位的PCM数据。转换过程中,将每个采样数据的位数进行缩放或扩展即可。程序可以根据用户选择的目标位数对PCM数据进行相应的位数转换,并将转换后的数据写入新的PCM文件或WAV文件。 在实现这个音频转换程序时,可以使用Qt的音频相关类库,如QAudioFormat和QAudioOutput来处理音频数据的读取和写入,以及音频文件格式的解析和生成。同时,还可以利用Qt提供的界面框架来设计一个简洁易用的用户界面,让用户可以方便地选择要转换的文件和转换参数。 总的来说,基于Qt/C的音频转换程序可以实现PCM到WAV的互转和位数转换功能,通过读取和处理音频数据,生成相应的音频文件。该程序可以提供一个用户友好的界面,让用户可以方便地操作和选择转换参数。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值