是否可以在 javascript 中查看图像文件的原始数据?
我正在尝试编写将图像转换为十六进制转储的脚本。
如何查看我正在写入图像文件的数据?
最佳答案
你可以用 XHR 做到这一点:
var xhr = new XMLHttpRequest();
xhr.open('GET', '/my/image/file.png', true);
xhr.responseType = 'arraybuffer'; // this will accept the response as an ArrayBuffer
xhr.onload = function(buffer) {
var words = new Uint32Array(buffer),
hex = '';
for (var i = 0; i < words.length; i++) {
hex += words.get(i).toString(16); // this will convert it to a 4byte hex string
}
console.log(hex);
};
xhr.send();
查看 ArrayBuffer 的文档s 和 TypedArray
你可以看到我如何在测试中使用它 here
关于JavaScript - 如何查看图像的原始数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12754812/