简体   繁体   English

解码utf8字符串(图像)以在node.js中缓冲/图像

[英]Decode a utf8 string (image) to buffer/ Image in node.js

I'm receiving an image in a stream as shown below that is ( imageBuffer.toString('utf8')) converted to utf8 string before receiving, however when I try to create a buffer with the following data the buffer appears to be different and not able to recreate image. 我正在接收如下所示的流中的图像,即在接收之前将( imageBuffer.toString('utf8'))转换为utf8字符串,但是,当我尝试使用以下数据创建缓冲区时,该缓冲区似乎有所不同,并且无法重新创建图像。

msg = ExifII* DuckyF ,Photoshop 3.08BIM%? ?Adobed ↵→→→→↵↵ ↵ ?↵↵???? g →?!1AQa"?q 2 B# R3ђ $T bCSD %EU r cs !1Q?A "a2q B# ? 65 ] jF(mR $Pm * a F H7҄ Ҁ 7D ( msg = ExifII* DuckyF ,Photoshop 3.08BIM%? ?Adobed ↵→→→→↵↵↵↵?↵↵???? g → ?!1AQa“?q 2 B# R3ђ $T.bCSD %EU r cs !1Q?A ”a2q B# ?65 ] jF(mR。$ Pm。*。a.F.H7҄ 7D。(。

console.log( typeOf msg ) => string console.log(typeOf msg)=>字符串

So far I have tried this: 到目前为止,我已经尝试过了:

let buffer = Buffer.from(msg, 'utf8');
let writable = require('fs').createWriteStream( '/home/test.jpg);
writable.write(buffer);
writable.end();

Problem is: Not able to reproduce the same image. 问题是:无法再现相同的图像。 output buffer size is different from input buffer of the image. 输出缓冲区的大小不同于图像的输入缓冲区。

Use 采用

let buffer = Buffer.from(new Uint8Array(msg));
...

Do not send binary as utf-8 string. 不要将二进制作为utf-8字符串发送。 Try base64: 尝试base64:

imageBuffer.toString('base64')

and: 和:

let buffer = Buffer.from(msg, 'base64');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM