在Java中裁剪图像 [英] Cropping an image in Java

查看:105
本文介绍了在Java中裁剪图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用Java剪切图像的某个部分并将其保存回磁盘。是否有一个函数可以从指定宽度和高度的X,Y切割图像?

I'm trying to cut a certain part of image in Java and save it back to disk. Is there a function that cuts the images from X, Y with the specified width and height?

推荐答案

你通常


  1. 创建一个新的 BufferedImage dst

  2. 抓住它的图形对象

  3. 加载原始.jpeg图像( src 下面)

  4. 将所需部分绘制到 BufferedImage

  5. 使用 ImageIO 将缓冲的图像写入文件。

  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.

代码:

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:

这篇关于在Java中裁剪图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆