-1

I want to copy a part of BufferedImage, but the copying form is not a simple square but rather a square rotated on some angle. As an output I want to get the BufferedImage with width and height equals to the copying square size and contents from the initial image, where copying square intersects the initial image.

What is the simplest way to do it?

Kivan
  • 1,712
  • 1
  • 14
  • 30
  • (I'm not the downvoter. I think that the question is legitimate. But it could be improved with some details. Particularly: ) How is the "rotated square" given? Is it given as Center+Angle+Size, or differently? – Marco13 Aug 06 '16 at 12:41
  • Doesn't matter how it's given, because all approaches are convertible. Consider as you like Center+Angle+Size. – Kivan Aug 06 '16 at 13:56

1 Answers1

-1

A simple approach would be to create a new image with the desired size, transform a Graphics2D of this image according to the selected square region, and then simply paint the original image into this graphics:

private static BufferedImage extractRegion(
    BufferedImage source, Point2D center, Point2D size, double angleRad)
{
    int w = (int)size.getX();
    int h = (int)size.getY();
    BufferedImage result = 
        new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = result.createGraphics();
    g.setRenderingHint(
        RenderingHints.KEY_INTERPOLATION, 
        RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    g.translate(size.getX() / 2, size.getY() / 2);
    g.rotate(angleRad);
    g.translate(-center.getX(), -center.getY());
    g.drawImage(source, 0, 0, null);
    g.dispose();
    return result;

}
Marco13
  • 53,703
  • 9
  • 80
  • 159