因此,我正在通过套接字发送图像文件,在发送之前,我将该文件编码为base64。
发送图像:
const img = cv.imencode('.jpg', frame).toString('base64');
ws.send(img);
现在,在插槽的另一侧,当我收到图像并尝试显示它时,图像似乎已损坏。有人知道我需要做什么才能将其转换回常规图像文件(jpg,jpeg等)吗?
接收图像:
incomingImage = event.data.toString('base64');
IMAGE_SAMPLES.push(incomingImage);
答案 0 :(得分:0)
我不会做任何事情。但是,当您拥有base64
数据时,可以将base64
数据设置为src
标记的<img>
属性。
var imgData = [{"imgURL" : "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBwgHBgkIBwgKCgkLDRYPDQwMDRsUFRAWIB0iIiAdHx8kKDQsJCYxJx8fLT0tMTU3Ojo6Iys/RD84QzQ5OjcBCgoKDQwNGg8PGjclHyU3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3N//AABEIAGAAYAMBIgACEQEDEQH/xAAcAAEAAQUBAQAAAAAAAAAAAAAACAMEBQYHAQL/xAA7EAABAwMCAwMICAYDAAAAAAABAAIDBAURBhITITEHQVEUIlRhcXSUshYXMzVikbHRI1JVcoGhFTI0/8QAFAEBAAAAAAAAAAAAAAAAAAAAAP/EABQRAQAAAAAAAAAAAAAAAAAAAAD/2gAMAwEAAhEDEQA/AO4oiICIiAiIgIiICIiAiIgIiICLxzgxpc4gNAySTyAWN+kNk/rFu+KZ+6ClqvUFJpiyT3Sta97I8NZHGPOkeeQaP3WjR2XWerqKlv8AS6xNtZWxNljoYIHBkLTzA3B3nHxJHP2cl8dt90t9bo+GKiuFLO8V0biyGZrjja/uB9iz3ZzfbTFoeyQ1F0oopo6RjHxvqGNc0jlzBPJBhm1+oezqWkn1bfWXi01tQKdz+GWyUrtrjv8AxNw05GfWPA9NY9sjGvY4Oa4ZBByCFyLt6ulFW6ftkNFWU9Q7y3e7gytfgCN47j+JdC01dKH6O2sSV1M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ql1f6HTfFNXv1Sav8ARKX4pqkYiCOf1Sav9Epfimp9Uer/AEWk+KH7KRiIOGaO7MtTWrVNruFbBStp6acSSFtQHHGD0GF3NEQEREH/2Q=="}];
document.getElementById("sampleImg").src = imgData[0].imgURL;
<html>
<head>
</head>
<body>
<img id="sampleImg">
</body>
</html>