在Java中裁剪图像 [英] Cropping an image in Java
本文介绍了在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?
推荐答案
你通常
- 创建一个新的
BufferedImage
(dst $ c $具有所需宽度和高度的c>。
- 抓住它的
图形
对象 - 加载原始.jpeg图像(
src
下面) - 将所需部分绘制到
BufferedImage
- 使用
ImageIO
将缓冲的图像写入文件。
- 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
.
代码:
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屋!
查看全文