Given a javafx.scene.image.Image object rotate it and produce a rotated javafx.scene.image.Image object

StackOverflow https://stackoverflow.com/questions/22612985

Question

I have code that create an Image: (m_img is javafx.scene.image.Image)

Image m_img = new Image("file:" + p_Fil.getAbsoluteFile(),false);

This is fine, but it does ignore the exif TAG_ORIENTATION so pictures taken on a phone in portrait mode do not appear the correct way up.

When I Change the load method, I am able to read this tag and save the result into an int (1-8) as follows:

byte bb[] = FileUtilities.readFile(p_Fil);
getOrientation(new ByteArrayInputStream(bb),p_Fil);
m_img = new Image(new ByteArrayInputStream(bb));
if (m_orientation==1) return; //1 means no transformation required

If m_orientation is 1 then it is correct so no further action is required.

But now I need to somehow transform the image depending on the number returned. (Rotation, or Flip etc.)

I think the javafx.scene.transform.Rotate class should help me. Can anyone provide sample code where given an image object you can output another rotated image object.

Was it helpful?

Solution

You will have to rely on the AWT for this:

I recommend having this method:

public BufferedImage getRotatedImage(BufferedImage bufferedImage, int angle) {
     AffineTransform transform = new AffineTransform();
     transform.rotate(angle);
     AffineTransformOp op = new AffineTransformOp(transform, AffineTransformOp.TYPE_BILINEAR);
     bufferedImage = op.filter(bufferedImage, null);
     return bufferedImage;
}

Then:

BufferedImage buffImg= SwingFXUtils.fromFXImage(m_img, null);
buffImg = getRotatedImage(buffImage, 90); //or other angle if needed be
m_img = SwingFXUtils.toFXImage(buffImg, null);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top