开发者

Applying blur filter to BitmapData

Here's the code I am using to blur an image using BitmapData. The function is calle开发者_StackOverflow中文版d on a Slider_changeHandler(event:Event):voidevent and the value of the slider is passed to the function as blurvalue.

The problem is the function works but seems to be cummalative (if that's the correct word!), that is, suppose I slide it to the maximum and after that try to reduce the blur by sliding it back towards the front the blur still keeps increasing. How do I make it to work so when I will slide it up blur increases and when I slide it back blur decreases and when slider is at 0, no blur is applied.

            var blur:BlurFilter = new BlurFilter();
            blur.blurX = blurvalue; 
            blur.blurY = blurvalue; 
            blur.quality = BitmapFilterQuality.MEDIUM;
            bitmapdata.applyFilter(bitmapdata,new
                Rectangle(0,0,bitmapdata.width,bitmapdata.height),new Point(0,0),
                blur);
            return bitmapdata;


how about returning a clone of the original bitmapData with the filter applied ?

e.g.

var result:BitmapData = bitmapdata.clone();
var blur:BlurFilter = new BlurFilter();
blur.blurX = blurvalue; 
blur.blurY = blurvalue; 
blur.quality = BitmapFilterQuality.MEDIUM;
result.applyFilter(result,new
Rectangle(0,0,bitmapdata.width,bitmapdata.height),new Point(0,0),blur);
return result;

Also, if you're using the BlurFilter, you might need a larger rectangle, depending on the amount of blur. For that, you can use the generateFilterRect() method to get correct sized rectangle for the filter.


If I were you, I'd take the BitmapData and put it in a Bitmap object, then add the filters:

var bitmap:Bitmap = new Bitmap(bitmapData);
var blur:BlurFilter = new BlurFilter();
blur.blurX = blurvalue; 
blur.blurY = blurvalue; 
blur.quality = BitmapFilterQuality.MEDIUM;
bitmap.filters = [blur];

By doing this (interchanging the filters array), you're not making the filters cumulative.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜