C++ – 对字符串和图片进行base64编解码
本文作者:StubbornHuang
版权声明:本文为站长原创文章,如果转载请注明原文链接!
原文标题:C++ – 对字符串和图片进行base64编解码
原文链接:https://www.stubbornhuang.com/2501/
发布于:2023年02月09日 11:32:09
修改于:2023年02月09日 14:00:00

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
,然后将图片保存到本地验证是否正确。
参考链接
当前分类随机文章推荐
- Windows - 使用类的成员函数作为Win32窗口消息回调处理函数WindowProc 阅读914次,点赞0次
- C++ - 得到字符串中某个字符串出现的个数 阅读3843次,点赞2次
- C++STL容器 - std::map删除指定元素 阅读1698次,点赞0次
- C++ – 字节数组byte[]或者unsigned char[]与bool的相互转换 阅读863次,点赞1次
- CMake - 设置Debug或者Release编译模式 阅读2125次,点赞0次
- C++ - queue存储动态指针时正确释放内存 阅读5234次,点赞2次
- C++ – 字节数组byte[]或者unsigned char[]与long double的相互转换 阅读916次,点赞0次
- C++ - 使用Crypto++/CryptoPP加解密库对字符串或者文件进行AES256加密 阅读2981次,点赞1次
- C++ - 在某一天某个时间点定时执行任务,比如2022年9月19日晚上9点准点执行发送邮件函数 阅读307次,点赞0次
- C++ - 求解std::vector
中topk数值以及topk数值对应的索引 阅读2187次,点赞0次
全站随机文章推荐
- 资源分享 - Qt5开发及实例(第3版)陆文周主编高清PDF下载 阅读8258次,点赞6次
- Visual Studio - 将程序的日志输出到Visual Studio即时窗口 阅读4739次,点赞1次
- Pip - 常用命令(安装,卸载,升级第三方库) 阅读3131次,点赞1次
- Pytorch - 使用torch.matmul()替换torch.einsum('nctw,cd->ndtw',(a,b))算子模式 阅读1759次,点赞0次
- WordPress - PhpStudy本地环境修改固定链接打不开网页404错误 阅读4322次,点赞1次
- Modern OpenGL从零开始 - 从茫茫多的OpenGL第三方库讲起 阅读3421次,点赞1次
- C++11/std::thread - 可作为线程函数的几种方式总结 阅读3472次,点赞1次
- C++11 - std::shared_ptr初始化的几种方式 阅读6884次,点赞2次
- C++11 - 快速学会正则表达式 阅读1259次,点赞2次
- WordPress - 修改WP Editor.md markdown编辑器插件从剪切板粘贴图片上传的清晰度和质量 阅读930次,点赞0次
评论
167