简体   繁体   English

画布中的图像处理

[英]Image manipulation in canvas

I have a number of images within #mycontainer like: 我在#mycontainer有许多图像,例如:

<div id="mycontainer">
    <img src="http://localhost:8080/images/my-image.png" />
    …
</div>

I need to convert those into B/W. 我需要将它们转换为黑白。 Pretty common task but I didn't find any solution for this that would just work for me — there is some problem with the actions execution. 这是很普通的任务,但是我没有找到任何适合我的解决方案-动作执行存在一些问题。 What I have now is the following: 我现在所拥有的是:

function grayscale(src) {
    var ctx = document.createElement('canvas').getContext('2d'),
        imgObj = new Image(),
        pixels, i, n, gs, url;

    // wait until the image has been loaded
    imgObj.onload = function () {
        ctx.canvas.width = this.width;
        ctx.canvas.height = this.height;
        ctx.drawImage(this, 0, 0);
        pixels = ctx.getImageData(0, 0, this.width, this.height);
        for (i = 0, n = pixels.data.length; i < n; i += 4) {
            gs = pixels.data[i] * 0.3 + pixels.data[i+1] * 0.59 + pixels.data[i+2] * 0.11;
            pixels.data[i] = gs;   // red
            pixels.data[i+1] = gs;   // green
            pixels.data[i+2] = gs;   // blue
        }
        ctx.putImageData(pixels, 0, 0);
    };
    imgObj.src = src;
    return ctx.canvas.toDataURL('image/png');
}

In general the actions are: 通常,这些动作是:

  • I supply src of an image to process, 我提供图片的src进行处理,
  • wait for the image to be fully loaded, 等待图像完全加载,
  • draw the image on the canvas, convert the pixels and put the converted pixels back on the canvas 在画布上绘制图像,转换像素并将转换后的像素放回画布
  • then I want the data URL of the resulting image from the canvas to be returned. 那么我要返回画布上生成的图像的数据URL。

Right now, when in Developer Tools I am tring something like: 现在,在开发人员工具中时,我在进行类似以下操作:

c = $('#mycontainer').find('img')[0];
grayscale(c.src);

I get back data URL of a fully transparent default 300px x 150px canvas as if that imgObj.onload() doesn't exist in the script at all. 我得到完全透明的默认300px x 150px画布的数据URL,好像该imgObj.onload()在脚本中根本不存在。

Can anybody point me to a mistake here please? 有人可以在这里指出我的错误吗?

Quick answer: Since you're using jQuery, you might look at the jQuery desaturate plugin , which might do what you need. 快速解答:由于您使用的是jQuery,因此您可以查看jQuery desaturate插件 ,它可以满足您的需要。

Longer answer in reference to your code - imgObj.onload is an asynchronous callback function, so it won't have executed by the time you reach your return statement. 参考代码的答案更长imgObj.onload是一个异步回调函数,因此在您return语句时将不会执行。 You'll need to execute any code that requires the post- onload data URL from inside the onload callback. 您需要从onload回调内部执行所有需要后onload数据URL的代码。 One way to do this would be to have grayscale take a callback argument: 一种方法是让grayscale使用回调参数:

function grayscale(src, callback) {
    // ... snip ...

    // wait until the image has been loaded
    imgObj.onload = function () {
        // ... snip ...
        ctx.putImageData(pixels, 0, 0);
        // now fire the callback
        callback(ctx.canvas.toDataURL('image/png'));
    };
    imgObj.src = src;
}

c = $('#mycontainer').find('img')[0];
grayscale(c.src, function(dataUrl) {
    // further stuff with grayscale dataUrl
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM