You'd typically
- Create a new
BufferedImage
with the desired width and height. - Get hold of it's
Graphics
object - Load the original .jpeg image
- Paint the desired part of that, onto the
BufferedImage
- Write the buffered image out to file using
ImageIO
.
In code:
Image orig = ImageIO.read(new File("duke.jpg"));
int x = 10, y = 20, w = 40, h = 50;
BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
bi.getGraphics().drawImage(orig, 0, 0, w, h, x, y, x + w, y + h, null);
ImageIO.write(bi, "png", new File("duke_cropped.png"));
Given this .jpg...
...It generates this .png:
No comments:
Post a Comment