Skip to content

Instantly share code, notes, and snippets.

@kidapu
Created February 23, 2016 11:50
Show Gist options
  • Save kidapu/0b9c2cbe98191a394c80 to your computer and use it in GitHub Desktop.
Save kidapu/0b9c2cbe98191a394c80 to your computer and use it in GitHub Desktop.
base64 decode + encode sample
// reference : https://gist.github.com/satoruhiga/1687325
#pragma once
static int WIDTH = 300;
static int HEIGHT = 300;
#include "ofMain.h"
#include "Poco/Base64Encoder.h"
#include "Poco/Base64Decoder.h"
class ofApp : public ofBaseApp
{
protected:
string _base64ImageData = "iVBORw0KGgoAAAANSUhEUgAAAHIAAAA8CAYAAAC6j+5hAAAAAXNSR0IArs4c6QAAAAZiS0dEAP8A/wD/oL2nkwAAAAlwSFlzAAALEwAACxMBAJqcGAAAAAd0SU1FB9sLGgwmJX2ZbqEAAAQrSURBVHja7Z2rdxtHFIe/34xspFO+f4BbXIXrD6hQkIMC2qAE1CygMbJBjWpSFhGjmoTJxRUpqniPeMXbZfHOLdjNqRw/9Kj2odm9TEBndefbO/cxd65kZrRVJD2r4DF/mdlC0hHwRUnP+FtVg5TUB74qPv5pZmldIHtyv5f/FHt/azb28j8LG5TwgDTDXvdKBHYEDBwMwPU/KeHR54uZq4tmENIAM2BmZnM6WQuimc17u4Z3gBsFbORRf+mtXP1dbADCw7AAnBr6LRCuO6hPQwTYCcie9A3olUdJYGdbdV/5CzHqyS2KLeqm43cfIoD7v8FCT+4D6BRISvzhCei0J/dB0rCDeBfi1hYpKXFw4ql8UROPLrz8LBDOzWzRQdzSIiUNPboS9VmGsIFHVy2zzkchbgzyUP7Eowug3wDF+h5dHMqftB3iRiC9/LuAHTdNw4Ade/l3EZct5hn2fFXk7taFKGzUWFWxkZeu4oQYXq9TNHH7DvE/mDqKyzLXh7gSZE/6bh8gLltmHD5zM4hPgswLynq1b0sQsOP9jmY3h/goSEl9j37c16Xw6FRS0haIj4J0cNqQFGPr1MTBSVsgPghS0rM6k/0dBj/Dis4ba4f4IEgH38cS93n0Qxsg3gNZWONRRIlYkp/MxA3xHkiH+za2lNrgRewQ74CUlJTUilB7oaDoVogW4h2QB7hjIpUD3ChmiHdABizaI6Hm6FYORCgOliUlnn1MoNcPeiQldR5EizDJYFpW16DLw3S+JnKpW8dbs5syWz9dEa1+GTtIww1i1q8HkMGRiF5qdR2H8icZbB09Z5a9We0jsSR2isJq3XVyYykvvXNNeFsrkn7Myjk66UB20oHspAPZSQcyfpBqw7W1RfQgDf6JnaKh+EGKMIu/IBC3jg4gg+i31th1/BTsRG+RseuY+0izNO6AR/M6p4dUnH6EX+LdVsOkNXlkBtOI9Zy2BqSZpYaie3MNTdowa+DOMIhAGHs0iknBQPi1Edv7ioPhHfpIMLNFTFZpaGZmf7QgIr9faw2EMZBGYo3ntETugcz9iUUQwdr7Ns3hefD049ZsvN95peYZXNMicU/kXm/3dItNM8JZ7AWAtUGa2SLDzvZwS/2pjdMknzxYNrMpewXTzto6QXJlh0C+MM2HmWGXbR4DutZ0yFuzm55EMc6zkZZoLZ/lunbPzq3ZTYY1LQBKM+xtN5B3w+YrM5tm2PN8/ngTUgx7mfvxTjbuojOzNLPsTYZd1mSdae4Ps5ctHLy7O5BLQK8L66ysNmtoUoy8vO7QbRHsPGWdwLmkSw/HoBfs/rJM6tDkI+HaLHQWWAbIz4COgfGBDoaBMCyukG17yysVmoGbfLSPnQ+sCuSyFAs/hfx/QHx+uTNZvjFc3FXsLwdNRbviIoN5VZWZKoI2UY0f/xcfeyq0L/JG7gAAAABJRU5ErkJggg==";
ofImage _image;
ofImage _image2;
public:
void setup()
{
_image = base64decode(_base64ImageData);
string base64string = base64endode(_image);
cout << base64string << endl; // <- 最初に入れたstrと違っている・・・!!
_image2 = base64decode( base64string );
}
void draw()
{
//2つのほぼ同じイメージがみえる
_image.draw(0, 0);
_image2.draw(WIDTH*0.5,0);
}
// base64 str -> ofImage
ofImage base64decode(string base64str)
{
stringstream ss;
ss << base64str;
Poco::Base64Decoder decoder(ss);
ofBuffer buffer;
decoder >> buffer;
ofImage image;
image.load(buffer);
return image;
}
// ofImage -> base64 str
string base64endode(ofImage image)
{
ofPixels pixel = image.getPixels();
ofBuffer imageBuffer;
ofSaveImage(pixel, imageBuffer);
stringstream ss;
ss.str("");
Poco::Base64Encoder encoder(ss);
encoder << imageBuffer;
encoder.close();
return ss.str();
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment