当前位置: 首页 > 面试题库 >

NodeJS base64图像编码/解码不太起作用

湛宏旷
2023-03-14
问题内容

我一直在尝试处理将发布到nodeJS(和express框架)的图像保存到数据库中,并且遇到了一些麻烦。忽略所有的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) {});
});

问题答案:

我认为您有点误解了编码参数的用法。如果要指定编码“二进制”,则需要一致地进行编码。但实际上您根本不需要它。您似乎对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, 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) {});
});

下一个示例将起作用,但是效率很低,因为不需要一直更改编码,但是我只是想表明它是清楚的。如果您真的DID想要使用特定的编码,则需要确保您保持一致。这些功能中的每一个都有一个编码参数。

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

这是正确的方法。将所有内容保留为Buffer,除非将其设为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) {});
});


 类似资料:
  • HTML and CSS allows you to embed external resources right into base using data:URL scheme. Usually, image conversion to base64 is done with external on-line services or third-party assets builder. HTM

  • 我有这样一个csv文件 我在读书

  • 问题内容: 我正在为Firefox / IE构建一个开放式搜索附加组件,并且该图像需要经过Base64编码,因此如何对我拥有的收藏夹图标进行64位编码? 我只熟悉PHP 问题答案: 据我记得,图像数据有一个xml元素。您可以使用此网站对文件进行编码(使用上载字段)。然后,只需将数据复制并粘贴到XML元素即可。 您也可以这样使用PHP来做到这一点: 使用Mozilla指南获取有关创建OpenSear

  • 我有一个编码为base64的SVG文件,我想用ImageView显示图像。这是我尝试过的: 但是decodedByte总是返回null。 附注: 此代码适用于jpeg图像。 如果Bas64字符串包含Bas64前缀("data: Image/svg xml; Bas64,"或"data: Image/jpeg; Bas64,),则decdedByte也总是返回null Bas64字符串是正确的(它在

  • 我正在使用spring boot,spring安全,OAuth2和JWT来验证我的应用程序,但我一直得到这个令人讨厌的错误,我不知道什么是错误的。我的类: : : 除: 我的实体模型类是: 实体: 实体: 我在数据库中的密码是正确加密的spring安全BCrypt,它的数据类型是varchar(255),大于60。

  • 我无法使用正确的详细信息登录,因为程序不断声明编码的密码看起来不像bcrypt。有人知道怎么解决这个吗?我正在使用JDBC身份验证。 我也有正确的数据库表,有足够的空间用于编码密码。我不确定哪里出了问题。 JSP表单: 安全配置: 登录控制器 我的数据库:这里