Transfer an image

This commit is contained in:
Lewis Moten
2024-05-12 23:41:37 -04:00
parent 838761ae82
commit e8634b98d4
7 changed files with 143 additions and 66 deletions

View File

@@ -61,3 +61,20 @@ export const bitsToInt = (bits, bitLength) => {
2
);
}
export const urlToBytes = src => {
const xhr = new XMLHttpRequest();
// we need a synchronous response.
xhr.open('GET', src, false);
xhr.overrideMimeType('text/plain; charset=x-user-defined');
xhr.send(null);
if(xhr.status !== 200) return [];
let bytes = [];
for(let i = 0; i < xhr.response.length; i++) {
bytes.push(xhr.response.charCodeAt(i) & 0xFF);
}
return bytes;
}
export const bytesToUrl = bytes => {
const blob = new Blob([new Uint8Array(bytes)]);
return URL.createObjectURL(blob);
}