【问题标题】:how to decode base64 to image in Nodejs?如何在Nodejs中将base64解码为图像?
【发布时间】:2020-01-13 02:37:13
【问题描述】:

我正在通过套接字发送编码为 base64 的图像,但解码不起​​作用。必须包含新图像的文件被写入 base64 而不是 jpg 文件。

编码套接字:

function encode_base64(filename) {
  fs.readFile(path.join(__dirname, filename), function (error, data) {
    if (error) {
      throw error;
    } else {
      console.log(data);
      var dataBase64 = data.toString('base64');
      console.log(dataBase64);
      

      client.write(dataBase64);
    }
  });
}

rl.on('line', (data) => {
    encode_base64('../image.jpg')

})

解码套接字:

function base64_decode(base64str, file) {
  
   var bitmap = new Buffer(base64str, 'base64');
   
   fs.writeFileSync(file, bitmap);
   console.log('****** File created from base64 encoded string ******');
  }


client.on('data', (data) => {


    base64_decode(data,'copy.jpg')

  
});

// the first few characters in the new file 
//k1NRWuGwBGJpmHDTI9VcgOcRgIT0ftMsldCjFJ43whvppjV48NGq3eeOIeeur

【问题讨论】:

    标签: javascript node.js stream buffer


    【解决方案1】:

    改变编码功能如下。另外,请记住 new Buffer() 已被弃用,因此请使用 Buffer.from() 方法。

    function encode_base64(filename) {
      fs.readFile(path.join(__dirname, filename), function (error, data) {
        if (error) {
          throw error;
        } else {
          //console.log(data);
          var dataBase64 = Buffer.from(data).toString('base64');
          console.log(dataBase64);
          client.write(dataBase64);
        }
      });
    }
    

    解码如下:

    function base64_decode(base64Image, file) {
      fs.writeFileSync(file,base64Image);
       console.log('******** File created from base64 encoded string ********');
    
    }
    
    client.on('data', (data) => {
        base64_decode(data,'copy.jpg')
    });
    

    【讨论】:

    • 接收套接字的终端给出了这个错误:TypeError: base64str.split is not a function at base64_decode (C:\Users\HP\Desktop\Node\sockets\client2.js:17:28 )
    • 是数据字符串吗?
    • 在“client.write(dataBase64)”上的数据是字符串
    • 终端写的是“从base64编码字符串创建的文件”,但jpg文件仍然写为base64(不制作图像)
    • 你能再试一次吗?我把解码方法代码改了一点
    【解决方案2】:

    您可以使用以下方法解码 base64 图像。

    已编辑

    去除标题

    let base64String = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgA'; // Not a real image
    // Remove header
    let base64Image = base64String.split(';base64,').pop();
    

    写入文件

    import fs from 'fs';
    fs.writeFile('image.png', base64Image, {encoding: 'base64'}, function(err) {
        console.log('File created');
    });
    

    注意:-不要忘记这里的 {encoding: 'base64'},你会很高兴的。

    【讨论】:

    • 它给了我一个错误:DeprecationWarning: Buffer() is deprecated due to security and usability issues so I changed it to Buffer.from(data, "base64") but the file is write in base64不像jpg
    • 尝试给你另一种解决方案。
    • 我已经更新了我的代码,你可以看看。 @AliHmede
    • 什么是 image.png?以及如何访问此结果?
    • Image.png 是您想要的文件名,结果在名为 image.png @Grogu 的文件中查看/访问
    【解决方案3】:

    似乎解码函数base64_decode 将数据作为缓冲区获取。 因此,new Buffer(base64str, 'base64') 中的编码参数将被忽略。 (比较 Buffer.from(buffer)Buffer.from(string[, encoding]) 的文档)。

    我建议先转换成字符串

    function base64_decode(base64str, file) {
    
       var bitmap = new Buffer(base64str.toString(), 'base64');
    
       fs.writeFileSync(file, bitmap);
       console.log('******** File created from base64 encoded string ********');
      }
    
    

    【讨论】:

    • 奇怪的字符出现在文件“not base64 even”中......
    • 对。实际上这是另一个问题,因为k1NRWuGwBGJpmHDTI9VcgOcRgIT0ftMsldCjFJ43whvppjV48NGq3eeOIeeur 不是有效的base64 字符串。尝试使用base64_decode 函数接收到的前10 个字符将console.log 写入客户端的前10 个字符。
    【解决方案4】:

    您可以使用Buffer.from 解码Base64,并使用fs.writeFileSync 将其写入文件

    const { writeFileSync } = require("fs")
    
    const base64 = "iVBORw0KGgoA..."
    const image = Buffer.from(base64, "base64")
    
    writeFileSync("image.png", image)
    

    如果文件中有Base64字符串,需要先解码成字符串,比如:

    const { writeFileSync } = require("fs")
    
    const base64 = fs.readFileSync(path, "ascii")
    const image = Buffer.from(base64, "base64")
    
    writeFileSync("image.png", image)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-05
      • 1970-01-01
      • 2014-06-12
      • 2012-10-05
      • 1970-01-01
      • 1970-01-01
      • 2021-08-31
      • 2011-12-27
      相关资源
      最近更新 更多