我想在BufferedImage上应用一些过滤器,但不要在整个
上应用这些过滤器的bufferedImage,我需要在矩形,椭圆,徒手选择
上应用滤镜BufferedImage.anybody有想法吗?
谢谢
答案 0 :(得分:4)
请参阅Graphics.setClip(Shape形状):
Graphics g = image.getGraphics();
g.setClip(shape);
然后,您可以在整个图形(图像)上应用滤镜,但它只会应用于剪裁区域。
以下代码将生成此图片:
public static void main(String[] args) throws Exception {
BufferedImage image = new BufferedImage(400, 400, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = (Graphics2D) image.getGraphics();
// set "user defined" clip
g.setClip(new Polygon(
new int[] { 50, 100, 50 },
new int[] { 50, 50, 100 },
3));
g.fillRect(0, 0, 400, 400);
// set an ellipse
g.setClip(new Ellipse2D.Double(100, 100, 200, 200));
g.fillRect(0, 0, 400, 400);
// set an rectangle
g.setClip(new Rectangle(300, 300, 50, 50));
g.fillRect(0, 0, 400, 400);
g.dispose();
ImageIO.write(image, "png", new File("test.png"));
}