如何从 Node.js 缓冲区显示 JPG 图像(UInt8Array)

How to display a JPG image from a Node.js buffer (UInt8Array)(如何从 Node.js 缓冲区显示 JPG 图像(UInt8Array))
本文介绍了如何从 Node.js 缓冲区显示 JPG 图像(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;

转换后 b64encoded 中的数据是正确的,因为我在 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 缓冲区显示 JPG 图像(UInt8Array)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Update another component when Formik form changes(当Formik表单更改时更新另一个组件)
Formik validation isSubmitting / isValidating not getting set to true(Formik验证正在提交/isValiating未设置为True)
React Validation Max Range Using Formik(使用Formik的Reaction验证最大范围)
Validation using Yup to check string or number length(使用YUP检查字符串或数字长度的验证)
Updating initialValues prop on Formik Form does not update input value(更新Formik表单上的初始值属性不会更新输入值)
password validation with yup and formik(使用YUP和Formick进行密码验证)