我有以下情况。我通过带有context.getImageData().data
. 这给了我一个很好的整数数组(或 UInt8ClampedArray 说 Bidelman),就像[r, g, b, a, r, g, b, a, r, g, b, a, etc.]
我在console.log()
他们时一样。现在我想通过 binaryjs 将它发送到 node.js 服务器,当然数据会像地狱一样被打乱:https ://gist.github.com/thgie/6550638
如何首先将数组转换为二进制文件,或者如何将二进制文件恢复为可读数组?
代码示例
客户:
client.send(ctx.getImageData(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT).data, {'id': 'bin', 'frame': frame});
服务器:
client.on('stream', function(stream, meta){
var bufs = [];
stream.on('data', function(data){
bufs.push(data);
});
stream.on('end', function(){
var buf = Buffer.concat(bufs);
fs.writeFile(__dirname + "/tmp/"+meta.frame+".dat", buf, "binary", function(err) {
if(err) {
console.log(err);
} else {
console.log("The file was saved!");
}
});
});
}
它看起来像这样:
H>DÿNDJÿL@FÿPCJÿR@KÿQ?IÿLDPÿKCOÿMCKÿLAIÿL@DÿL@DÿLBGÿKAFÿIAHÿLDLÿLAIÿLAIÿO@IÿO@IÿIAJÿJBKÿMCIÿNDJÿQEIÿQEIÿPGHÿOFGÿMIHÿMIHÿPDFÿNCEÿIBDÿLEHÿPDFÿPDFÿMHLÿLGKÿMHLÿNIMÿOGLÿNFKÿOEMÿPFNÿRFSÿPERÿPERÿPERÿRGOÿSHPÿSGMÿSGMÿRJRÿQIPÿQJNÿRKPÿRKPÿQJNÿSGMÿSGMÿOHJÿSLOÿQJLÿRKMÿSIJÿTKKÿQLPÿPJNÿSLQÿRKPÿSLQÿUMRÿSKSÿUMTÿVMWÿSKUÿUJUÿRGQÿUMTÿUMTÿSKUÿVMWÿSMTÿSMTÿQNVÿQNVÿSMVÿTNWÿZMVÿXKTÿUJUÿVKVÿTOSÿTOSÿWQUÿWQUÿYTVÿYTVÿXRVÿUPTÿYSZÿYSZÿXRVÿZUYÿWPRÿYRTÿWRQÿXSRÿVOOÿWPPÿXRVÿXRVÿZOYÿ[P[ÿZO[ÿ\Q^ÿZT_ÿZT_ÿZV`ÿZV`ÿ]T^ÿ]T^ÿ^TZÿ`V[ÿ[VXÿ[VXÿ]UZÿ^V[ÿZV`ÿ[Waÿ_Xdÿ_Xdÿ]X`ÿ]X`ÿaZ_ÿaZ_ÿ_Y]ÿ`[_ÿ`Y]