Node.js   发布时间:2022-04-24  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了node.js – NodeJS base64图像编码/解码不太有效大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我一直在尝试将发布到nodeJS(和快速框架)的图像保存到数据库中,并且遇到了一些麻烦.忽略所有的Web处理,我认为我已经将问题缩小到了base64编码在节点中发生的方式.我相信下面的过度简化示例应该可以工作,但输出图像总是被破坏.

示例(1)加载图像(2)保存if(image_orig)的副本以确认该节点可以正确读取文件.这总是有效的. (3)我拍摄图像并对其内容进行base64编码,(4)然后对其进行解码.最终输出图像(image_decoded)始终损坏.

救命!
(OSX Lion上的node.js 0.6.0)

console.log("starTing");
process.chdir(__dirName);

var fs = require("fs");

var image_origial = "image.jpg";
fs.readFile(image_origial,function(err,original_data){
    fs.writeFile('image_orig.jpg',original_data,function(err) {});
    var base64Image = new Buffer(original_data,'binary').toString('base64');
    var decodedImage = new Buffer(base64Image,'base64').toString('binary');
    fs.writeFile('image_decoded.jpg',decodedImage,function(err) {});
});

@L_489_6@

我认为你误解了编码参数的用法.如果要指定编码’binary’,则需要一致地执行.但实际上你根本不需要它.您似乎混淆了Buffer与二进制字符串的使用.

// This tells node to load the filE into a Buffer 'original_data' because you
// have not specified an encoding for the returned values. If you provided an
// encoding,then original_data would be a String with that encoding.
fs.readFile(image_origial,original_data){

    // This tells node to take that buffer,and write it to the new filename.
    // Again no encoding is provided,so it will assume a Buffer or utf8 String.
    fs.writeFile('image_orig.jpg',function(err) {});

    // This tells node to create a new buffer from the old buffer,which means
    // it will iterate over original_data copying the bytes one at a time. But
    // they will be identical buffers. It will ignore the 'binary' argument
    // since the object you are passing isn't a String.
    // Then it encodes the content of that Buffer to base64,which is fine.
    var base64Image = new Buffer(original_data,'binary').toString('base64');

    // Here you decode the base64 to a buffer,which is fine,but then you
    // convert the buffer into a String with encoding 'binary'. This means that
    // it is a String object whose code points are bytes of the buffer.
    var decodedImage = new Buffer(base64Image,'base64').toString('binary');

    // Here you try to write that String object to a file. Since the argument you
    // have given is a String and you have not given an encoding argument for the
    // write command,then it will assume that 'utf8' is the encoding. It will try to
    // decode your binary String into a utf8 encoded buffer,and write that buffer.
    // This will cause it to fail because that encoding conversion is wrong.
    // Really through,'binary' is just wrong to use. Buffers are already binary.
    fs.writeFile('image_decoded.jpg',function(err) {});
});

一个示例将起作用,但效率非常低,因为不需要一直更改编码,但我只想表明它是清楚的.如果你真的想要拥有特定的编码,你需要确保你是一致的.这些函数中的每一个都有一个编码参数.

fs.readFile(image_origial,'binary',function(err) {});
});

这是正确的方法.将所有内容保留为缓冲区,除非您将其设置为base64.

fs.readFile(image_origial,function(err) {});
    var base64Image = original_data.toString('base64');
    var decodedImage = new Buffer(base64Image,'base64');
    fs.writeFile('image_decoded.jpg',function(err) {});
});

大佬总结

以上是大佬教程为你收集整理的node.js – NodeJS base64图像编码/解码不太有效全部内容,希望文章能够帮你解决node.js – NodeJS base64图像编码/解码不太有效所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。