5

I found conver png file to base64 but it asks to read from file stream like:

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);

I have png in opencv converted like:

                imencode(".png", im, buf);

when I want to convert

    base64_coder.encode(buf,sout);

it asks stream..

My c++ knowledge is limited so any help appreciated.

The purpose for this:

I need to write png images to the mongodb that can be used by meteorjs . So they are asking base64 encoded . images.

thx

EDIT : im is Cv::Mat . obj. I am converting it to png . buf includes the png.

Rahibe Meryem
  • 269
  • 3
  • 14
  • 1
    Whats im, whats buf? you need to show a full example with the error messages. – aram Jan 17 '18 at 17:24
  • You can store images on mongodb using gridfs or collectionFS, then you would not need to convert them to base64 – Jankapunkt Jan 17 '18 at 18:43
  • im is cv::mat and converting to png to in buf. – Rahibe Meryem Jan 17 '18 at 18:47
  • Check Nyffenegger's b64 decode/encode implementation it takes a char * (which I'm assuming your buffer is), instead of streams, and returns a std::string, I did something like that to store pictures in mongodb once :) – aram Jan 17 '18 at 19:10

3 Answers3

2

What worked for me:

  • Added .cpp and .h files from https://github.com/ReneNyffenegger/cpp-base64 to my project;

    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());
    

And encoded_png now contains the base64 string, which can be decoded to get the original image.

arvids
  • 184
  • 2
  • 11
0

I found :

auto base64_png = reinterpret_cast<const unsigned char*>(buf.data());
                std::string encoded_png = "data:image/jpeg;base64,"+base64_encode(base64_png,buf.size());

the headers in the: github

it is solved my problem

Rahibe Meryem
  • 269
  • 3
  • 14
0

While both methods are sound, it's worth noting the increase in size (~33%) when encoding images in base64, as detailed in this Stack Overflow answer. If storage or transmission efficiency is a concern, consider compressing the PNG images before base64 encoding.

An option to address this is the png-base64-webp header-only library. It encodes a PNG image to base64 through WebP compression, optimizing space usage. However, note that decoding will yield a WebP image rather than a PNG. This might not be an issue if the primary goal is image retrieval and display, given WebP's wide support across browsers and platforms.

To utilize the library, include the header and invoke the encodePNGToBase64 function:

#include "png-base64-webp.h"

int main() {
    const char* imagePath = "/path/to/your/image.png";
    char* encodedData = encodePNGToBase64(imagePath);
    if (encodedData) {
        // Utilize the encoded data
        free(encodedData);  // Ensure to release the memory post usage
    }
    return 0;
}
nsssayom
  • 364
  • 1
  • 5
  • 21