【问题标题】:How to blur an image under the mouse如何在鼠标下模糊图像
【发布时间】:2013-01-08 12:29:25
【问题描述】:

我正在使用 java,并且正在生成图像。当鼠标经过生成的图像时,我需要对图像使用模糊或像素化滤镜。

我应该使用什么方法来完成这个?

【问题讨论】:

  • 我刚刚使用 java.awt.image.ConvolveOp 模糊了整个图像。
  • “你试过什么?”,换句话说,你能把你试过的代码贴在这里吗?
  • 1) 为了尽快获得更好的帮助,请发帖 SSCCE。 2) 解决办法大概是画出清晰的图像,然后在鼠标下方的区域应用一个剪辑,然后画出模糊的图像。

标签: java image awt mouseevent java-2d


【解决方案1】:

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

您需要定义一个包含您想要的任何值的数组的内核,使用内核作为参数实例化 ConvolveOp,然后过滤所需的图像。

【讨论】:

  • 谢谢,但这只会模糊整个图像。
  • 我自己还没有实现这个,但是如果你可以模糊整个图像,你应该能够模糊图像的一部分。程序员必须思考,你知道...
【解决方案2】:
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();
}

【讨论】:

    猜你喜欢
    • 2016-06-15
    • 1970-01-01
    • 1970-01-01
    • 2015-06-10
    • 1970-01-01
    • 2010-12-08
    • 1970-01-01
    • 1970-01-01
    • 2021-08-11
    相关资源
    最近更新 更多