#include #include #include "core/doc_img_cleaner.hpp" #import "utils/log_util.h" extern "C" __attribute__((visibility("default"))) __attribute__((used)) int32_t native_add(uint8_t *src, uchar **dst, int32_t width, int32_t height) { cv::Mat matImg = cv::imdecode(cv::Mat(1, width, CV_8UC1, src), cv::IMREAD_UNCHANGED); int channels = matImg.channels(); cv::Mat gray; cv::cvtColor(matImg, gray, cv::COLOR_BGRA2GRAY); std::vector buf; cv::imencode(".png", gray, buf); size_t size = buf.size(); *dst = (uchar *)malloc(size * sizeof(uchar)); for (size_t i = 0; i < size; i++) { (*dst)[i] = buf[i]; } return size; } extern "C" __attribute__((visibility("default"))) __attribute__((used)) intptr_t init(const char* modelPath) { auto *docImgCleanerPtr = new DocImgCleaner(modelPath); return (intptr_t)docImgCleanerPtr; } extern "C" __attribute__((visibility("default"))) __attribute__((used)) void release(intptr_t docImgCleanerAdr) { auto *docImgCleanerPtr = (DocImgCleaner *)docImgCleanerAdr; delete docImgCleanerPtr; } extern "C" __attribute__((visibility("default"))) __attribute__((used)) bool process(const char* imgPath, const char* dstImgPath, intptr_t docImgCleanerAdr) { #ifdef DEBUG auto start_time = cv::getTickCount(); #endif cv::Mat bgrImg = cv::imread(imgPath); int maxWidth = 2560; if (bgrImg.size().width > maxWidth) { float aspectRatio = bgrImg.size().width / (float)bgrImg.size().height; int width = maxWidth; int height = width / aspectRatio; cv::resize(bgrImg, bgrImg, cv::Size(width, height)); } auto *docimgCleaner = (DocImgCleaner *)docImgCleanerAdr; cv::Mat processedImg = docimgCleaner->process(bgrImg); // auto _imgPath = std::string(imgPath); // auto processedImgPath = _imgPath.substr(0, _imgPath.find_last_of("/")) + "/processedImg.jpg"; cv::cvtColor(processedImg, processedImg, cv::COLOR_RGB2BGR); auto succ = cv::imwrite(dstImgPath, processedImg); #ifdef DEBUG LOGW("doc_image_cleaner", "total time:%f", (cv::getTickCount() - start_time) / cv::getTickFrequency()); #endif return succ; }