0

I want to rotate a BufferedImage by an angle in radians. I used the following code. matrixImage is a matrix of integers where foreground pixels have 1 as value while background pixels have 0 as value. The new BufferedImage is correctly rotated but the extra borders are black. The new image is bigger than the original one and the new parts are black. I want that all the background pixels of the new image are white. I tried the solution proposed at Rotate BufferedImage and remove black bound, but I noticed that during the rotation the image changes.

bufferedImage = matrix2BufferedImage(matrixImage);
AffineTransform transform = new AffineTransform();
transform.rotate(radians, bufferedImage.getWidth() / 2, bufferedImage.getHeight() / 2);
AffineTransformOp op = new AffineTransformOp(transform, AffineTransformOp.TYPE_BILINEAR);
bufferedImage = op.filter(bufferedImage, null);
Ilya Lysenko
  • 1,772
  • 15
  • 24
Antonio
  • 29
  • 5
  • 1
    Does this answer your question? [Rotate BufferedImage with transparent background](https://stackoverflow.com/questions/48602703/rotate-bufferedimage-with-transparent-background) – Ilya Lysenko Mar 12 '20 at 22:36
  • Maybe this solution also will help: https://stackoverflow.com/questions/5925616/java-rotated-image-turns-all-black – Ilya Lysenko Mar 12 '20 at 22:39

1 Answers1

0

I solved using the following code for rotating

    private BufferedImage rotateImage(BufferedImage sourceImage, double angle) {
    AffineTransform transform = new AffineTransform();
    transform.rotate(angle, sourceImage.getWidth() / 2, sourceImage.getHeight() / 2);
    AffineTransformOp op = new AffineTransformOp(transform, AffineTransformOp.TYPE_BILINEAR);
    BufferedImage destImage = op.filter(sourceImage, null);

    Graphics2D g2d = destImage.createGraphics();

    g2d.drawRenderedImage(sourceImage, transform);

    g2d.dispose();
    return destImage;
}

Then, I binarised the buffered image with the following code

            int value=binarized.getRGB(x,y);
            if(value==0)
                value=-1;
            output[y][x] = ((0xFFFFFF & value) == 0xFFFFFF) ? (byte) 0 : 1;

Thanks for suggesting me the right post!

Antonio
  • 29
  • 5