未捕获错误:INDEX_SIZE_ERR:DOM异常1,getImageData

时间:2011-11-07 10:31:59

标签: javascript html5

我使用以下代码将我的图像转换为灰度,在本地计算机上它很好但是当我在服务器上部署它时它传递未捕获错误:INDEX_SIZE_ERR:DOM例外1 我想知道是它由于域名不同,我不确定但问题是由于“getImageData”。此外,此问题仅适用于chrome。

我已经阅读了很多帖子并尝试了很多,但似乎对我没什么影响。

提前致谢...

function grayscale(img) {
         var canvas = document.createElement('canvas');
         var ctx = canvas.getContext('2d');
         var imgObj = new Image();
         imgObj.src = img;
         canvas.width = imgObj.width;
         canvas.height = imgObj.height;
         ctx.drawImage(imgObj, 0, 0);
         var imgPixels = ctx.getImageData(0, 0, canvas.width, canvas.height);
         for (var y = 0; y < imgPixels.height; y++) {
         for (var x = 0; x < imgPixels.width; x++) {
         var i = (y * 4) * imgPixels.width + x * 4;
         var avg = (imgPixels.data[i] + imgPixels.data[i + 1] + imgPixels.data[i + 2]) / 3;
                    imgPixels.data[i] = avg;
                    imgPixels.data[i + 1] = avg;
                    imgPixels.data[i + 2] = avg;
                }
            }
            ctx.putImageData(imgPixels, 0, 0, 0, 0, imgPixels.width, imgPixels.height);
            return canvas.toDataURL();
        }

3 个答案:

答案 0 :(得分:3)

这对我来说很好用

// img is HTMLImageElement
function makeGreyScale(img) {
  var canvas = document.createElement('canvas');
  var ctx = canvas.getContext('2d');
  var imgObj = new Image();
  imgObj.src = img.src;
  imgObj.onload = function(){
    canvas.width = imgObj.width;
    canvas.height = imgObj.height;
    ctx.drawImage(imgObj, 0, 0);
    var imgPixels = ctx.getImageData(0, 0, canvas.width, canvas.height);

    for(var y = 0; y < imgPixels.height; y++){
      for(var x = 0; x < imgPixels.width; x++){
        var i = (y * 4) * imgPixels.width + x * 4;
        var avg = (imgPixels.data[i] + imgPixels.data[i + 1] + imgPixels.data[i + 2]) / 3;
        imgPixels.data[i] = avg;
        imgPixels.data[i + 1] = avg;
        imgPixels.data[i + 2] = avg;
      }
    }
    ctx.putImageData(imgPixels, 0, 0, 0, 0, imgPixels.width, imgPixels.height);
    img.src= canvas.toDataURL();
  };
}

答案 1 :(得分:2)

很可能你不使用window.load调用脚本而是使用DOM加载。我使用灰度脚本遇到了同样的问题。这是适用于我的代码:

$(window).load(function(){

    imageColorize();
});


//Black and white images to color
function imageColorize(){

    // Fade in images so there isn't a color "pop" document load and then on window load
    $(".colorize img").fadeIn(500);

    // clone image
    $('.colorize img').each(function(){
        var el = $(this);
        el.css({"position":"absolute"}).wrap("<div class='img_wrapper' style='display: inline-block'>").clone().addClass('img_grayscale').css({"position":"absolute","z-index":"998","opacity":"0"}).insertBefore(el).queue(function(){
            var el = $(this);
            el.parent().css({"width":this.width,"height":this.height});
            el.dequeue();
        });
        this.src = grayscale(this.src);
    });

    // Fade image
    $('.colorize img').mouseover(function(){
        $(this).parent().find('img:first').stop().animate({opacity:1}, 1000);
    });
    $('.colorize .img_grayscale').mouseout(function(){
        $(this).stop().animate({opacity:0}, 1000);
    });

}

// Grayscale w canvas method
function grayscale(src){
    var canvas = document.createElement('canvas');
    var ctx = canvas.getContext('2d');
    var imgObj = new Image();
    imgObj.src = src;
    canvas.width = imgObj.width;
    canvas.height = imgObj.height;
    ctx.drawImage(imgObj, 0, 0);
    var imgPixels = ctx.getImageData(0, 0, canvas.width, canvas.height);
    for(var y = 0; y < imgPixels.height; y++){
        for(var x = 0; x < imgPixels.width; x++){
            var i = (y * 4) * imgPixels.width + x * 4;
            var avg = (imgPixels.data[i] + imgPixels.data[i + 1] + imgPixels.data[i + 2]) / 3;
            imgPixels.data[i] = avg;
            imgPixels.data[i + 1] = avg;
            imgPixels.data[i + 2] = avg;
        }
    }
    ctx.putImageData(imgPixels, 0, 0, 0, 0, imgPixels.width, imgPixels.height);
    return canvas.toDataURL();
}

答案 2 :(得分:0)

我在IE9,IE10中遇到了类似的问题。

我建议你使用(动态创建图像对象):

imgObj.naturalWidth;
imgObj.naturalHeight;

而不是

imgObj.width;
imgObj.height;