如何将图像转换为bufferedArray

问题描述 投票:0回答:1

我试图将图像转换为像这种格式的缓冲数组

<Buffer ff d8 ff e2 02 1c 49 43 43 5f 50 52 4f 46 49 4c 45 00 01 01 00 00 02 0c 6c 63 6d 73 02 10 00 00 6d 6e 74 72 52 47 42 20 58 59 5a 20 07 dc 00 01 00 19 ... >

我想将它发送到我的节点的fs.writefile,因为它将我的图像写入base64中的文件时会出现错误图像。谢谢

html node.js mongodb express
1个回答
0
投票

您必须阅读有关Streams以及如何使用它们处理数据的信息。 如果您的目标是拥有缓冲区数组,您可以编写如下内容:

const fs = require('fs');

const img = '/path/to/image.jpg';

const fileStream = fs.createReadStream(img);

const buffers = [];

fileStream.on('data', chunk => {
  buffers.push(chunk);
})

fileStream.on('end', () => {
  console.log(buffers)
})

但是,如果您想将文件写入另一个位置,那么pipe()流更好。 喜欢这段代码:

fs.createReadStream(file).pipe(fs.createWriteStream(destination))

此外,这是令人惊讶的tutorial使用流

© www.soinside.com 2019 - 2024. All rights reserved.