[将png编码为base64,然后将其转换回pixbuf时,出现格式未知错误

问题描述 投票:0回答:1

我一直在尝试使用vala中base64格式的图片。但是我有一些麻烦。我写了一个小的Vala程序来显示问题:

我可以轻松加载图像文件。我可以将其转换为字节,从字节转换为uchar数组,从其转换为base64编码的字符串,然后一直转换回字节,然后将其解析为MemoryInputStream。但是,当我将其解析为Pixbuf时,它告诉我该格式无法识别。然后,我可以运行bytes.compare(reversed_bytes)以查看其中的内容完全相同。 MemoryInputStream是否以某种方式读取错误?

无论如何,这是一个示例程序:

using Gdk;

static void main(string[] args){

        string path = "%s".printf(args[1]);

        Pixbuf original_pixbuf = new Gdk.Pixbuf.from_file(path);

        test_pixbuf_from_b64(original_pixbuf);

}

public void test_pixbuf_from_b64(Pixbuf original_pixbuf){
    Bytes bytes = original_pixbuf.pixel_bytes;

    var uchar_array = bytes.get_data();

    string base64_string = Base64.encode(uchar_array);

    var reversed_uchar_array = Base64.decode(base64_string);

    Bytes reversed_bytes = new Bytes(reversed_uchar_array);


    //Lets test the contents of the bytes to see they are equal

    if(bytes.compare(reversed_bytes) == 0){
        print("The reversed bytes value is the same as the original!\n");
    }else{
        print("The value of reversed bytes is different from the original!\n");
    }

    MemoryInputStream mis = new MemoryInputStream.from_bytes(reversed_bytes);

    try{
        Pixbuf reversed_pixbuf = new Pixbuf.from_stream(mis); //This fails, with a format not recognized error. Why though?
    }catch(Error er){
        stdout.printf("The following error occurred when recreating the pixbuf: %s", er.message);
    }
}

编译为:

valac t.vala --pkg=gdk-pixbuf-2.0

运行:

./t "path/to/image/file/here"

直到我弄清楚这一点,我才能真正开始我的项目。

base64 vala gdkpixbuf pixbuf
1个回答
0
投票

[JPG,PNG或GIF图片的问题]

Gdk.Pixbuf icon = new Gdk.Pixbuf.from_file ("pablo.jpg");
uint8[] bx;
icon.save_to_buffer(out bx,"jpeg",null); // ---> jpeg, png or gif
string b64 = Base64.encode(bx);
© www.soinside.com 2019 - 2024. All rights reserved.