BASE64的NodeJS图像编码/解码不太工作 [英] NodeJS base64 image encoding/decoding not quite working

查看:214
本文介绍了BASE64的NodeJS图像编码/解码不太工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图处理储蓄张贴到的NodeJS(和前preSS框架)到一个数据库的图像,并且已经遇到了一些麻烦。忽略所有的网络处理,我认为我已经缩小问题的方式base64编码在节点发生。我认为,过于简单​​的例子下面应该工作,但输出图像始终损坏。

的例子(1)图像中的负载(2)节省的,如果( image_orig )以确认节点可以正确读取该文件的副本。这始终工作。 (3)我走的形象和连接的base64 code的内容,(4)然后去code吧。最终输出的图像( image_de codeD )总是损坏。

帮助!
(在OSX狮子的Node.js 0.6.0)

 的console.log(起);
process.chdir(__目录名);变种FS =需要(FS);VAR image_origial =image.jpg的;
fs.readFile(image_origial,函数(ERR,original_data){
    fs.writeFile('image_orig.jpg',original_data,功能(错误){});
    VAR base64Image =新的缓冲区(original_data,'二进制')的toString('的base64')。
    VAR德codeDIMAGE =新的缓冲区(base64Image的base64')的toString('二进制')。
    fs.writeFile('image_de coded.jpg',德codeDIMAGE,功能(错误){});
});


解决方案

我觉得是被误解的说法编码位的使用。如果你要指定编码二进制,那么你需要始终如一地做到这一点。但实际上你并不需要它。你似乎混淆缓冲VS二进制字符串使用。

//这告诉节点将文件加载到缓冲区'original_data',因为你
//没有指定为返回值的编码。如果你提供了一个
//编码,然后original_data将与该编码的字符串。
fs.readFile(image_origial,函数(ERR,original_data){    //这告诉节点采取该缓冲区,并将其写入新的文件名。
    //再没有编码来提供,它会假设一个缓冲或UTF8字符串。
    fs.writeFile('image_orig.jpg',original_data,功能(错误){});    //这告诉节点从旧的缓冲器,这意味着创建一个新的缓冲区
    //它会遍历original_data一次复制字节之一。但
    //它们将是相同的缓冲器。它会忽略'二进制'参数
    //因为你正在传递的对象是不是字符串。
    //然后连接codeS缓冲区的内容为Base64,这是罚款。
    VAR base64Image =新的缓冲区(original_data,'二进制')的toString('的base64')。    //在这里你去code为Base64到缓冲区,这是很好的,但你
    //缓冲区转换为字符串与编码二进制。这意味着
    //它是一个字符串对象,其code点是缓冲区的字节数。
    VAR德codeDIMAGE =新的缓冲区(base64Image的base64')的toString('二进制')。    //这里您尝试String对象写入文件。由于你的说法
    //给是一个字符串,你没有给一个说法编码为
    //写命令,那么它会假设'UTF8'是编码。它会尝试
    //德code的二进制字符串转换成UTF8连接$​​ C $ CD缓冲区,并写入缓冲区。
    //这将导致失败,因为编码转换是错误的。
    //真的通过,'二进制'是错误的使用。缓冲器已经二进制。
    fs.writeFile('image_de coded.jpg',德codeDIMAGE,功能(错误){});
});

这一个例子将工作,但因为改变编码不需要所有的时间非常低效的,但我只是想表明它是清楚的。如果你真的想有一个特定的编码,你需要确保你是一致的。这些功能每个人心中都有一个编码参数。

fs.readFile(image_origial,'二元',函数(ERR,original_data){
    fs.writeFile('image_orig.jpg',original_data,'二元',函数(ERR){});
    VAR base64Image =新的缓冲区(original_data,'二进制')的toString('的base64')。
    VAR德codeDIMAGE =新的缓冲区(base64Image的base64')的toString('二进制')。
    fs.writeFile('image_de coded.jpg',德codeDIMAGE,'二元',函数(ERR){});
});

这是做正确的方式。保留一切作为缓冲,当你把它的base64除。

fs.readFile(image_origial,函数(ERR,original_data){
    fs.writeFile('image_orig.jpg',original_data,功能(错误){});
    变种base64Image = original_data.toString('的base64');
    VAR德codeDIMAGE =新的缓冲区(base64Image的base64');
    fs.writeFile('image_de coded.jpg',德codeDIMAGE,功能(错误){});
});

I've been trying to handle saving images POSTed to nodeJS (and the express framework) to a database, and have been having some trouble. Ignoring all the web processing, I think that I've narrowed down the problem to the way base64 encoding is happening in node. I believe that the oversimplified example below should work, but the output image is always corrupted.

The example (1) loads in an image (2) saves a copy of if (image_orig) to confirm that node can read the file properly. This always works. (3) I take the image and base64 encode its contents, (4) then decode it. The final output image (image_decoded) is always corrupted.

Help! (node.js 0.6.0 on OSX Lion)

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) {});
});

解决方案

I think are are misunderstanding the usage of the encoding argument a bit. If you are going to specify encoding 'binary', then you need to do it consistently. But really you don't need it at all. You seem to be confusing the usage of Buffer vs binary strings.

// 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, function(err, 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', original_data, 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', decodedImage, function(err) {});
});

This next example will work but is very inefficient because changing encodings all the time isn't needed, but I just want to show it to be clear. If you really DID want to have a specific encoding, you need to make sure you are consistent. Every one of those functions has an encoding argument.

fs.readFile(image_origial, 'binary', function(err, original_data){
    fs.writeFile('image_orig.jpg', original_data, 'binary', 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, 'binary', function(err) {});
});

This is the right way to do it. Keep everything as a Buffer, except when you make it base64.

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

这篇关于BASE64的NodeJS图像编码/解码不太工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆