问题描述
我遇到创建ImageData的问题。我收到以下错误消息:
I am having an issue with creating ImageData. I am getting the following error message:
以下是我正在运行的方法:
Here is the method that I am running:
public setPixelData(data: Buffer, width: number, height: number) {
var imageData = new ImageData(new Uint8ClampedArray(data), width, height);
this.canvas.getContext('2d').putImageData(imageData, 0, 0);
}
我已经转储了数据,这就是显示的内容:
I have dumped the data and this is what is showing:
data = Uint8Array[632028]
width = 720
height = 720
那么,这个错误的原因是什么,以及如何修复?
So, what would be the cause of this error, and how can it be fixed?
推荐答案
我遇到了同样的问题,显然数组大小需要是宽度,高度和4 的倍数。
正确指出乘法4是由于RGBA颜色空间,它由红色,绿色,蓝色和alpha四个通道组成。
I had the same problem, apparently the array size needs to be the multiple of width, height and 4.As Daniel pointed out correctly the multiplication with 4 is due to the RGBA color space, which consists of the four channels red, green, blue, and alpha.
所以对你来说case:720 * 720 * 4等于2073600
So for your case: 720 * 720 * 4 equals 2073600
var width = 720, height = 720;
var data = new ImageData(
new Uint8ClampedArray(4 * width * height),
720,
720
);
console.log(data);
这篇关于ImageData字节长度不是4 *宽度的倍数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!