Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.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++ c++;将png转换为base64_C++_Opencv_Meteor_Base64 - Fatal编程技术网

C++ c++;将png转换为base64

C++ c++;将png转换为base64,c++,opencv,meteor,base64,C++,Opencv,Meteor,Base64,我发现将png文件转换为base64,但它要求从文件流中读取,如: ostringstream sout; istringstream sin; // this is the object we will use to do the base64 encoding base64 base64_coder; // now base64 encode the compressed data base64_coder.encode(sin,sout); 我在opencv中转换了png,如下所

我发现将png文件转换为base64,但它要求从文件流中读取,如:

ostringstream sout;
istringstream sin;

// this is the object we will use to do the base64 encoding
base64 base64_coder;



// now base64 encode the compressed data
base64_coder.encode(sin,sout);
我在opencv中转换了png,如下所示:

                imencode(".png", im, buf);
当我想改变信仰的时候

    base64_coder.encode(buf,sout);
它问小溪

<我的C++知识有限,所以任何帮助都值得欣赏。< /P> 目的是:

我需要将png图像写入mongodb,以便meteorjs使用。所以他们要求base64编码。图像

thx

编辑:im是Cv::Mat。obj。我正在把它转换成png。buf包括png。我发现:

auto base64_png = reinterpret_cast<const unsigned char*>(buf.data());
                std::string encoded_png = "data:image/jpeg;base64,"+base64_encode(base64_png,buf.size());
auto base64\u png=reinterpret\u cast(buf.data());
std::string encoded_png=“data:image/jpeg;base64,”+base64_encode(base64_png,buf.size());
中的标题:

它解决了我的问题

什么对我有用:

  • 将.cpp和.h文件从添加到我的项目中

    string encoded_png;
    Mat img; // Load an image here
    
    vector<uchar> buf;
    cv::imencode(".png", img, buf);
    auto base64_png = reinterpret_cast<const unsigned char*>(buf.data());
    encoded_png = "data:image/jpeg;base64," + base64_encode(base64_png, buf.size());
    
    字符串编码\u png;
    Mat img;//在这里加载图像
    向量buf;
    cv::imencode(“.png”,img,buf);
    auto base64_png=重新解释强制转换(buf.data());
    encoded_png=“data:image/jpeg;base64,”+base64_encode(base64_png,buf.size());
    

encoded\u png
现在包含base64字符串,可以对其进行解码以获得原始图像。

什么是im,什么是buf?您需要展示一个包含错误消息的完整示例。您可以使用gridfs或collectionFS在mongodb上存储图像,这样您就不需要将它们转换为base64im is cv::mat并将其转换为png in buf。请检查Nyffenegger的b64解码/编码实现,它使用char*(我假设您的缓冲区是)而不是streams,并返回一个std::字符串,我曾经在mongodb中存储过这样的图片:)