如何从Node.js缓冲区(UInt8Array)显示JPG图像 [英] How to display a JPG image from a Node.js buffer (UInt8Array)
问题描述
我有一个自定义的Node.JS插件,可以将jpg捕获的图像传输到我的应用程序中,效果非常好-如果将缓冲区内容写入磁盘,则可以像预期的那样是正确的jpg图像。
I have a custom Node.JS addon that transfers a jpg capture to my application which is working great - if I write the buffer contents to disk, it's a proper jpg image, as expected.
var wstream = fs.createWriteStream(filename);
wstream.write(getImageResult.imagebuffer);
wstream.end();
我正在努力将该图像显示为img.src,而不是将其保存到磁盘中,像
I'm struggling to display that image as an img.src rather than saving it to disk, something like
var image = document.createElement('img');
var b64encoded = btoa(String.fromCharCode.apply(null, getImageResult.imagebuffer));
image.src = 'data:image/jpeg;base64,' + b64encoded;
转换后b64中的数据已正确编码,因为我在 http://codebeautify.org/base64-to-image-converter 并显示正确的图像。我一定想念一些愚蠢的东西。任何帮助都将是惊人的!
The data in b64encoded after the conversion IS correct, as I tried it on http://codebeautify.org/base64-to-image-converter and the correct image does show up. I must be missing something stupid. Any help would be amazing!
谢谢您的帮助!
推荐答案
是您想要的吗?
// Buffer for the jpg data
var buf = getImageResult.imagebuffer;
// Create an HTML img tag
var imageElem = document.createElement('img');
// Just use the toString() method from your buffer instance
// to get date as base64 type
imageElem.src = 'data:image/jpeg;base64,' + buf.toString('base64');
这篇关于如何从Node.js缓冲区(UInt8Array)显示JPG图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!