简体   繁体   English

javascript 从缓冲区 (nodejs/socket.io) 将图像绘制到 html

[英]javascript draw image into html from buffer (nodejs/socket.io)

(sorry for my english) Hi, i have an appplication created with nodejs to push image data into a webpage. (对不起我的英语)嗨,我有一个用 nodejs 创建的应用程序,用于将图像数据推送到网页中。

the data from nodejs server is pushed into webpage using socket.io使用 socket.io 将来自 nodejs 服务器的数据推送到网页中

This data is the complete image, i try to write to disc to see the image and is good.该数据是完整的图像,我尝试写入磁盘以查看图像并且很好。 The data is put into a buffer to encode in base64 then send to the webpage and i try to show using 'data:image/png;base64,'+data but nothing happend... The data seems to be "complete" including the headers of the PNG image.数据被放入缓冲区以在 base64 中编码,然后发送到网页,我尝试使用 'data:image/png;base64,'+data 显示,但没有发生任何事情......数据似乎是“完整的”,包括PNG图像的标题。

The server use thrift to communicate with another client (this in C++), this client create the images and send to the nodejs-thrift server, when a image is receive, the nodejs-socket.io server push into webpage.服务器使用 thrift 与另一个客户端(在 C++ 中)通信,该客户端创建图像并发送到 nodejs-thrift 服务器,当接收到图像时,nodejs-socket.io 服务器推送到网页。

some code: serverside一些代码:服务器端

var http = require('http'),
url = require('url'),
fs  = require('fs'),
sys = require('sys');
var hserver = http.createServer(function(request,response){
var path = url.parse(request.url).pathname;
switch(path){
   case '/':

        fs.readFile(__dirname + '/index.html',function(err,data){
           if(err) return send404(res);
           response.writeHead(200,{'Content-Type':'text/html'});
           response.write(data,'utf8');
           response.end(); 
        });
    break;
    default: send404(response);
}
   }),
send404 = function(res){
 res.writeHead(404);
 res.write('404');
 res.end();
};

hserver.listen(8080);
var io = require('socket.io');
var socket = io.listen(hserver);
//this is the function in the Thrift part to send the image data to webpage using socket.io
var receiveImage = function(image,success){
buff= new Buffer(image.data.toString('base64'),'base64');
socket.broadcast({
    data: buff.toString('base64'),
    width: image.width,
    height: image.height
});
success(true);
}

In the webpage i try to load the image using the on.message callback of socket.io在网页中,我尝试使用 socket.io 的 on.message 回调加载图像

<script>
var socket = new io.Socket(null,{port:8080});
socket.connect();
socket.on('message',function(obj){
        document.getElementById('imagenes').src='data:image/png;base64,'+obj.data.toString();
});
socket.on('connect',function(){         
    document.getElementById('stream').innerHTML = "Conectado!"
});
</script>

But i can't see the image.. The obj.data.toString('base64') create a string like this:但我看不到图像.. obj.data.toString('base64') 创建一个这样的字符串:

 PNGIHDRKIDATxYw5L1LlFMM/migzWUadOadLUd+43PIfPU9VU5AJAg3dIWUmHAMajS8GUuq ............
 .............
 /NIlWDHUDmIPdHIEND

is the all data of the image..是图像的所有数据..

how can i show the image??我怎样才能显示图像? (maybe using img tag or into canvas) (也许使用 img 标签或进入画布)

Thanks in advance.提前致谢。

EDIT: Some changes to code, but nothing change.. my guess.. the image don't show because the data (PNGIHDRKIDATxYw......PdHIENDB) isn't the correct data.. i search about this and the png data used to show images inline is diferent (without the header PNGIHDRKIDAT and the tail ENDB), i remove the header and tail and don't show.. maybe the data i receive isn't correct, but if i try to write to filesystem in the server side using nodejs (fs) i get the correct images (with fs.writeFileSync("imagen.png",image.data.toString('binary'),'binary') ) using binary encode, but with base64 encode theres no image.编辑:对代码进行了一些更改,但没有任何改变..我的猜测..图像不显示,因为数据(PNGIHDRKIDATxYw......PdHIENDB)不是正确的数据..我搜索这个和 png用于显示内联图像的数据不同(没有 header PNGIHDRKIDAT 和尾部 ENDB),我删除了 header 和尾部并且不显示。也许我收到的数据不正确,但如果我尝试写入文件系统在使用nodejs(fs)的服务器端,我使用二进制编码获得正确的图像(使用 fs.writeFileSync("imagen.png",image.data.toString('binary'),'binary') ),但使用 base64 编码没有图像。

In the C++ side (where i create the images and send them to the nodejs server).在 C++ 端(我创建图像并将它们发送到 nodejs 服务器)。 I need to encode the data in base64.我需要对 base64 中的数据进行编码。 Then in node i get this data put into a buffer with base64 encoding and send it to the webpage.然后在节点中,我将此数据放入具有 base64 编码的缓冲区并将其发送到网页。

Your client side code looks fine (except for the fact that .toString('base64') doesn't do anything to a String object whilst in-browser, only to Buffer objects in node), I'm making a guess that the problem is in receiveImage , which should be:您的客户端代码看起来不错(除了.toString('base64')在浏览器中时对String object 没有任何作用,仅对节点中的Buffer对象做任何事情),我猜这个问题在receiveImage中,应该是:

var receiveImage = function(image,success) {
    socket.broadcast({
        data: image.data.toString('base64'),
        width: image.width,
        height:image.height
    });
    success(true);
}

It has been my experience that a base64 string of an image should be loaded with a data url like so:根据我的经验,图像的 base64 字符串应加载数据 url ,如下所示:

<img alt="some image" src="data(PNGIHDRKIDATxYw5L1LlFMM/migzWUadOadLUd+43PIfPU9VU5AJAg3dIWUmHAMajS8GUuq .........)" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM