在许多工程、设计和文档处理场景中,图纸包含了大量有价值的信息。然而,手动从图纸中提取信息并录入到 Excel 表格中是一项繁琐且容易出错的工作,效率极低。
编辑有许多PDF图纸,需要提取所有页面中每一页(如下图实例图片的黄色区域)的指定区域信息到EXCEL中,大量图纸就没办法操作嘞!
实现方案详细步骤
libcurl
来进行 HTTP 请求,用于调用腾讯云 API。nlohmann/json
来处理腾讯云 API 返回的 JSON 数据。libxl
来创建和操作 Excel 文件。遍历指定文件夹下的所有图纸文件,将其读取为二进制数据。
将图纸的二进制数据进行 Base64 编码,然后使用 libcurl
发送 HTTP 请求到腾讯云文字识别 API,获取识别结果。
使用 nlohmann/json
解析腾讯云 API 返回的 JSON 数据,提取识别出的文字内容。
使用 libxl
创建 Excel 文件,并将识别结果写入到 Excel 表格中。
cpp
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <curl/curl.h>
#include <nlohmann/json.hpp>
#include <libxl.h>
#include <base64.h>
// 回调函数,用于处理 HTTP 响应
size_t WriteCallback(void *contents, size_t size, size_t nmemb, std::string *s) {
size_t newLength = size * nmemb;
try {
s->append((char*)contents, newLength);
} catch(std::bad_alloc &e) {
return 0;
}
return newLength;
}
// 调用腾讯云文字识别 API
std::string recognizeImage(const std::string& imageBase64, const std::string& secretId, const std::string& secretKey) {
CURL *curl;
CURLcode res;
std::string readBuffer;
curl = curl_easy_init();
if(curl) {
std::string url = "https://ocr.tencentcloudapi.com/";
std::string postFields = "{\"ImageBase64\":\"" + imageBase64 + "\",\"Action\":\"GeneralBasicOCR\",\"Version\":\"2018-11-19\",\"Region\":\"ap-guangzhou\",\"SecretId\":\"" + secretId + "\",\"Timestamp\":" + std::to_string(time(nullptr)) + ",\"Nonce\":123456,\"SignatureMethod\":\"HmacSHA256\",\"RequestClient\":\"SDK_PHP_3.0\"}";
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postFields.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
res = curl_easy_perform(curl);
if(res != CURLE_OK) {
std::cerr << "curl_easy_perform() failed: " << curl_easy_strerror(res) << std::endl;
}
curl_easy_cleanup(curl);
}
return readBuffer;
}
// 读取文件为二进制数据
std::string readFile(const std::string& filename) {
std::ifstream file(filename, std::ios::binary);
std::string content((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
return content;
}
// 导出识别结果到 Excel 文件
void exportToExcel(const std::vector<std::string>& results, const std::string& excelFilename) {
libxl::Book* book = xlCreateBook();
if (book) {
libxl::Sheet* sheet = book->addSheet("Sheet1");
if (sheet) {
for (size_t i = 0; i < results.size(); ++i) {
sheet->writeStr(i, 0, results[i].c_str());
}
}
book->save(excelFilename.c_str());
book->release();
}
}
int main() {
std::string secretId = "your_secret_id";
std::string secretKey = "your_secret_key";
std::string folderPath = "path/to/your/image/folder";
std::string excelFilename = "output.xlsx";
std::vector<std::string> recognitionResults;
// 遍历文件夹中的所有图纸文件
// 这里简单假设所有文件都是图纸文件
// 实际应用中需要进行文件类型过滤
std::ifstream dir(folderPath);
std::string filename;
while (std::getline(dir, filename)) {
std::string filePath = folderPath + "/" + filename;
std::string imageData = readFile(filePath);
std::string imageBase64 = base64_encode(reinterpret_cast<const unsigned char*>(imageData.c_str()), imageData.length());
std::string response = recognizeImage(imageBase64, secretId, secretKey);
nlohmann::json jsonResponse = nlohmann::json::parse(response);
if (jsonResponse.contains("Response") && jsonResponse["Response"].contains("TextDetections")) {
for (const auto& detection : jsonResponse["Response"]["TextDetections"]) {
std::string text = detection["DetectedText"].get<std::string>();
recognitionResults.push_back(text);
}
}
}
// 导出识别结果到 Excel 文件
exportToExcel(recognitionResults, excelFilename);
return 0;
}
libcurl
的 HTTP 响应,将响应数据存储到 std::string
中。std::string
。libxl
创建 Excel 文件,并将识别结果写入到 Excel 表格中。your_secret_id
和 your_secret_key
替换为你自己的腾讯云 API 密钥。path/to/your/image/folder
替换为实际的图纸文件夹路径。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。