• 欢迎大家交换友链,可在https://www.stubbornhuang.com/申请友情链接进行友链交换申请!

  • 计算机图形学与计算几何经典必备书单整理,下载链接可参考:https://www.stubbornhuang.com/1256/

  • 问题反馈可发送邮件到stubbornhuang@qq.com

  • 工资「喂饱肚子」,副业「养活灵魂」!

  • 本站由于前段时间遭受到大量临时和国外邮箱注册,所以对可注册的邮箱类型进行了限制!

  • 如果觉得本站的内容有帮助,可以考虑打赏博主哦!

  • 感谢大家访问本站,希望本站的内容可以帮助到大家!

  • 在本站开通年度VIP,无限制下载本站资源和阅读本站文章

  • 本站会放置Google广告用于维持域名以及网站服务器费用。

C++ – 对字符串和图片进行base64编解码

C++ 发布于2023-02-09 阅读 8,175次 0次评论 0次点赞 本文共3017个字,阅读需要8分钟。

1 cpp-base64

本次要使用的是Github上开源的base64编解码库:https://github.com/ReneNyffenegger/cpp-base64,也是我在实际项目中经常使用的base64编解码库,基本上满足了我的需求。

在Github官方仓库中提供了如何使用该库的官方页面:https://renenyffenegger.ch/notes/development/Base64/Encoding-and-decoding-base-64-with-cpp/,在本文中我们也会简要的介绍它所提供的api和使用方法,并且使用该库对字符串图片进行base64编解码。

1.1 cpp-base64的api函数

cpp-base64库提供了以下4个base64编码函数

std::string base64_encode     (std::string const& s, bool url = false);
std::string base64_encode     (unsigned char const*, size_t len, bool url = false);
std::string base64_encode_pem (std::string const& s);
std::string base64_encode_mime(std::string const& s);

一个base解码函数

std::string base64_decode(std::string const& s, bool remove_linebreaks = false);

1.1.1 编码函数

std::string base64_encode     (std::string const& s, bool url = false);
std::string base64_encode     (unsigned char const*, size_t len, bool url = false);

都可以将数据编码为base64,并返回编码后的字符串std::string,二者的区别是

std::string base64_encode     (std::string const& s, bool url = false);

以std::string作为输入参数,而

std::string base64_encode     (unsigned char const*, size_t len, bool url = false);

unsigned const char*和数据长度len作为输入参数,这两个函数都有一个参数url,该参数决定编码后的字符串是否需要在Url中使用,因为Url中需要去除一些特殊的字符,比如/等,这和普通的base64编码不一样。

我们再来看

std::string base64_encode_pem (std::string const& s);
std::string base64_encode_mime(std::string const& s);

这两个编码函数,这两个编码函数也可以将数据编码为base64,但是与上面两个函数的区别是,base64_encode_pem函数会在每个第64个(pem)编码字符后插入一个换行符,而base64_encode_mime则会在每个第76个(mime)编码字符后插入换行符。

1.1.2 解码函数

解码函数为

std::string base64_decode(std::string const& s, bool remove_linebreaks = false);

其中参数s为base64编码后的字符串,如果编码后的字符串中包含换行符,则需要将remove_linebreaks设置为true,否则为false。

2 使用cpp-base64对字符串和图片进行base64编解码

2.1 对字符串进行编解码

下面是一个使用cpp-base64对字符串进行base64编解码的示例

#include <iostream>
#include <string>

#include "base64.h"

void Test_Str(const std::string& origin_str)
{
    std::string encode_str = base64_encode(origin_str);
    std::cout << "字符串base64编码后 : " << encode_str << std::endl;

    std::string decode_str = base64_decode(encode_str);
    std::cout << "字符串base64解码后 : " << decode_str << std::endl;
}

int main()
{
    Test_Str("Hello World");

    return 0;
}

输出

字符串base64编码后 : SGVsbG8gV29ybGQ=
字符串base64解码后 : Hello World

2.2 对图片进行编解码

下面是一个使用opencv读取图片,然后使用cpp-base64对图片数据进行base64编解码的示例

#include <iostream>
#include <string>

#include "base64.h"
#include "opencv/cv.hpp"

void Test_Image(const std::string& image_path)
{
    cv::Mat image = cv::imread(image_path);
    std::vector<uchar> image_buf;
    cv::imencode(".jpg", image, image_buf);

    std::string image_encode = base64_encode(image_buf.data(), image_buf.size());
    std::cout << "图片base64编码后 : " << image_encode << std::endl;

    std::string image_decode = base64_decode(image_encode);
    std::cout << "图片base64解码后 : " << image_decode << std::endl;

    std::vector<uchar> data(image_decode.begin(), image_decode.end());
    cv::Mat img = cv::imdecode(cv::Mat(data), 1);

    cv::imwrite("out.jpg", img);
}

int main()
{
    Test_Image("input.jpg");

    return 0;
}

在上述示例中,使用opencv读取本地图片之后,使用cv::imencode将读取的图片编码为jpg格式,然后使用image_buf存储相应的值,然后使用cpp-base64进行编码。解码过程很简单就是使用base64先解码成std::string,然后将std::string转为uchar,最后使用cv::imdecode解码为cv::Mat,然后将图片保存到本地验证是否正确。

参考链接

欢迎扫码关注我的微信公众号,及时获取文章更新

微信公众号二维码

本文作者:StubbornHuang

版权声明:本文为站长原创文章,如果转载请注明原文链接!

原文标题:C++ – 对字符串和图片进行base64编解码

原文链接:https://www.stubbornhuang.com/2501/

发布于:2023年02月09日 11:32:09

修改于:2023年06月21日 17:11:42

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

文章末尾
上一篇
网站推荐 - 注册chatGPT无法接收验证短信?使用便宜好用的sms-activate
工具软件网站
下一篇
Chrome浏览器启动很慢的原因,不要开硬件加速模式!
其他
当前分类随机文章推荐

发表评论

您必须 [ 登录 ] 才能发表留言!

关注我们的公众号

微信公众号