从Base64字符串转换为PNG文件

Convert from Base64 String to PNG File

本文关键字:PNG 文件 转换 Base64 字符串      更新时间:2023-09-26

我正在尝试使用下面的方法将base64编码的字符串转换为图像对象。

function gotFileWriter(writer) {
    console.log('Starting gotFileWriter');
    writer.onwrite = function (evt) {
        console.log("write success");
    };
    $.mobile.showPageLoadingMsg();
    //        console.log('height: ' + cb_canvas.height);
    //        console.log('width: ' + cb_canvas.width);
    Signaturebase64 = cb_canvas.toDataURL();
    //I need to save the base64 string to a PNG image on the Phone here.  
    writer.write(Signaturebase64 );
    $.mobile.hidePageLoadingMsg();
    $.mobile.changePage("#MyJob");
    console.log('Finished gotFileWriter');
}

线路:

Signaturebase64 = cb_canvas.toDataURL();

工作如预期,并返回我的base64字符串。

我现在想做的是将其转换为手机持久存储中的图像文件。

下面一行是将base64字符串写入存储器,但我希望它将其保存为PNG文件:

writer.write(filedata);

您不能使用PhoneGap FileWriter来写入二进制数据。您需要编写一个插件,将base64编码的数据发送到本机端,将其编码为二进制,然后使用本机代码进行编写。

您需要将base64解码回二进制。以下是一个示例:http://blog.danguer.com/2011/10/24/base64-binary-decoding-in-javascript/