将 BGR 图像转换为 base64 字符串作为 jpeg
Convert BGR image into base64 string as jpeg
我在 OpenCV 约定中表示彩色图像,其中每个像素在 BGR 顺序中逐行表示为 unsigned char
:
const int BGR = 3;
const int rows= 256;
const int cols = 512;
unsigned char rawIm[BGR * rows *cols] = {'g', 't', 'y', // lots of chars.....}
我想将此流转换为表示相应 jpeg 图像的 base64 字符串,而无需实际将图像写入磁盘,只需 "plain" 字节转换。我怎么能在 C++ 中做到这一点?
图片转jpeg部分可以使用toojpeg,比libjpeg好用多了。
https://create.stephan-brumme.com/toojpeg/
但是你必须先将 BGR 反转为 RGB,因为 toojpeg 还不支持 BGR。
这是一个例子:
#include <vector>
#include <string>
#include <iostream>
#include "toojpeg.h"
std::vector<unsigned char> jpeg_data;
void myOutput(unsigned char byte) {
jpeg_data.push_back(byte);
}
int main() {
const auto width = 800;
const auto height = 600;
const auto bytesPerPixel = 3;
unsigned char bgr_data[width * height * bytesPerPixel];
// put some sample data in bgr_data, just for the example
for (unsigned i = 0; i < sizeof(bgr_data); i += 3) {
bgr_data[i] = i / width;
bgr_data[i + 1] = i / width * 2;
bgr_data[i + 2] = i / width * 3;
}
// convert BGR to RGB
unsigned char rgb_data[sizeof(bgr_data)];
for (unsigned i = 0; i < sizeof(bgr_data); i += 3) {
rgb_data[i] = bgr_data[i + 2];
rgb_data[i + 1] = bgr_data[i + 1];
rgb_data[i + 2] = bgr_data[i];
}
// convert the RGB data to jpeg
bool isRGB = true;
const auto quality = 90;
const bool downsample = false;
const char* comment = "example image";
bool result_ok = TooJpeg::writeJpeg(myOutput, rgb_data, width, height, isRGB, quality, downsample, comment);
if (result_ok) {
// jpeg_data now contains jpeg-encoded image, which can be encoded as base 64
}
return 0;
}
我在 OpenCV 约定中表示彩色图像,其中每个像素在 BGR 顺序中逐行表示为 unsigned char
:
const int BGR = 3;
const int rows= 256;
const int cols = 512;
unsigned char rawIm[BGR * rows *cols] = {'g', 't', 'y', // lots of chars.....}
我想将此流转换为表示相应 jpeg 图像的 base64 字符串,而无需实际将图像写入磁盘,只需 "plain" 字节转换。我怎么能在 C++ 中做到这一点?
图片转jpeg部分可以使用toojpeg,比libjpeg好用多了。 https://create.stephan-brumme.com/toojpeg/
但是你必须先将 BGR 反转为 RGB,因为 toojpeg 还不支持 BGR。
这是一个例子:
#include <vector>
#include <string>
#include <iostream>
#include "toojpeg.h"
std::vector<unsigned char> jpeg_data;
void myOutput(unsigned char byte) {
jpeg_data.push_back(byte);
}
int main() {
const auto width = 800;
const auto height = 600;
const auto bytesPerPixel = 3;
unsigned char bgr_data[width * height * bytesPerPixel];
// put some sample data in bgr_data, just for the example
for (unsigned i = 0; i < sizeof(bgr_data); i += 3) {
bgr_data[i] = i / width;
bgr_data[i + 1] = i / width * 2;
bgr_data[i + 2] = i / width * 3;
}
// convert BGR to RGB
unsigned char rgb_data[sizeof(bgr_data)];
for (unsigned i = 0; i < sizeof(bgr_data); i += 3) {
rgb_data[i] = bgr_data[i + 2];
rgb_data[i + 1] = bgr_data[i + 1];
rgb_data[i + 2] = bgr_data[i];
}
// convert the RGB data to jpeg
bool isRGB = true;
const auto quality = 90;
const bool downsample = false;
const char* comment = "example image";
bool result_ok = TooJpeg::writeJpeg(myOutput, rgb_data, width, height, isRGB, quality, downsample, comment);
if (result_ok) {
// jpeg_data now contains jpeg-encoded image, which can be encoded as base 64
}
return 0;
}