简体   繁体   中英

Base64 Image encoding is incorrect

I am attempting to convert a.jpg file into base64 then into an svg file but my base64 is wrong I don't know if I'm doing something obviously wrong this is my first time working with base64 in c++

When I run the code I get an svg file with the proper format and the correct padding but the file is blank when I open it.

Here's my code:

#include <iostream>
#include <fstream>
#include <string>
#include "base64.h"
#include "base64.cpp"

int main()
{
    bool url;
    std::string image;
    std::string encoded;
    std::string alldata = "";
    std::cout << "Name of file without extension: \n";
    std::cin >> image;
    // Takes the name of the image
    std::string outfile = image + ".svg";
    // creates outfiles
    image = image + ".jpg";
    // adds jpg extension to name of file
    std::string line;
    std::ifstream input(image, std::ios::in | std::ios::binary);
    // opens the image

    if (input.is_open()) {
    // checks if the file is open
        while (getline(input, line)) {

     //  std::string encoded = base64_encode_mime(const& s);
            alldata = alldata + (line.c_str());
        // makes multiple lines into one

        }

        input.close();
    }
    else {
        std::cout << "ERROR: File not found";
    }

    encoded = base64_encode(reinterpret_cast<const unsigned char*>(alldata.c_str()), alldata.length(), url = false);
    // encodes the one line of image data
    std::string svg;
    std::ofstream output(outfile,std::ios::binary|std::ios::out);
            svg = "<?xml version='1.0' encoding='UTF-8' standalone='no'?> <!DOCTYPE svg PUBLIC '-//W3C//DTD SVG 1.1//EN' 'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd'> <svg version='1.1' id='Layer_1' xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink' x='0px' y='0px' width='1920px' height='1200px' viewBox='0 0 1920 1200' enable-background='new 0 0 1920 1200' xml:space='preserve'>  <image id='image0' width='1920' height='1200' x='0' y='0' href='data:image/png;base64,";
            svg = svg + encoded;
            svg = svg + "'/> </svg>";
            output << svg;
}

I'm using the base64 library by René Nyffenegger found here: https://github.com/ReneNyffenegger/cpp-base64/blob/master/base64.cpp I have tested this library, it appears to encode text correctly.

Your error is in the way you read the data from the file. Don't use getline on a binary file, apart from anything else it does not return the newlines to you. The second problem is that by using c_str() when you append each line you are assuming that there will be no null characters in your data, which is very unlikely to be true.

The simplest way to do this would be to read the file one character at a time

if (input.is_open()) {
    char ch;
    while (input.get(ch)) {
         alldata += ch;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM