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

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

Domanda

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.

È stato utile?

Soluzione

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);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top