I am trying to rotate images using Thumbnailator library. The code that I use is as shown below. It rotates the image or flips the image successfully but the color quality completely spoils. The input and output images are also shown.
package com.abk;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.filters.Flip;
import net.coobird.thumbnailator.filters.Rotation;
import net.coobird.thumbnailator.util.exif.ExifUtils;
import net.coobird.thumbnailator.util.exif.Orientation;
public class ImageAutoRotate {
public static void main(String[] args) {
try {
BufferedImage img = ImageIO.read(new File("314.jpg"));
BufferedImage newImg = Rotation.RIGHT_90_DEGREES.apply(img);
BufferedImage flipImg = Flip.HORIZONTAL.apply(img);
File outputfile = new File("314_2.jpg");
ImageIO.write(newImg, "jpg", outputfile);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Resolved the issue by saving the image as a PNG image. The issue was actually caused because the image profile was being treated as CMYK while saving
The final code to resolve this issue is shown at this link