問題描述
我有一個自定義 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)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!