我制作了一个旋转图像的方法(在stackoverflow的很好的答案中找到了大多数代码)。现在我需要剪切图像:(1)通过轮廓和(2)内联图像(没有显示边框)。但我不知道该怎么做,我在这里找不到所有答案的样本。是否有可能以某种方式从旋转的图像中获取坐标(所有四个边缘点)?谢谢你的帮助。
public static BufferedImage rotateTest(BufferedImage image, double degrees) {
// --- get image size
int w = image.getWidth();
int h = image.getHeight();
// --- need longest side
int m = w;
if (h > w) {
m = h;
}
// --- double length for rotating
m = m * 2;
// --- rotate component
BufferedImage result = new BufferedImage(m, m, image.getType());
Graphics2D g = (Graphics2D) result.getGraphics();
// --- create transform
AffineTransform transformer = new AffineTransform();
// --- translate it to the center of the component
transformer.translate(result.getWidth() / 2, result.getHeight() / 2);
// --- do the actual rotation
transformer.rotate(Math.toRadians(degrees));
// --- translate the object so that you rotate it around the center
transformer.translate(-image.getWidth() / 2, -image.getHeight() / 2);
// --- anti aliasing
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.setRenderingHint(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
// --- drawing
g.drawImage(image, transformer, null);
// --- done
g.dispose();
// --- cut to outline
// --- run out
return result;
}