In trying to send document scans as binary data over a web socket the first block of code below works fairly well. Since the first byte contains information concerning what exactly to do with the blob, the image data starts at an offset of one byte; and because it appears that blob.slice() returns a copy of the original blob rather than simply reading it, this likely is not the most efficient method of processing the blob because it makes a copy of the entire blob less one byte.
socket.onmessage = function(evt) {
if (evt.data instanceof Blob) {
evt.data.slice(0,1).arrayBuffer()
.then( (b) => {
let v = new DataView(b),
r = v.getUint8(0);
// Do something based on r and then display scan.
let objectURL = URL.createObjectURL( evt.data.slice(1) );
imgscan.onload = () => {
URL.revokeObjectURL( objectURL );
};
imgscan.src = objectURL;
})
If the websocket's binaryType is changed to arraybuffer, it's a bit easier to read the first byte and apparently does not make a copy, but I do not understand how to get the image data out of the buffer to display it; that is, I don't see which method to apply to the DataView to get the raw binary data of the image. Would you please explain or point me to the correct documentation? Thank you.
This SO question seems similar but was not helpful to me anyway.
socket.onmessage = function(evt) {
if ( evt.data instanceof ArrayBuffer ) {
let v = new DataView(evt.data),
r = v.getUint8(0);
// How to get the raw binary image data out of
// the array buffer starting at the first byte?
new Uint8Array(evt.data, 1)), but I don’t think you can display it in an img element without creating a new blob. (The other options are data URLs and JavaScript decoding onto a canvas, which are worse.)getInt8()but that doesn't seem helpful. If the array buffer has to be converted to a new blob, then it would appear that there is no advantage to setting the socket'sbinaryTypetoarraybuffer. It's quick enough as is and I' likely wasting time trying to optimize something that cannot be changed. It makes me wonder if sending the data as text with the image as base64 would be more efficient. It's, I think, 33% larger but wouldn't need to be copied to separate the equivalent of the first byte.