将图像.jpg转换为 Base64
Convert image.jpg to Base64
我工作的公司要求我找到一种将图像转换为Base64的方法。基本上,有一个相机将以JPG格式拍照,我需要在Base64中转换该JPG图片,以便我可以通过PLC程序发送数据并在应用程序端重建它,这将是一个Web应用程序。
然后我将不得不做:
document.getElementById("ImageLoad").src = "data:image/png;base64," + Bytes;
在Javascript中做Jquery。
我尝试将ifstream与ios::in | ios::binary一起使用,只是读取文件并输出结果,但它不起作用。
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
string line;
ifstream input("test.jpg", ios::in | ios::binary);
ofstream output("text.txt");
if (input.is_open()) {
while (getline(input,line)) {
output << line;
}
input.close();
}
}
我期待如下输出:
/9j/4AAQSkZJRgABAQEASABIAAD/2wBDAAYEBAQFBA
但是我得到一个长字符串,看起来像这样:
}!× ÙÝÜ÷åXŠmŒš@Õä ‡6gxD1;*wïµ¼4 ÒÑôÿ ¿OÑúx0¥ˆ‘ÀÃõûóC
这对
我有用:https://renenyffenegger.ch/notes/development/Base64/Encoding-and-decoding-base-64-with-cpp
我不敢相信C++标准库中没有base64功能!
#include <fstream>
#include <string>
#include "base64.h"
using namespace std;
int main()
{
string line;
ifstream input("test.jpg", ios::in | ios::binary);
ofstream output("text.txt");
if (input.is_open()) {
while (getline(input, line)) {
string encoded = base64_encode(reinterpret_cast<const unsigned char*>(line.c_str()), line.length());
output << encoded;
}
input.close();
}
}
相关文章:
- 防止主数据类型C++的隐式转换
- 模板参数替换失败,并且未完成隐式转换
- 努力将整数转换为链表。不知道我在这里做错了什么
- HEX值到wchar_t字符(UTF-8)的转换
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 将 BGR 图像转换为 jpeg 格式的 base64 字符串
- 如何在 c++ 中将 IStream 转换为 Base64 字符串
- 将图像.jpg转换为 Base64
- 如何将十六进制值字符串转换为 base64 字符串
- C++ 将 PNG 转换为 base64
- 将未签名的char数组转换为base64字符串
- 将 base64 解码字符串转换为无符号字符[32]
- 将Botan SHA256输出转换为base64,以匹配.NET SHA256 MALAGENG().computeHa
- 将解码的 Base64 字节字符串转换为布尔向量
- 将我的C++ Base64 代码转换为 Java 不起作用
- c++ OpenCV:将mat转换为base64,反之亦然
- Base64 转换器的输出不正确
- 使用Qt将二进制std::stringstream内容转换为base64(并返回)
- 如何在Qt中将Jpeg Base64字符串转换为jpeg图像
- 如何将二进制字符串转换为base64编码的数据