如何在Java中裁剪调整大小的图像?

问题描述 投票:-2回答:1

当我将具有实际尺寸的图像设置到面板上并使用鼠标进行裁剪时,它工作正常,但是当我将图像调整到面板上并裁剪时,裁剪图像会出错。如何使用鼠标裁剪调整图像大小?

  int x = Math.min(p1.x, p2.x);
  int y = Math.min(p1.y, p2.y);
  int w = Math.abs(p1.x - p2.x);
  int h = Math.abs(p1.y - p2.y);
  BufferedImage dest = image.getSubimage(x,y,w,h)
java swing
1个回答
0
投票

如果要从原始图像裁剪,则需要计算两个图像的x / y比例因子。然后,您需要通过这些比例因子调整x / y /宽度/高度值。

因此,如果原始图像为400 x 100且调整后的图像为100 x 100,您可以执行以下操作:

double xScale = originalImageWidth / resizeImageWidth = 400 / 100 = 4.

所以现在如果调整大小的图像上的裁剪矩形是(10,10,20,30);

然后你需要计算你的价值,如:

int x = rectangle.x * xScale;
int width = rectangle.width * xScale;

image.getSubImage(x, y, width, height);

您显然需要使用y比例因子计算y / height值。

© www.soinside.com 2019 - 2024. All rights reserved.