mirror of http://192.168.1.51:8099/lmh188/twain3.0
删除OCR源码,改为链接静态库
This commit is contained in:
parent
f9b494c5b6
commit
6d93230f8b
|
@ -1,163 +0,0 @@
|
||||||
#include "hg_gpdf.h"
|
|
||||||
|
|
||||||
#if defined(WIN32)
|
|
||||||
#include "baseapi.h"
|
|
||||||
#include "allheaders.h"
|
|
||||||
#include "renderer.h"
|
|
||||||
#else
|
|
||||||
#include <leptonica/allheaders.h>
|
|
||||||
#include <tesseract/baseapi.h>
|
|
||||||
#include <tesseract/renderer.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
//#define USE_QT
|
|
||||||
#ifdef USE_QT
|
|
||||||
#include <QDebug>
|
|
||||||
#include <QTime>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
Pix* createPix(const unsigned char * imagedata, int width, int height, int bytes_per_pixel, int bytes_per_line, int dpi)
|
|
||||||
{
|
|
||||||
int bpp = bytes_per_pixel * 8;
|
|
||||||
if (bpp == 0) bpp = 1;
|
|
||||||
Pix* pix = pixCreate(width, height, bpp == 24 ? 32 : bpp);
|
|
||||||
pixSetXRes(pix, dpi);
|
|
||||||
pixSetYRes(pix, dpi);
|
|
||||||
l_uint32* data = pixGetData(pix);
|
|
||||||
int wpl = pixGetWpl(pix);
|
|
||||||
switch (bpp)
|
|
||||||
{
|
|
||||||
case 1:
|
|
||||||
for (int y = 0; y < height; ++y, data += wpl, imagedata += bytes_per_line)
|
|
||||||
for (int x = 0; x < width; ++x)
|
|
||||||
if (imagedata[x / 8] & (0x80 >> (x % 8)))
|
|
||||||
CLEAR_DATA_BIT(data, x);
|
|
||||||
else
|
|
||||||
SET_DATA_BIT(data, x);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 8:
|
|
||||||
// Greyscale just copies the bytes in the right order.
|
|
||||||
for (int y = 0; y < height; ++y, data += wpl, imagedata += bytes_per_line)
|
|
||||||
for (int x = 0; x < width; ++x)
|
|
||||||
SET_DATA_BYTE(data, x, imagedata[x]);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 24:
|
|
||||||
// Put the colors in the correct places in the line buffer.
|
|
||||||
for (int y = 0; y < height; ++y, imagedata += bytes_per_line)
|
|
||||||
for (int x = 0; x < width; ++x, ++data) {
|
|
||||||
SET_DATA_BYTE(data, COLOR_RED, imagedata[3 * x]);
|
|
||||||
SET_DATA_BYTE(data, COLOR_GREEN, imagedata[3 * x + 1]);
|
|
||||||
SET_DATA_BYTE(data, COLOR_BLUE, imagedata[3 * x + 2]);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 32:
|
|
||||||
// Maintain byte order consistency across different endianness.
|
|
||||||
for (int y = 0; y < height; ++y, imagedata += bytes_per_line, data += wpl)
|
|
||||||
for (int x = 0; x < width; ++x)
|
|
||||||
data[x] = static_cast<l_uint32>((imagedata[x * 4] << 24) | (imagedata[x * 4 + 1] << 16) |
|
|
||||||
(imagedata[x * 4 + 2] << 8) | imagedata[x * 4 + 3]);
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
pix->informat = bytes_per_pixel == 1 ? 1 : 2;
|
|
||||||
if (bytes_per_pixel == 1)
|
|
||||||
{
|
|
||||||
PIXCMAP* colormap = pixcmapCreate(8);
|
|
||||||
LEPT_FREE(colormap->array);
|
|
||||||
colormap->array = reinterpret_cast<l_uint8*>(LEPT_CALLOC(256, sizeof(RGBA_QUAD)));
|
|
||||||
colormap->n = 256;
|
|
||||||
colormap->nalloc = 256;
|
|
||||||
colormap->depth = 8;
|
|
||||||
l_uint8* ptr = reinterpret_cast<l_uint8*>(colormap->array);
|
|
||||||
for (int i = 0; i < 256; i++)
|
|
||||||
ptr[i * 4 + 0] = ptr[i * 4 + 1] = ptr[i * 4 + 2] = ptr[i * 4 + 3] = static_cast<l_uint8>(i);
|
|
||||||
pixSetColormap(pix, colormap);
|
|
||||||
}
|
|
||||||
pixSetXRes(pix, 200);
|
|
||||||
pixSetYRes(pix, 200);
|
|
||||||
return pix;
|
|
||||||
}
|
|
||||||
|
|
||||||
HG_OCR::HG_OCR()
|
|
||||||
: m_ptr(nullptr)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
HG_OCR::~HG_OCR()
|
|
||||||
{
|
|
||||||
if (m_ptr)
|
|
||||||
{
|
|
||||||
reinterpret_cast<tesseract::TessBaseAPI*>(m_ptr)->End();
|
|
||||||
delete reinterpret_cast<tesseract::TessBaseAPI*>(m_ptr);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int HG_OCR::init(const char* trainFile, RECOGNITION_MODE mode)
|
|
||||||
{
|
|
||||||
if (m_ptr) delete reinterpret_cast<tesseract::TessBaseAPI*>(m_ptr);
|
|
||||||
tesseract::TessBaseAPI* api = new tesseract::TessBaseAPI();
|
|
||||||
api->SetPageSegMode(tesseract::PSM_AUTO_OSD);
|
|
||||||
m_ptr = reinterpret_cast<void*>(api);
|
|
||||||
return api->Init(trainFile, mode == RECOGNITION_OCR ? "chi_sim" : "osd");
|
|
||||||
}
|
|
||||||
|
|
||||||
bool HG_OCR::ocr(const char** inputFileNames, int numOfFiles, const char* outputFileName, SAVE_MODE flag)
|
|
||||||
{
|
|
||||||
if (m_ptr == nullptr) return false;
|
|
||||||
if (inputFileNames == nullptr || numOfFiles == 0) return false;
|
|
||||||
|
|
||||||
tesseract::PointerVector<tesseract::TessResultRenderer> renderers;
|
|
||||||
tesseract::TessBaseAPI *api = reinterpret_cast<tesseract::TessBaseAPI*>(m_ptr);
|
|
||||||
if (flag & SAVE_PDF)
|
|
||||||
renderers.push_back(new tesseract::TessPDFRenderer(outputFileName, api->GetDatapath(), false));
|
|
||||||
if (flag & SAVE_TXT)
|
|
||||||
renderers.push_back(new tesseract::TessTextRenderer(outputFileName));
|
|
||||||
|
|
||||||
#ifdef USE_QT
|
|
||||||
QTime timer;
|
|
||||||
timer.start();
|
|
||||||
#endif
|
|
||||||
|
|
||||||
int numOfRenderer = renderers.length();
|
|
||||||
for (int i = 0; i < numOfRenderer; i++)
|
|
||||||
renderers[i]->BeginDocument("");
|
|
||||||
|
|
||||||
for (int i = 0; i < numOfFiles; i++)
|
|
||||||
{
|
|
||||||
Pix* img = pixRead(inputFileNames[i]);
|
|
||||||
api->SetInputName(outputFileName);
|
|
||||||
api->SetImage(img);
|
|
||||||
api->Recognize(nullptr);
|
|
||||||
for (int i = 0; i < numOfRenderer; i++)
|
|
||||||
renderers[i]->AddImage(api);
|
|
||||||
pixDestroy(&img);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int i = 0; i < numOfRenderer; i++)
|
|
||||||
renderers[i]->EndDocument();
|
|
||||||
|
|
||||||
renderers.clear();
|
|
||||||
|
|
||||||
#ifdef USE_QT
|
|
||||||
qDebug() << timer.elapsed();
|
|
||||||
#endif
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
int HG_OCR::orientation(const unsigned char *data, int width, int height, int channels, int dpi)
|
|
||||||
{
|
|
||||||
Pix* pix = createPix(data, width, height, channels, (width * channels + 3) / 4 * 4, dpi);
|
|
||||||
tesseract::TessBaseAPI *api = reinterpret_cast<tesseract::TessBaseAPI*>(m_ptr);
|
|
||||||
api->SetImage(pix);
|
|
||||||
int orient_deg = 1;
|
|
||||||
api->DetectOrientationScript(&orient_deg, nullptr, nullptr, nullptr);
|
|
||||||
pixDestroy(&pix);
|
|
||||||
return orient_deg;
|
|
||||||
}
|
|
|
@ -1,85 +0,0 @@
|
||||||
/*
|
|
||||||
* ====================================================
|
|
||||||
|
|
||||||
* 功能:本地图片进行OCR,并生成多页PDF功能
|
|
||||||
* 作者:刘丁维
|
|
||||||
* 生成时间:2020/3/5
|
|
||||||
* 最近修改时间:2020/3/5
|
|
||||||
* 版本号:v1.0
|
|
||||||
|
|
||||||
* ====================================================
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef HG_GPDF_H
|
|
||||||
#define HG_GPDF_H
|
|
||||||
|
|
||||||
#if defined (_WIN32)
|
|
||||||
#if !defined (HG_GPDF_API_BUILD)
|
|
||||||
#define HG_GPDF_API //__declspec(dllexport)
|
|
||||||
#else
|
|
||||||
#define HG_GPDF_API //__declspec(dllimport)
|
|
||||||
#endif
|
|
||||||
#else
|
|
||||||
#define HG_GPDF_API
|
|
||||||
#endif
|
|
||||||
|
|
||||||
class HG_GPDF_API HG_OCR
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
|
|
||||||
//识别模式
|
|
||||||
enum RECOGNITION_MODE
|
|
||||||
{
|
|
||||||
RECOGNITION_OSD, //文稿方向识别模式
|
|
||||||
RECOGNITION_OCR //字符识别模式
|
|
||||||
};
|
|
||||||
|
|
||||||
//字符识别保存模式
|
|
||||||
enum SAVE_MODE
|
|
||||||
{
|
|
||||||
SAVE_PDF = 0x01, //PDF保存字符识别结果
|
|
||||||
SAVE_TXT = 0x02, //TXT保存字符识别结果
|
|
||||||
SAVE_PDF_TXT = 0x03 //同时采用PDF和TXT保存字符识别结果
|
|
||||||
};
|
|
||||||
public:
|
|
||||||
|
|
||||||
HG_OCR();
|
|
||||||
|
|
||||||
~HG_OCR();
|
|
||||||
|
|
||||||
/*
|
|
||||||
* 函数功能:初始化PDF生成器
|
|
||||||
* trainFile:[in] 训练库文件路径,不含文件名,末尾无需'/'结尾
|
|
||||||
* mode:[in] 具体选项参照 enum RECOGNITION_MODE,默认值为RECOGNITION_OCR
|
|
||||||
* 返回值:0为成功,否则为异常。异常查看打印输出
|
|
||||||
*/
|
|
||||||
int init(const char* trainFile, RECOGNITION_MODE mode = RECOGNITION_OCR);
|
|
||||||
|
|
||||||
/*
|
|
||||||
* 函数功能:字符识别,并生成文本
|
|
||||||
* inputFileNames:[in] 本地待识别图片完整路径,可同时输入多个图片路径
|
|
||||||
* numOfFiles:[in] inputFileNames长度
|
|
||||||
* numOfFiles:[in] inputFileNames长度
|
|
||||||
* outputFileName:[in] 识别结果保存路径,包含文件基础名字,不含文件后缀名
|
|
||||||
* flag:[in] 字符识别保存模式,参照 enum SAVE_MODE
|
|
||||||
* 返回值:true为成功,否则为异常。异常查看打印输出
|
|
||||||
*/
|
|
||||||
bool ocr(const char** inputFileNames, int numOfFiles, const char* outputFileName, SAVE_MODE flag = SAVE_PDF);
|
|
||||||
|
|
||||||
/*
|
|
||||||
* 函数功能:文本方向识别
|
|
||||||
* data:[in] 图像数据指针
|
|
||||||
* width:[in] 图像宽度
|
|
||||||
* height:[in] 图像高度
|
|
||||||
* channels:[in] 图像通道
|
|
||||||
* dpi:[in] 图片DPI
|
|
||||||
* 返回值:以顺时针进行计数,输出0,90,180,270四种结果
|
|
||||||
*/
|
|
||||||
int orientation(const unsigned char* data, int width, int height, int channels, int dpi);
|
|
||||||
|
|
||||||
private:
|
|
||||||
void* m_ptr;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
#endif //HG_GPDF_H
|
|
Loading…
Reference in New Issue