How to display a JPG image from a Node.js buffer (UInt8Array)

前端 未结 3 1906
夕颜
夕颜 2020-12-10 07:08

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 ex

相关标签:
3条回答
  • 2020-12-10 07:26

    You need to add img to the DOM.

    Also if you are generating the buffer in the main process you need to pass it on the the render process of electron.

    0 讨论(0)
  • 2020-12-10 07:41

    facepalm...There was an extra leading space in the text...

     var getImageResult = addon.getlatestimage();
     var b64encoded = btoa(String.fromCharCode.apply(null, getImageResult.imagebuffer));
     var datajpg = "data:image/jpg;base64," + b64encoded;
     document.getElementById("myimage").src = datajpg;
    

    Works perfectly.

    0 讨论(0)
  • 2020-12-10 07:45

    Is that what you want?

    // 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');
    
    0 讨论(0)
提交回复
热议问题