Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/159.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 将BGR图像转换为jpeg格式的base64字符串_C++_Base64_Jpeg_Tobase64string - Fatal编程技术网

C++ 将BGR图像转换为jpeg格式的base64字符串

C++ 将BGR图像转换为jpeg格式的base64字符串,c++,base64,jpeg,tobase64string,C++,Base64,Jpeg,Tobase64string,我用OpenCV约定表示彩色图像,其中每个像素以BGR顺序一行接一行表示为无符号字符: const int BGR = 3; const int rows= 256; const int cols = 512; unsigned char rawIm[BGR * rows *cols] = {'g', 't', 'y', // lots of chars.....} 我想将这个流转换成base64字符串,它表示相应的jpeg图像,而不实际将图像写入磁盘,只是“普通”字节转换。如何在C++中实现

我用OpenCV约定表示彩色图像,其中每个像素以BGR顺序一行接一行表示为
无符号字符

const int BGR = 3;
const int rows= 256;
const int cols = 512;
unsigned char rawIm[BGR * rows *cols] = {'g', 't', 'y', // lots of chars.....}

我想将这个流转换成base64字符串,它表示相应的jpeg图像,而不实际将图像写入磁盘,只是“普通”字节转换。如何在C++中实现? 对于“图像到jpeg”部分,可以使用toojpeg,它比libjpeg更易于使用。

但是您必须首先将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;
}

#包括
#包括
#包括
#包括“toojpeg.h”
std::矢量jpeg_数据;
void myOutput(无符号字符字节){
jpeg_数据。推回(字节);
}
int main(){
常数自动宽度=800;
常数自动高度=600;
常量自动字节/像素=3;
无符号字符bgr_数据[宽度*高度*字节/像素];
//在bgr_数据中放入一些示例数据,仅用于示例
对于(无符号i=0;i
有一种方法可以在不写入磁盘的情况下使用,编码到base64将是一个单独的步骤。@Benny K基本上你想将一个无符号字符数组编码到base64吗?我的意思是,事实上,它是一个BGR数组,恰好是从JPEG文件解码的,这只是额外的信息?@傻瓜,不,原始字节数组应该首先转换为JPEG编码的字节数组,这是我最有问题的部分,之后,将这个字节数组转换为base64字符串是我要寻找的。好的,这实际上是两个问题:1。如何将BGR原始数据编码为jpeg格式,以及2。如何将步骤1中的数据转换为base 64。是吗?@傻瓜是的,主要问题是1。第二步更简单,看看以前的文章。