Flex/Actionscript 白色到透明 [英] Flex/Actionscript White to Transparent

查看:30
本文介绍了Flex/Actionscript 白色到透明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的 Flex 3 应用程序中使用 actionscript 编写一些内容,该脚本将拍摄图像,当用户单击按钮时,它将去除所有白色(ish)像素并将它们转换为透明,我说白色(ish)因为我试过完全是白色的,但我在边缘得到了很多伪影.我使用以下代码已经有点接近了:

I am trying to write something in my Flex 3 application with actionscript that will take an image and when a user clicks a button, it will strip out all the white(ish) pixels and convert them to transparent, I say white(ish) because I have tried exactly white, but I get a lot of artifacts around the edges. I have gotten somewhat close using the following code:

targetBitmapData.threshold(sourceBitmapData, sourceBitmapData.rect, new Point(0,0), ">=", 0xFFf7f0f2, 0x00FFFFFF, 0xFFFFFFFF, true);

但是,它也会使红色或黄色消失.为什么要这样做?我不完全确定如何使这项工作.还有其他更适合我需求的功能吗?

However, it also makes red or yellows disappear. Why is it doing this? I'm not exactly sure how to make this work. Is there another function that is better suited for my needs?

推荐答案

不久前,我和一个朋友试图为一个项目执行此操作,但发现编写一个在 ActionScript 中执行此操作的内联方法非常慢.您必须扫描每个像素并对其进行计算,但事实证明,使用 PixelBender 执行此操作的速度快如闪电(如果您可以使用 Flash 10,否则您会卡在缓慢的 AS 中).

A friend and I were trying to do this a while back for a project, and found writing an inline method that does this in ActionScript to be incredibly slow. You have to scan each pixel and do a computation against it, but doing it with PixelBender proved to be lightning fast (if you can use Flash 10, otherwise your stuck with slow AS).

像素弯曲器代码如下:

input image4 src;
output float4 dst;

// How close of a match you want
parameter float threshold
<
  minValue:     0.0;
  maxValue:     1.0;
  defaultValue: 0.4;
>;

// Color you are matching against.
parameter float3 color
<
  defaultValue: float3(1.0, 1.0, 1.0);
>;

void evaluatePixel()
{
  float4 current = sampleNearest(src, outCoord());
  dst = float4((distance(current.rgb, color) < threshold) ? 0.0 : current);
}

如果您需要在 AS 中执行此操作,您可以使用以下内容:

If you need to do it in AS you can use something like:

function threshold(source:BitmapData, dest:BitmapData, color:uint, threshold:Number) {
  dest.lock();

  var x:uint, y:uint;
  for (y = 0; y < source.height; y++) {
    for (x = 0; x < source.width; x++) {
      var c1:uint = source.getPixel(x, y);
      var c2:uint = color;
      var rx:uint = Math.abs(((c1 & 0xff0000) >> 16) - ((c2 & 0xff0000) >> 16));
      var gx:uint = Math.abs(((c1 & 0xff00) >> 8) - ((c2 & 0xff00) >> 8));
      var bx:uint = Math.abs((c1 & 0xff) - (c2 & 0xff));

      var dist = Math.sqrt(rx*rx + gx*gx + bx*bx);

      if (dist <= threshold)
        dest.setPixel(x, y, 0x00ffffff);
      else
        dest.setPixel(x, y, c1);
    }
  }
  dest.unlock();
}

这篇关于Flex/Actionscript 白色到透明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆