Applying blur filter to BitmapData

≯℡__Kan透↙ 提交于 2019-12-24 01:19:51

问题


Here's the code I am using to blur an image using BitmapData. The function is called 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;

回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/6003706/applying-blur-filter-to-bitmapdata

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!