Image src = ImageIO.read(new File("duke.jpg"));
int x = 10, y = 20, w = 40, h = 50;
BufferedImage dst = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
dst.getGraphics().drawImage(src, 0, 0, w, h, x, y, x + w, y + h, null);
ImageIO.write(dst, "png", new File("duke_cropped.png"));
鉴于此 .jpg...
.. .它生成这个.png:
You'd typically
Create a new BufferedImage (dst below) with the desired width and height.
Get hold of it's Graphics object
Load the original .jpeg image (src below)
Paint the desired part of that, onto the BufferedImage
Write the buffered image out to file using ImageIO.
In code:
Image src = ImageIO.read(new File("duke.jpg"));
int x = 10, y = 20, w = 40, h = 50;
BufferedImage dst = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
dst.getGraphics().drawImage(src, 0, 0, w, h, x, y, x + w, y + h, null);
ImageIO.write(dst, "png", new File("duke_cropped.png"));
发布评论
评论(1)
您通常会
BufferedImage
(下面的dst
)。Graphics
对象src
)BufferedImage
上ImageIO
将图像输出到文件。在代码中:
鉴于此 .jpg...
.. .它生成这个.png:
You'd typically
BufferedImage
(dst
below) with the desired width and height.Graphics
objectsrc
below)BufferedImage
ImageIO
.In code:
Given this .jpg...
...It generates this .png: