2013-01-24 63 views
-2

我正在使用java,並且生成了一個圖像。當鼠標移過生成的圖像時,我需要圖像使用模糊或像素濾鏡。如何模糊鼠標下的圖像

我應該用什麼方法來實現這個目標?

+0

我剛剛使用java.awt.image.ConvolveOp模糊了整個圖像。 – ppst99

+1

「你有什麼嘗試?」,換句話說,**你可以在這裏粘貼你試過的代碼嗎?** – BackSlash

+1

1)爲了更好地幫助,請發佈[SSCCE](http://sscce.org/)。 2)解決方案可能是繪製清晰的圖像,然後將剪輯應用到鼠標下的區域並繪製模糊的圖像。 –

回答

0
public void test(int x, int y){ // Here x and y are the parameter thrown by mouseDragged() function 
blur1 = displayImage; // blur1 is Image variable 

blur2 = new BufferedImage(blur1.getWidth(this), blur1 //blur2 is BufferedImage Variable 
    .getHeight(this), BufferedImage.TYPE_INT_RGB); 
tst = blur2.createGraphics(); // tst is Graphics2D variable 
tst.drawImage(blur1, 0, 0, this); 
blur3 = new BufferedImage(blur1.getWidth(this), blur1 //blur3 is BufferedImage Variable 
    .getHeight(this), BufferedImage.TYPE_INT_RGB); 
float data[] = { 0.0625f, 0.125f, 0.0625f, 0.125f, 0.25f, 0.125f, 
    0.0625f, 0.125f, 0.0625f }; 
Kernel kernel = new Kernel(3, 3, data); 
ConvolveOp convolve = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, 
    null); 
blur2 = OSC.getSubimage(x, y, 20, 20); // 20 is the size in pixels where the effect is applied 
blur3 = OSC.getSubimage(x, y, 20, 20); 
convolve.filter(blur2, blur3);  
    Graphics osg = OSC.getGraphics(); 
    osg.setColor(fillColor); 
    osg.drawImage(blur3,x,y,null); 
    osg.dispose(); 
    repaint(); 
} 
0

看這裏: http://www.java2s.com/Code/Java/2D-Graphics-GUI/ImageEffectSharpenblur.htm

您需要定義一個數組你想要的任何值內核,實例化用的ConvolveOp的籽粒作爲參數,然後篩選所需的圖像。

+0

謝謝,但這隻會模糊整個圖像。 – ppst99

+0

我自己並沒有實現這個功能,但如果你可以模糊整個圖像,你應該能夠模糊圖像的一部分。程序員必須思考,你知道... – Jimmt