我正在尝试编写一个 greasemonkey 脚本,它最好能够处理图像(具体来说,找到图像中最暗的像素)。有没有办法做到这一点,或者我必须嵌入 Flash?
最佳答案
由于它是特定于 Firefox 的,因此您可以使用 canvas 元素。我从来没有写过 greasemonkey 脚本,所以我不知道你会怎么做,但我的想法是,你创建一个新的 Canvas 元素并将图像绘制到 Canvas 上。然后,您可以从 Canvas 中获取像素值。
// Create the canvas element
var canvas = document.createElement("canvas");
canvas.width = image.width;
canvas.height = image.height;
// Draw the image onto the canvas
var ctx = canvas.getContext("2d");
ctx.drawImage(image, 0, 0);
// Get the pixel data
var imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
// Loop through imageData.data - an array with 4 values per pixel: red, green, blue, and alpha
for (int x = 0; x < imageData.width; x++) {
for (int y = 0; y < imageData.height; y++) {
var index = 4 * (y * imageData.width + x);
var r = imageData.data[index];
var g = imageData.data[index + 1];
var b = imageData.data[index + 2];
var a = imageData.data[index + 3];
// Do whatever you need to do with the rgba values
}
}
关于javascript - 使用 Javascript 进行图像处理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/366145/