AS3:如何将彩色位图的位图数据更改为黑白?

发布于 2024-07-27 13:08:55 字数 216 浏览 4 评论 0原文

如何在 AS3 中将彩色位图的位图数据更改为黑白? 我正在为 Flash 中的 CMS 开发一个简单的图像编辑器工具。

人们应该能够将上传的位图的颜色切换为黑白。 我希望位图数据本身发生变化因此我可以随后使用 Adob​​e 的 JPGEncoder 类将其写入 ByteArray。

How can I change the bitmapdata of a coloured Bitmap to Black and White in AS3 ?
I'm developing a simple image editor tool for a CMS in flash.

People should be able to switch the colour of the uploaded Bitmap to black and white. I want the bitmapdata itself to change So I can write it to a ByteArray with Adobe's JPGEncoder Class afterwards.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

2024-08-03 13:08:55

这将是我认为最优雅的解决方案(source 是您的 BitmapData):

const rc:Number = 1/3, gc:Number = 1/3, bc:Number = 1/3;
source.applyFilter(source.bitmapData, source.bitmapData.rect, new Point(), new ColorMatrixFilter([rc, gc, bc, 0, 0,rc, gc, bc, 0, 0, rc, gc, bc, 0, 0, 0, 0, 0, 1, 0]));

使用 flash.geom::Pointflash .filters::ColorMaxtrixFilter ...

ColorMatrixFilter 允许做很多事情,例如色调偏移、着色、增亮、变暗和去饱和度等等...否则 BitmapData::paletteMapBitmapData::colorTransform 是很好的补充...

只是想指出,使用以下内容

const rc:Number = 1/3, gc:Number = 1/2, bc:Number = 1/6; 

看起来更自然一点,因为主观上,#00FF00 是比 #FF0000 更亮,而 #FF0000 又比 #0000FF 更亮,

祝你好运...;)

greetz

back2dos

this would be the most elegant solution i presume (with source being you BitmapData):

const rc:Number = 1/3, gc:Number = 1/3, bc:Number = 1/3;
source.applyFilter(source.bitmapData, source.bitmapData.rect, new Point(), new ColorMatrixFilter([rc, gc, bc, 0, 0,rc, gc, bc, 0, 0, rc, gc, bc, 0, 0, 0, 0, 0, 1, 0]));

with flash.geom::Point and flash.filters::ColorMaxtrixFilter ...

ColorMatrixFilter allows many things, such as hue shifts, colorisation, lightening, darkening and desaturation and so on ... otherwise BitmapData::paletteMap and BitmapData::colorTransform are good complements ...

just wanted to note, that using the following

const rc:Number = 1/3, gc:Number = 1/2, bc:Number = 1/6; 

looks a little more natural, since subjectively, #00FF00 is brighter than #FF0000, which in turn is brighter than #0000FF

good luck then ... ;)

greetz

back2dos

疾风者 2024-08-03 13:08:55

好吧,只需使用 getPixel 和 setPixel,并对颜色进行平均(我确信可能还有另一种方法可以使用滤镜或其他方法来实现此目的):

for(int i = 0; i < bitmapData.height; i++)
{
    for(int j = 0; j < bitmapData.width; j++)
    {
        var color:uint = bitmapData.getPixel(i, j);
        var red:uint = color & 0xFF0000 >> 16;
        var green:uint = color & 0x00FF00 >> 8;
        var blue:uint = color & 0x0000FF >> 0;
        var bwColor:uitn = red + green + blue / 3;
        bwColor = bwColor << 16 + bwColor << 8 + bwColor; // puts the average in each channel

        bitmapData.setPixel(i, j, bwColor);
    }
}

Well, just using getPixel and setPixel, and averaging the colors (I'm sure there may be another way to do this with a filter or something):

for(int i = 0; i < bitmapData.height; i++)
{
    for(int j = 0; j < bitmapData.width; j++)
    {
        var color:uint = bitmapData.getPixel(i, j);
        var red:uint = color & 0xFF0000 >> 16;
        var green:uint = color & 0x00FF00 >> 8;
        var blue:uint = color & 0x0000FF >> 0;
        var bwColor:uitn = red + green + blue / 3;
        bwColor = bwColor << 16 + bwColor << 8 + bwColor; // puts the average in each channel

        bitmapData.setPixel(i, j, bwColor);
    }
}
相守太难 2024-08-03 13:08:55

实际上,您可以以更简单的方式使用 back2dos 的解决方案:

const rc:Number = 1/3, gc:Number = 1/3, bc:Number = 1/3;
mc.filters = [ new ColorMatrixFilter([rc, gc, bc, 0, 0,rc, gc, bc, 0, 0, rc, gc, bc, 0, 0, 0, 0, 0, 1, 0]) ];

其中 mc 是 MovieClip 或 Sprite 容器,其中位图数据作为视觉元素存在。
谢谢 back2dos :)

actually, you can use back2dos 's solution in a much easier way:

const rc:Number = 1/3, gc:Number = 1/3, bc:Number = 1/3;
mc.filters = [ new ColorMatrixFilter([rc, gc, bc, 0, 0,rc, gc, bc, 0, 0, rc, gc, bc, 0, 0, 0, 0, 0, 1, 0]) ];

where mc is the MovieClip or Sprite container where the bitmapdata exists as a visual element.
thanks back2dos :)

一抹淡然 2024-08-03 13:08:55

谢谢饼干,
复制原始位图数据确实是恢复颜色的最简单的解决方案。

   function clearColor() {
      colorbmd = new BitmapData(source.width, source.height);
      colorbmd = source.clone();
      //apply filter here    
    }
    function restoreColor() {
      source = colorbmd;
    }

我想一旦你转换了
图像变成黑白你就不能去
返回(您在执行过程中会丢失信息)
转换)。 你必须做一个
在应用过滤器之前复制。 –
幸运饼干

Thanks Cookie,
Copying the original bitmapdata was indeed the easiest solution to restore color.

   function clearColor() {
      colorbmd = new BitmapData(source.width, source.height);
      colorbmd = source.clone();
      //apply filter here    
    }
    function restoreColor() {
      source = colorbmd;
    }

I think once you've converted the
image to black and white you can't go
back (You lose information during the
conversion). You will have to make a
copy before you apply the filter. –
CookieOfFortune

一枫情书 2024-08-03 13:08:55

我用这个剪刀:

//Tweens to black and white
TweenLite.to(DisplayObjectYouWantToTween ,1,{colorMatrixFilter:{matrix:[0.3,0.59,0.11,0, 0,0.3,0.59,0.11,0,0,0.3,0.59,0.11,0,0,0,0,0,1,0]}});

//Tween from black and white to Color
TweenLite.to(DisplayObjectYouWantToTween,1,{colorMatrixFilter:{matrix:[1,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0]}});

I use this snipper:

//Tweens to black and white
TweenLite.to(DisplayObjectYouWantToTween ,1,{colorMatrixFilter:{matrix:[0.3,0.59,0.11,0, 0,0.3,0.59,0.11,0,0,0.3,0.59,0.11,0,0,0,0,0,1,0]}});

//Tween from black and white to Color
TweenLite.to(DisplayObjectYouWantToTween,1,{colorMatrixFilter:{matrix:[1,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0]}});
翻了热茶 2024-08-03 13:08:55

我尝试了两种方法。
看起来 ColorMatrixFilter 方法在处理大图像时工作得更快。

有没有什么方法可以去除过滤器? 或者我应该再次更改 ColorMatrixFilter 值吗?

I tried both methods.
Seems like the ColorMatrixFilter method works a lot faster when working with large images.

Is there some method to remove the Filter too? or shoud I change the ColorMatrixFilter values again?

池木 2024-08-03 13:08:55

感谢您的方法,我创建了一个小工具,可以让您使用这些值:
http://bjornson.inhb.de/?p=159

初始值是Macke 提出:RGB 为 30%、59% 和 11%。

您还可以加载外部图像并尝试获得图像的最佳效果。

Thank you for the approach, I created a small tool, that lets you play around with the values:
http://bjornson.inhb.de/?p=159

The initial values are the ones proposed by macke: 30%, 59% and 11% for rgb.

You can also load external images and try out for the best result for your images.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文