#include #include "core/doc_scanner.hpp" #include "native_structs.h" //typedef struct { // int32_t x; // int32_t y; //} Coordinate; // //typedef struct { // Coordinate* coordsPtr; // uint32_t length; //} CoordinateArray; typedef struct { int32_t left; int32_t top; int32_t width; int32_t height; } ROI; typedef struct { uint8_t *buf; int32_t height; int32_t width; const char *format; int32_t bytesPerRow; ROI *roiPtr; } NativeCameraImage; extern "C" __attribute__((visibility("default"))) __attribute__((used)) intptr_t initDocFinder(const char* doc_model_path, const char* corner_model_path, float precise = 0.85) { auto *docFinderPtr = new DocScanner(doc_model_path, corner_model_path, precise); return (intptr_t)docFinderPtr; } extern "C" __attribute__((visibility("default"))) __attribute__((used)) int findDocWithCameraImg(CoordinateArray *coordList, NativeCameraImage *cameraImgPtr, intptr_t _docScannerPtr, const char *tmpDir) { //#ifdef DEBUG // int64_t start_time = cv::getTickCount(); //#endif cv::Mat rgbaMat = cv::Mat(cameraImgPtr->height, cameraImgPtr->width, CV_8UC4, cameraImgPtr->buf, cameraImgPtr->bytesPerRow); ROI *roiPtr = cameraImgPtr->roiPtr; cv::Rect roi(roiPtr->left, roiPtr->top, roiPtr->width, roiPtr->height); rgbaMat = rgbaMat(roi); // std::string rootDir(tmpDir); // std::string dstPath = rootDir + "/test.jpg"; // std::cout << "ret:" << cv::imwrite(dstPath.c_str(), rgbaMat) << std::endl; auto *docScannerPtr = (DocScanner *)_docScannerPtr; std::vector points; docScannerPtr->scan(rgbaMat, points); bool succ = points.size() == coordList->length; if (succ) { float width = rgbaMat.size().width; float height = rgbaMat.size().height; for (size_t i = 0; i < coordList->length; i++) { coordList->coordsPtr[i].x = points[i].x / width; coordList->coordsPtr[i].y = points[i].y / height; } } //#ifdef DEBUG // float elapsedTime = (cv::getTickCount() - start_time) / cv::getTickFrequency(); // float fps = 1 / elapsedTime; // std::cout << "findDocPoints fps:" << fps << std::endl; //#endif return succ; } extern "C" __attribute__((visibility("default"))) __attribute__((used)) int findDocWithImgPath(const char *imgPath, const char *dstPath, intptr_t _docFinderPtr, CoordinateArray *coordList, ROI *roiPtr) { auto *docScannerPtr = (DocScanner *)_docFinderPtr; cv::Mat bgr = cv::imread(imgPath), rgb; cv::Rect roi(roiPtr->left, roiPtr->top, roiPtr->width, roiPtr->height); bgr = bgr(roi); cv::cvtColor(bgr, rgb, cv::COLOR_BGR2RGB); std::vector points; docScannerPtr->scan(rgb, points); bool succ = points.size() == coordList->length; if (succ) { float width = rgb.size().width; float height = rgb.size().height; for (size_t i = 0; i < coordList->length; i++) { coordList->coordsPtr[i].x = points[i].x / width; coordList->coordsPtr[i].y = points[i].y / height; } cv::imwrite(dstPath, bgr); } return succ; } extern "C" __attribute__((visibility("default"))) __attribute__((used)) void deleteDocFinder(intptr_t _docFinderPtr) { #ifdef DEBUG std::cout << "deleteDocFinder" << std::endl; #endif auto *docFinderPtr = (DocScanner *)_docFinderPtr; delete docFinderPtr; }