2012-03-23 114 views
2

我想使用下面的方法将base64编码的字符串转换为图像对象。从Base64字符串转换为PNG文件

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

如果base64字符串只是编码的图像数据,只需对其进行解码并将其写入具有图像扩展名的文件即可。 – 2012-03-23 14:17:14

回答

0

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

+0

感谢您的回复。你有任何这样的链接或教程? – Seany84 2012-03-23 15:05:35

+0

查看插件信息:http://wiki.phonegap.com/w/page/36752779/PhoneGap%20Plugins也看看phonegap的源代码,看看我们如何做文件编写器,你可以添加一些代码在写之前先做base64解码。 – 2012-03-23 16:39:35

+0

谢谢西蒙,我会在星期一告诉你我是如何继续这样做的。 – Seany84 2012-03-23 20:15:50

相关问题