我在Chrome和Firefox最新版本中运行以下代码。
window.onload = function()
{
var fileInput = document.getElementById('fileInput');
fileInput.addEventListener('change', function(e)
{
var fileCount=fileInput.files.length;
for(var i=0;i<fileCount;i++)
{
var file = fileInput.files[i];
var reader = new FileReader();
reader.onload = function(e)
{
var screenshotImage = new Image();
screenshotImage.onload = function()
{
var canvas = document.createElement('canvas');
canvas.id = makeid(5);
var canvasContext = canvas.getContext('2d');
canvas.width = this.width;
canvas.height = this.height;
canvasContext.drawImage(this,0,0,this.width, this.height);
var imgd = canvasContext.getImageData(0,0,canvas.width,canvas.height);
imagePxielData.push(imgd.data);
document.body.appendChild(canvas);
}
screenshotImage.src = this.result;
}
reader.readAsDataURL(file);
}
});
}
function readData()
{
for(var i=0;i<imageCount;i++)
{
var imgdata=imagePxielData[i];
tmp=0;
for (var j = 0, n = imgdata.length; j < n; j += 4)
{
var r = imgdata[j];
var g = imgdata[j+1];
var b = imgdata[j+2];
if(tmp<100)
console.log("r=>"+r+"g=>"+g+"b=>"+b);
tmp++;
}
}
}
通过上传图片运行后,前100个RGB值在Chrome和Firefox中显示不同的值。 Chrome显示正确的值,Firefox显示不同的值(不正确的值)。
为什么这会显示不同的值? 如何修复此问题以在Chrome和Firefox中显示正确的RGB值?
谢谢。