Base64编码:
Base64编码是一种基于 64 个可打印字符来表示二进制数据的一种方法。字符选用了"A-Z、a-z、0-9、+、/" 64个可打印字符,这是标准的Base64协议规定。在日常使用中我们还会看到“=”或“==”号出现在Base64的编码结果中,“=”在此是作为填充字符出现,在最后的字节数不足三个的时候会用“=”补足。
对于Base64编码的具体实现可以参考:一篇文章彻底弄懂Base64编码原理
C++实现图片转换成Base64编码:
通过此方法,我们可以将图片转换成base64编码,可以打印在控制台,也可以输出到一个文本文件里面。但需要注意的是,一张1MB的图片,经过base64编码后,长度在10万以上,所以我们会选择后一种输出方式,便于保存。图片的位置建议放于cpp程序的同一目录下,代码中图片的名称也需要替换掉。
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;
static const string base64_chars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/";
//此方法是用来判断一个字符是否属于Base64字符集的
static inline bool is_base64(unsigned char c) {
return (isalnum(c) || (c == '+') || (c == '/'));
}
//获取图片的ASSLL码后,再根据Base64编码规则进行编码
string base64_encode(unsigned char const* bytes_to_encode, unsigned int in_len) {
string ret;
int i = 0;
int j = 0;
unsigned char char_array_3[3];
unsigned char char_array_4[4];
while (in_len--) {
char_array_3[i++] = *(bytes_to_encode++);
if (i == 3) {
char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
char_array_4[3] = char_array_3[2] & 0x3f;
for (i = 0; (i < 4); i++) {
ret += base64_chars[char_array_4[i]];
}
i = 0;
}
}
if (i) {
for (j = i; j < 3; j++) {
char_array_3[j] = '\0';
}
char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
char_array_4[3] = char_array_3[2] & 0x3f;
for (j = 0; (j < i + 1); j++) {
ret += base64_chars[char_array_4[j]];
}
while ((i++ < 3)) {
ret += '=';
}
}
return ret;
}
int main() {
// 此路径换成图片文件路径
const string file_path = "input_image.png";
// 打开文件以读取二进制数据
ifstream image_file(file_path, ios::in | ios::binary);
if (!image_file.is_open()) {
cerr << "无法打开文件: " << file_path << endl;
return 1;
}
// 将文件数据读取到缓冲区
vector<unsigned char> buffer((istreambuf_iterator<char>(image_file)), istreambuf_iterator<char>());
image_file.close();
// 对缓冲区数据进行Base64编码
string encoded_string = base64_encode(buffer.data(), buffer.size());
// 可以选择控制台输出Base64编码字符串
//cout << "Base64编码字符串: " << endl << encoded_string << endl;
// 将结果保存到文件或者其他操作
ofstream out_file("output.txt");
out_file << encoded_string;
out_file.close();
//提示信息
cout << "生成成功" << endl;
return 0;
}
C++实现Base64转图片:
将图片转换成baes编码以后,我们可以按照编码规则将其转换成图片,此处需要注意的是,由于编码的长度过长,编译器是无法直接加载的,所以我们不能将其直接放在代码里面读取,需要使用流的处理方式,通过读取上文生成的txt文件来进行解码。此处存放base64编码的txt文件也需要置于cpp程序同一目录下,代码中文件名称也需要进行更改。
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <stdexcept>
using namespace std;
static const string base64_chars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/";
//此方法是用来判断一个字符是否属于Base64字符集的
static inline bool is_base64(unsigned char c) {
return (isalnum(c) || (c == '+') || (c == '/'));
}
//接收一个经过Base64编码的字符串作为参数,返回解码后的原始数据,遍历输入的字符串,
//从中提取出每个Base64字符,并根据Base64解码规则还原原始数据。
string base64_decode(const string& encoded_string) {
int in_len = encoded_string.size();
int i = 0;
int j = 0;
int in_ = 0;
unsigned char char_array_4[4], char_array_3[3];
string ret;
while (in_len-- && (encoded_string[in_] != '=') && is_base64(encoded_string[in_])) {
char_array_4[i++] = encoded_string[in_]; in_++;
if (i == 4) {
for (i = 0; i < 4; i++) {
char_array_4[i] = base64_chars.find(char_array_4[i]);
}
char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
for (i = 0; (i < 3); i++) {
ret += char_array_3[i];
}
i = 0;
}
}
if (i) {
for (j = i; j < 4; j++) {
char_array_4[j] = 0;
}
for (j = 0; j < 4; j++) {
char_array_4[j] = base64_chars.find(char_array_4[j]);
}
char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
for (j = 0; (j < i - 1); j++) {
ret += char_array_3[j];
}
}
return ret;
}
int main() {
// 示例Base64编码字符串
ifstream file("output.txt");
string base64_string;
if (file.is_open()) {
while (getline(file, base64_string)) {
}
file.close();
}
// 解码Base64字符串
string decoded_string = base64_decode(base64_string);
// 将解码后的数据写入文件
ofstream image_file("output_image.png", ios::out | ios::binary);
//如果成功打开文件就将解码后的数据写入文件里面,关闭文件流
//打开失败就输出错误信息
if (image_file.is_open()) {
image_file.write(decoded_string.c_str(), decoded_string.size());
image_file.close();
cout << "图片已成功保存为 output_image.png" << endl;
}
else {
cerr << "无法打开文件以写入图片" << endl;
}
return 0;
}
图片与base64编码的txt文件位置大概如下所示:
对于一些不方便直接保存在相册的照片,可以进行编码后保存。感兴趣的小伙伴可以自行进行尝试