I have the following case. I capture a webcam frame via a canvas with context.getImageData().data. That gives me a nice Array (or UInt8ClampedArray says Bidelman) of ints like [r, g, b, a, r, g, b, a, r, g, b, a, etc.] when I console.log() them. Now I want to send this to a node.js server via binaryjs and of course the data get's scrambled like hell: https://gist.github.com/thgie/6550638
How can I convert the Array to something binary first or how do I get the binary back to a readable array?
Code examples
Client:
client.send(ctx.getImageData(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT).data, {'id': 'bin', 'frame': frame});
Server:
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!");
}
});
});
}
It will look like this:
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]
bufargument you're passing tofs.writeFileis an actual Buffer, right? Something is stringifying your data, apparently.