When you resize your image, the new image that writing out has no EXIF data included with it, so it is displayed raw, which makes it look like it's on its side.
I have a sample code to show you how to keep EXIF data:
public class ImageData {
public byte[] resize(int maxDimension, File imageFile) throws IOException {
DataInputStream inStream = null;
try {
inStream = new DataInputStream(
new BufferedInputStream(
new FileInputStream(imageFile)));
byte[] imageData = IOUtils.toByteArray(inStream);
BufferedImage image = readImage(imageData);
TiffImageMetadata metadata = readExifMetadata(imageData);
image = Scalr.resize(image, maxDimension);
if (metadata != null) {
imageData = writeExifMetadata(metadata, writeJPEG(image));
} else {
imageData = writePNG(image);
}
return imageData;
} catch (IOException | ImageReadException | ImageWriteException e) {
log.error("image resize failed", e);
return null;
} finally {
assert inStream != null;
inStream.close();
}
}
private TiffImageMetadata readExifMetadata(byte[] jpegData) throws ImageReadException, IOException {
ImageMetadata imageMetadata = Imaging.getMetadata(jpegData);
if (imageMetadata == null) {
return null;
}
JpegImageMetadata jpegMetadata = (JpegImageMetadata)imageMetadata;
return jpegMetadata.getExif();
}
private byte[] writeExifMetadata(TiffImageMetadata metadata, byte[] jpegData)
throws ImageReadException, ImageWriteException, IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
new ExifRewriter().updateExifMetadataLossless(jpegData, out, metadata.getOutputSet());
out.close();
return out.toByteArray();
}
private BufferedImage readImage(byte[] data) throws IOException {
return ImageIO.read(new ByteArrayInputStream(data));
}
private byte[] writeJPEG(BufferedImage image) throws IOException {
ByteArrayOutputStream jpegOut = new ByteArrayOutputStream();
ImageIO.write(image, "JPEG", jpegOut);
jpegOut.close();
return jpegOut.toByteArray();
}
/**
*
* @param image
* @return byte[]
* @throws IOException
* This method calls when the metadata of a JPEG image is null,
* it will convert the JPEG image to PNG
*/
private byte[] writePNG(BufferedImage image) throws IOException {
ByteArrayOutputStream pngOut = new ByteArrayOutputStream();
ImageIO.write(image,"PNG", pngOut);
pngOut.close();
return pngOut.toByteArray();
}
}
and you can use this class like this:
public String convertImageToThumbnail(String path) {
try {
File imageFile = new File(path);
byte[] imageBytes;
String ex = path.substring(path.lastIndexOf(".")+1);
ImageData imageData = new ImageData();
imageBytes = imageData.resize(50, imageFile);
String output = Base64.getEncoder().encodeToString(imageBytes);
return "data:image/"+ex+";base64," + output;
} catch (Exception e) {
log.error("Converting to thumbnail failed", e);
return null;
}
}
you would resize and keep the EXIF data by using this sample.
hope it'll be useful...