栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

我认为您有点误解了编码参数的用法。如果要指定编码“二进制”,则需要一致地进行编码。但实际上您根本不需要它。您似乎对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 enpres the content of that Buffer to base64, which is fine.    var base64Image = new Buffer(original_data, 'binary').toString('base64');    // Here you depre 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 pre points are bytes of the buffer.    var depredImage = 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    // depre your binary string into a utf8 enpred 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_depred.jpg', depredImage, 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 depredImage = new Buffer(base64Image, 'base64').toString('binary');    fs.writeFile('image_depred.jpg', depredImage, '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 depredImage = new Buffer(base64Image, 'base64');    fs.writeFile('image_depred.jpg', depredImage, function(err) {});});


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/382509.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号