我正在尝试裁剪一个500x500的图像,使其中心只有300x300的矩形,如下所示:
Original Image
+-------------------+ +-------------------+
| 500 x 500 | | Crop Area |
| | | +-----------+ |
| | | | 300 x 300 | |
| | | | | |
| | | | | |
| | | +-----------+ |
| | | |
+-------------------+ +-------------------+
我看到Graphics.drawImage()
带有8个int
参数的参数表示它将绘制图像的某个区域,这似乎只适合绘制图像的裁剪区域,但是当我尝试使用image.getGraphics().drawImage(image, 0, 0, 500, 500, 100, 100, 400, 400, null);
它时,并不能正确裁剪图像。
我应该给什么参数drawImage
来裁剪图像?
前四个int参数表示要绘制到的图像(目标图像)的矩形部分,后四个表示要绘制的图像(源图像)的矩形部分。如果这些矩形的大小不同,则将对源图像进行缩放(缩放)以适合目标图像。您尝试进行的drawImage(image, 0, 0, 500, 500, 100, 100, 400, 400, null)
操作不太奏效,因为在获得正确的图像区域后,您将其增大以适合整个图像。因为要裁剪图像-更改其尺寸-必须创建一个新图像,该尺寸适合于裁剪区域,然后绘制到该图像上。
这是一个将裁剪后的图像存储在BufferedImage中的示例:
//enter the appropriate type of image for TYPE_FOO
BufferedImage cropped = new BufferedImage(300, 300, BufferedImage.TYPE_FOO);
cropped.getGraphics().drawImage(image,
0, 0, 300, 300, //draw onto the entire 300x300 destination image
100, 100, 400, 400, //draw the section of the image between (100, 100) and (400, 400)
null);
image = cropped;
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句