File indexing completed on 2024-05-12 04:45:55

0001 #include "doodlehanlder.h"
0002 
0003 #include "tesseract/baseapi.h"
0004 #include "tesseract/ocrclass.h"
0005 #include <QDebug>
0006 #include <QImage>
0007 #include <chrono>
0008 #include <filesystem>
0009 #include <fstream>
0010 #include <iostream>
0011 #include <leptonica/allheaders.h>
0012 #include <string>
0013 
0014 DoodleHanlder::DoodleHanlder(QObject *parent)
0015     : QObject(parent)
0016 {
0017 }
0018 
0019 QString DoodleHanlder::getText(const QString &imagePath)
0020 {
0021     tesseract::TessBaseAPI *api = new tesseract::TessBaseAPI();
0022     api->Init(NULL, "eng", tesseract::OEM_LSTM_ONLY);
0023     api->SetPageSegMode(tesseract::PSM_AUTO);
0024     api->SetVariable("debug_file", "tesseract.log");
0025 
0026     //    char *text;
0027     QImage image(imagePath);
0028 
0029     if (image.isNull())
0030         return "";
0031 
0032     qDebug() << image;
0033 
0034     QImage g = image.convertToFormat(QImage::Format_Grayscale8);
0035     qDebug() << g;
0036     if (g.isNull())
0037         return "";
0038 
0039     api->SetImage(g.bits(), g.width(), g.height(), 1, g.bytesPerLine());
0040 
0041     QString outText = QString::fromStdString(api->GetUTF8Text());
0042     qDebug() << outText;
0043     api->End();
0044     //    pixDestroy(&image);
0045     return outText;
0046 }