C++ 图片base64编解码 && Tencent OCR API打造属于自己的OCR软件

这篇博客介绍了如何在C++环境中进行图片的Base64编码和解码,并结合Tencent OCR API来创建一个简单的OCR应用。首先,文章详细讲解了安装过程和项目结构,接着阐述了编译链接步骤,最后展示了测试成功并能跨平台使用的成果。
摘要由CSDN通过智能技术生成

环境

  • 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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值