环境
ubuntu 20.04
cmake 3.20
tencentcloud-cpp-sdk
nlohmann/json.hpp
安装Tencent-cpp-sdk
参考官方文档
安装nlohmann/json.hpp
参考官方文档
项目结构
.
├── build
├── CMakeLists.txt
└── src
├── CMakeLists.txt
├── include
│ └── base64.h
├── ocrimg.cpp
3 directories, 4 files
外部的CMakeLists.txt
cmake_minimum_required (VERSION 3.20)
project(OCRTest)
set(CMAKE_CXX_STANDARD 11)
link_directories(/usr/local/lib)
add_subdirectory(src bin)
内部的CMakeLists.txt
find_package(nlohmann_json 3.6.0 REQUIRED)
add_executable(ocrimg ocrimg.cpp)
target_link_libraries(
ocrimg
tencentcloud-sdk-cpp-ocr
tencentcloud-sdk-cpp-core
nlohmann_json::nlohmann_json
)
ocrimg.cpp
#include <tencentcloud/core/Credential.h>
#include <tencentcloud/core/profile/ClientProfile.h>
#include <tencentcloud/core/profile/HttpProfile.h>
#include <tencentcloud/ocr/v20181119/OcrClient.h>
#include <tencentcloud/ocr/v20181119/model/GeneralAccurateOCRRequest.h>
#include <tencentcloud/ocr/v20181119/model/GeneralAccurateOCRResponse.h>
#include <iostream>
#include <string>
#include <fstream>
#include "include/base64.h"
#include <nlohmann/json.hpp>
using namespace std;
using json = nlohmann::json;
using namespace TencentCloud;
using namespace TencentCloud::Ocr::V20181119;
using namespace TencentCloud::Ocr::V20181119::Model;
int main(int argc, char **argv) {
if (argc != 2) {
perror("mast enter fail name");
return 1;
}
Credential cred = Credential("xxxxxxxxxxxxxxxxxxxxxxxxx", "xxxxxxxxxx");
HttpProfile httpProfile = HttpProfile();
httpProfile.SetEndpoint("ocr.tencentcloudapi.com");
ClientProfile clientProfile = ClientProfile();
clientProfile.SetHttpProfile(httpProfile);
OcrClient client = OcrClient(cred, "ap-beijing", clientProfile);
GeneralAccurateOCRRequest req = GeneralAccurateOCRRequest();
fstream f, f2;
f.open(argv[1], ios::in | ios::binary);
f.seekg(0