Image is in landscape instead of portrait mode after scaling

2.8k views Asked by At

I have an image and want to scale it down using imgscalr [1].
The source image's size is 1836 x 3264 (so it's portrait mode) and destination resolution is 1336 x 768.
The failure is that the image is in landscape mode and not portrait mode anymore after scaling. The scaling itself works like a charm.
My code:

BufferedImage src = ImageIO.read(new File("sourceimage.jpg"));
BufferedImage scaled = Scalr.resize(src, Scalr.Method.SPEED, Scalr.Mode.FIT_TO_HEIGHT, 1336, 768, Scalr.OP_ANTIALIAS);
ImageIO.write(scaled, "jpg", f);

The metadata of the file looks correct (orientation = 1).
My expactation is when downscaling an portrait picture that it is still a portrait picture after the scaling process.

I also tried some other libraries/classes like this: https://frickelblog.wordpress.com/2009/06/08/fast-image-scaling-in-java/ but the effect is the same.

Can you please help me? This one drives me crazy! I'm pretty sure there is just a detail to change.

[1] http://www.thebuzzmedia.com/software/imgscalr-java-image-scaling-library/#maven

edit:
Here's the original image: https://i.stack.imgur.com/NEc8j.jpg
the scaled version: https://i.stack.imgur.com/SYx6p.jpg

2

There are 2 answers

1
MadProgrammer On BEST ANSWER

So the problem isn't with ImgScar but with ImageIO, ImageIO won't read the orientation of the image, so it was been read in as 3264x1836 - Landscape.

So what I ended up doing was rotating the image 90 degrees...

        BufferedImage src = ImageIO.read(new File("/Users/swhitehead/Downloads/original.jpg"));

        System.out.println(src.getWidth() + "x" + src.getHeight());
        BufferedImage rotated = Scalr.rotate(src, Scalr.Rotation.CW_90, Scalr.OP_ANTIALIAS);
        BufferedImage scaled = Scalr.resize(rotated, Scalr.Method.SPEED, Scalr.Mode.FIT_TO_HEIGHT, 1336, 768, Scalr.OP_ANTIALIAS);
        System.out.println(scaled.getWidth() + "x" + scaled.getHeight());
        ImageIO.write(scaled, "jpg", new File("Scaled.jpg"));

Which seems to have fixed "this" problem.

You can have a look at ImageIO.read( ) always rotates my uploaded picture for more details

0
progonkpa On

Browsers implement <img> differently.
Firefox will not read the EXIF-information so portrait pictures are displayed in landscape.
Chrome does read out the EXIF-information so portait stays portrait.

I fixed it with this CSS declaration:
image-orientation: from-image;