Cropping an image in Java [closed]

℡╲_俬逩灬. 提交于 2019-12-04 12:47:41

You'd typically

  1. Create a new BufferedImage (dst below) with the desired width and height.
  2. Get hold of it's Graphics object
  3. Load the original .jpeg image (src below)
  4. Paint the desired part of that, onto the BufferedImage
  5. 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"));

Given this .jpg...

...It generates this .png:

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!