如何从 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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

相关文档推荐

opening html from google drive(从谷歌驱动器打开 html)
Google apps script get range of bytes from binary file(谷歌应用程序脚本从二进制文件中获取字节范围)
Sending Multiple attachments with Google Script from Google Drive(使用 Google 脚本从 Google Drive 发送多个附件)
Distributing Google Apps Scripts for Sheets in your company network(在您的公司网络中分发适用于表格的 Google Apps 脚本)
Upload file to my google drive from anyone using javascript(使用 javascript 将文件从任何人上传到我的谷歌驱动器)
quot;Shared Drivequot; support in Google Apps Script(“共享驱动器Google Apps 脚本中的支持)