实现模糊和其他图像过滤效果非常简单.
例如,要对a执行模糊BufferedImage,可以使用以下ConvolveOp指定的卷积矩阵Kernel:
BufferedImageOp op = new ConvolveOp(new Kernel(3, 3,
new float[] {
1/9f, 1/9f, 1/9f,
1/9f, 1/9f, 1/9f,
1/9f, 1/9f, 1/9f
}
));
BufferedImage resultImg = op.filter(originalImg, resultImage);
Run Code Online (Sandbox Code Playgroud)
不太确定何时需要模糊效果,但它可能会在某个时候派上用场.但我认为它易于实施,这是一个悬而未决的成果.
这是卷积矩阵的一些信息.它可用于实现锐化,浮雕,边缘增强等效果.
执行像素化效果是一个简单的水果操作BufferedImage.
这可以分两步执行:
第1步:确定颜色:
public static Color determineColor(BufferedImage img, int x, int y, int w, int h) {
int cx = x + (int)(w / 2);
int cy = y + (int)(h / 2);
return new Color(img.getRGB(cx, cy), true);
}
Run Code Online (Sandbox Code Playgroud)
在该determineColor方法中,BufferedImage确定来自中心的像素颜色,并将其传递回调用者.
第2步:使用确定的颜色填充像素化块:
BufferedImage sourceImg = ...; // Source Image.
BufferedImage destimg = ...; // Destination Image.
Graphics g = destImg.createGraphics();
int blockSize = 8;
for (int i = 0; i < sourceImg.getWidth(); i += blockSize) {
for (int j = 0; j < sourceImg.getHeight(); j += blockSize) {
Color c = determineColor(sourceImg, i, j, blockSize, blockSize);
g.setColor(c);
g.fillRect(i, j, blockSize, blockSize);
}
}
g.dispose();
Run Code Online (Sandbox Code Playgroud)
虽然有相当多的代码,但这种影响在智力上是一个悬而未决的成果 - 没有太多复杂的事情发生.它基本上是找到一个块的中心颜色,并用这种颜色填充一个盒子.这是一个相当天真的实现,因此可能有更好的方法来实现它.
以下是执行上述像素化效果之前和之后的比较:
非像素化图像http://coobird.net/img/grad64.png 像素化图像http://coobird.net/img/grad64p.png