我想模糊我图像的一部分,所以我使用Kernel和BufferedImageOp编写了一些代码。
private void blurPartially(BufferedImage ss, int x, int y, int w, int h, Graphics gz) {
BufferedImage bufferedImage = deepCopy(ss).getSubimage(x, y, w, h);
Kernel kernel = new Kernel(3, 3, new float[] { 0.111f, 0.111f, 0.111f,
0.111f, 0.111f, 0.111f, 0.111f, 0.111f, 0.111f });
BufferedImageOp op = new ConvolveOp(kernel);
gz.drawImage(op.filter(bufferedImage, null), x, y, w, h, null);
repaint();
}
private BufferedImage deepCopy(BufferedImage bi) {
ColorModel cm = bi.getColorModel();
boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
WritableRaster raster = bi.copyData(null);
return new BufferedImage(cm, raster, isAlphaPremultiplied, null);
}
但它在模糊区域周围留下了一些奇怪的矩形:
我无法找到导致此问题的任何合理代码。