3

I want to resize image without rotate image This my code.

Image temp = image.getScaledInstance(width, height,ImageScale.SCALE_SMOOTH);
BufferedImage resized = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = resized.createGraphics();
g2d.drawImage(temp, 0, 0, width, height, null);
g2d.dispose();
return resized;
iamp.
  • 51
  • 3
  • 3
    I actually have no problems with rotation with your code. The image is resized without rotation (I then just write it to the output file). Could you please post the whole class/code? it looks that something is wrong after the given code, i.e. when you handle the resized image. – Dmitriy Popov Jun 27 '19 at 21:13
  • 1
    I had a similar issue once, it was really mind boggling. What it turned out to be was Exif meta data added by a phone which specified a rotation. So when viewing the image in a browser or on desktop it looked normal. But in Java it didnt see this meta data and was actually upside down... Still need code but might be a consideration. https://en.wikipedia.org/wiki/Exif#Example – ug_ Jun 27 '19 at 22:11

1 Answers1

2

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...

Issa Khodadadi
  • 917
  • 7
  • 19