1

我做了一个旋转图像的方法(大多数代码都在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;
}
4

0 回答 0