我应该将哪种类型的数组用于图像文件的 ArrayBuffer?
Which typed array should I use for ArrayBuffer of image files?
我正在使用 FileReader().readAsArrayBuffer()
读取图像文件,并且我正在使用 md5
库来创建文件的散列。 readAsArrayBuffer()
returns 一个 ArrayBuffer
.
const reader = new FileReader();
reader.onload = (event) => {
// NEED TO CREATE TYPED ARRAY FROM
const binary = new Uint8Array(event.target.result);
const md5Hash = md5(binary);
};
reader.readAsArrayBuffer(imageFile);
我们明白了:
The ArrayBuffer object is used to represent a generic, fixed-length raw binary data buffer.
It is an array of bytes, often refered to in other languages as a "byte array".
You cannot directly manipulate the contents of an ArrayBuffer; instead, you create one of the typed array objects or a DataView object which represents the buffer in a specific format, and use that to read and write the contents of the buffer.
因此,我不能直接在md5
函数中使用,我需要先创建一个类型数组对象。
我的选择是:
- Int8Array
- Uint8Array
- Int16Array
- Int32Array
我应该在我的代码中使用以上哪一个?为什么?
更多信息:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
对此没有统一的答案,这基本上取决于消费者以及他们想要处理的格式。
例如,如果您想解析 PNG 的实际文件格式,您实际上需要能够以 BigEndian 顺序读取它的一些块,这意味着您需要在大多数个人计算机上使用 DataView .
但这里 your library 声称它们确实支持 Uint8Array 和 ArrayBuffer。然而,测试它们似乎确实无法处理裸 ArrayBuffers,所以使用 Uint8Array,因为你的图书馆文档就是这么说的。
我正在使用 FileReader().readAsArrayBuffer()
读取图像文件,并且我正在使用 md5
库来创建文件的散列。 readAsArrayBuffer()
returns 一个 ArrayBuffer
.
const reader = new FileReader();
reader.onload = (event) => {
// NEED TO CREATE TYPED ARRAY FROM
const binary = new Uint8Array(event.target.result);
const md5Hash = md5(binary);
};
reader.readAsArrayBuffer(imageFile);
我们明白了:
The ArrayBuffer object is used to represent a generic, fixed-length raw binary data buffer.
It is an array of bytes, often refered to in other languages as a "byte array".
You cannot directly manipulate the contents of an ArrayBuffer; instead, you create one of the typed array objects or a DataView object which represents the buffer in a specific format, and use that to read and write the contents of the buffer.
因此,我不能直接在md5
函数中使用,我需要先创建一个类型数组对象。
我的选择是:
- Int8Array
- Uint8Array
- Int16Array
- Int32Array
我应该在我的代码中使用以上哪一个?为什么?
更多信息:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
对此没有统一的答案,这基本上取决于消费者以及他们想要处理的格式。
例如,如果您想解析 PNG 的实际文件格式,您实际上需要能够以 BigEndian 顺序读取它的一些块,这意味着您需要在大多数个人计算机上使用 DataView .
但这里 your library 声称它们确实支持 Uint8Array 和 ArrayBuffer。然而,测试它们似乎确实无法处理裸 ArrayBuffers,所以使用 Uint8Array,因为你的图书馆文档就是这么说的。