首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >HTML5画布调整大小(缩小)图像高质量?

HTML5画布调整大小(缩小)图像高质量?
EN

Stack Overflow用户
提问于 2013-09-21 01:48:59
回答 14查看 173K关注 0票数 168

我使用html5画布元素在浏览器中调整图像的大小。事实证明,质量非常低。我发现了这一点:Disable Interpolation when Scaling a ,但它对提高质量没有帮助。

下面是我的css和js代码,以及用Photoshop缩放和在canvas API中缩放的图像。

在浏览器中缩放图像时,如何获得最佳质量?

注意:我希望将大图像缩小为小图像,在画布中修改颜色,并将结果从画布发送到服务器。

CSS:

canvas, img {
    image-rendering: optimizeQuality;
    image-rendering: -moz-crisp-edges;
    image-rendering: -webkit-optimize-contrast;
    image-rendering: optimize-contrast;
    -ms-interpolation-mode: nearest-neighbor;
}

JS:

var $img = $('<img>');
var $originalCanvas = $('<canvas>');
$img.load(function() {


   var originalContext = $originalCanvas[0].getContext('2d');   
   originalContext.imageSmoothingEnabled = false;
   originalContext.webkitImageSmoothingEnabled = false;
   originalContext.mozImageSmoothingEnabled = false;
   originalContext.drawImage(this, 0, 0, 379, 500);
});

使用photoshop调整图像大小:

在画布上调整图像大小:

编辑:

我尝试按照下面的建议在多个步骤中进行缩容:

Resizing an image in an HTML5 canvasHtml5 canvas drawImage: how to apply antialiasing

这是我用过的函数:

function resizeCanvasImage(img, canvas, maxWidth, maxHeight) {
    var imgWidth = img.width, 
        imgHeight = img.height;

    var ratio = 1, ratio1 = 1, ratio2 = 1;
    ratio1 = maxWidth / imgWidth;
    ratio2 = maxHeight / imgHeight;

    // Use the smallest ratio that the image best fit into the maxWidth x maxHeight box.
    if (ratio1 < ratio2) {
        ratio = ratio1;
    }
    else {
        ratio = ratio2;
    }

    var canvasContext = canvas.getContext("2d");
    var canvasCopy = document.createElement("canvas");
    var copyContext = canvasCopy.getContext("2d");
    var canvasCopy2 = document.createElement("canvas");
    var copyContext2 = canvasCopy2.getContext("2d");
    canvasCopy.width = imgWidth;
    canvasCopy.height = imgHeight;  
    copyContext.drawImage(img, 0, 0);

    // init
    canvasCopy2.width = imgWidth;
    canvasCopy2.height = imgHeight;        
    copyContext2.drawImage(canvasCopy, 0, 0, canvasCopy.width, canvasCopy.height, 0, 0, canvasCopy2.width, canvasCopy2.height);


    var rounds = 2;
    var roundRatio = ratio * rounds;
    for (var i = 1; i <= rounds; i++) {
        console.log("Step: "+i);

        // tmp
        canvasCopy.width = imgWidth * roundRatio / i;
        canvasCopy.height = imgHeight * roundRatio / i;

        copyContext.drawImage(canvasCopy2, 0, 0, canvasCopy2.width, canvasCopy2.height, 0, 0, canvasCopy.width, canvasCopy.height);

        // copy back
        canvasCopy2.width = imgWidth * roundRatio / i;
        canvasCopy2.height = imgHeight * roundRatio / i;
        copyContext2.drawImage(canvasCopy, 0, 0, canvasCopy.width, canvasCopy.height, 0, 0, canvasCopy2.width, canvasCopy2.height);

    } // end for


    // copy back to canvas
    canvas.width = imgWidth * roundRatio / rounds;
    canvas.height = imgHeight * roundRatio / rounds;
    canvasContext.drawImage(canvasCopy2, 0, 0, canvasCopy2.width, canvasCopy2.height, 0, 0, canvas.width, canvas.height);


}

下面是如果我使用2级缩容的结果:

下面是如果我使用3级缩容的结果:

下面是如果我使用4级缩容的结果:

下面是如果我使用20级缩容的结果:

注意:事实证明,从1步到2步,图像质量有了很大的提高,但添加到该过程中的步骤越多,图像就变得越模糊。

添加的步骤越多,图像越模糊,有没有办法解决这个问题?

编辑2013-10-04:我尝试了GameAlchemist的算法。这是与Photoshop进行比较的结果。

PhotoShop图像:

GameAlchemist的算法:

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18922880

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档