如何从图像中剪切并保存矩形?

时间:2010-10-14 18:33:25

标签: java image-processing

如何剪切图像并将其保存到另一张图像?

1 个答案:

答案 0 :(得分:10)

如果srcBufferedImage,那么您可以从中剪切矩形(x1,y1)-(x2,y2)并将其写入dst.png,如下所示:

final BufferedImage dst = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);    

Graphics2D g = dst.createGraphics();
g.drawImage(src, x1, y1, x2, y2, null);
g.dispose();

ImageIO.write(dst, "PNG", new FileOutputStream("dst.png"));