JavaScript - 如何查看图像的原始数据 [英] JavaScript - how to view raw data for an image

查看:145
本文介绍了JavaScript - 如何查看图像的原始数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在javascript中查看图像文件的原始数据?

Is it possible to view the raw data for an image file in javascript?

我正在尝试编写一个将图像转换为十六进制转储的脚本。

I'm trying to write a script that converts an image into its hex dump.

如何查看我写入图像文件的数据?

How can I view the data I'm writing to the image file?

推荐答案

你可以用XHR做到这一点:

You can do this with 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 s

您可以看到我如何在测试中使用它这里

And you can see how I use it in testing here

这篇关于JavaScript - 如何查看图像的原始数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆