这里,img变量是从使用开放源代码的Png生成代码中提取的。
http://www.xarg.org/2010/03/generate-client-side-png-files-using-javascript/是canvas.toDataURL()的替代;webOS不支持toDataURL,所以我不得不使用这个库。
在这里,我使用了这个库并在我的画布图像数据像素数组上进行了操作。
EditorAssistant.prototype.getDataURL = function(width,height,data){
var p = new PNGlib(height, width, 256); // construcor takes height, weight and color-depth
var background = p.color(0, 0, 0, 0); // set the background transparent
for (var i = 0, n = data.length; i < n; i += 4) {
var x = i * 10;
var y = Math.sin(i) * Math.sin(i) * 50 + 50;
// use a color triad of Microsofts million dollar color
p.buffer[p.index(Math.floor(x), Math.floor(y))] = p.color(data[i], data[i+1], data[i+2]);
}
return 'data:image/png;base64,'+p.getBase64() ;
}老实说,我是node.js的新手。我只是想试试APProach.我想将被操纵的画布对象保存到我的应用程序中的图像目录中。路径确实存在于此,并且此代码不会生成任何错误。它给了我成功的回调和返回的字节数写,但我找不到一个图像名为icon.png在图像文件夹.上面生成的imgdata作为数据传递给这段代码。
var fs = IMPORTS.require('fs');
var path = IMPORTS.require('path');
var buff = new Buffer(data,'binary').toString('base64');
path.exists('images/', function(exists ){
if (exists) {
fs.open('images/icon.png', 'w', 666, function( e, id ) {
fs.write( id, buff, null, 'binary', function(err,written){
if(err)
callback({
error: false,
reply: err
});
if(written){
callback({
error: false,
reply: buff.toString()
});
}
fs.close(id, function(){
callback({
error: false,
reply: 'closed'
});
});
});
});
}
else {
callback({
error: true,
reply: 'File did not exist.'
});
}
}
})提前感谢
发布于 2011-05-04 20:00:14
data是一个以data:image/png;base64,开头的字符串,其余的是base64中的数据。
data
data:image/png;base64,从base64中移除到二进制文件
代码
var buff = new Buffer(data.substr('data:image/png;base64,'.length), 'base64');
...
fs.write(id, buff, 0, buff.length, 0, function(...https://stackoverflow.com/questions/5874521
复制相似问题