-1

when the image is loaded from localdrive by default my image rotates to Portrait from Landscape. My image type is ".jpg" and size is 1.32MB. To verify the issue I have just write the image to output directory after immediate read. I suspect this is happening due to filesize of input image ? Because when the file size is small, orientation is not changing. I need to keep the image in Landscape in order to split the image into 2 parts.

    InputStream inputStream= new FileInputStream("d:/dev/test/xxxxxx.jpg");
    BufferedImage image = ImageIO.read(inputStream);    
    File outputfile = new File("image.jpg");
    ImageIO.write(image, "jpg", outputfile);        

Please suggest me a better solution and why BufferedImage converts the orientation from Lnadscape to Portrait? If the reading of image as it is with orientation is not possible, how to transfer the image back to Landscape?

For work around I have resized the input image and retried the orientation is not changed. Thank again.

aarav
  • 192
  • 1
  • 3
  • 17
  • ImageIO does not change the orientation - so get a grip on it. – gpasch May 12 '22 at 20:50
  • This seems to happen all the time actually ([example](https://stackoverflow.com/questions/9453367/is-javax-imageio-imageio-broken-it-imports-some-images-as-rotated), [example](https://stackoverflow.com/questions/15978809/imageio-read-always-rotates-my-uploaded-picture), [example](https://stackoverflow.com/questions/67717883/image-loaded-upside-down-in-swing-while-in-image-editor-looks-perfect)). Seems like ImageIO does not take into account the orientation metadata of JPEGs. – gthanop May 13 '22 at 07:18
  • The JDK `JPEGImageReader` does not take EXIF Orientation tag into account. It just reads the JPEG data as it is stored in the stream. It has nothing to do with file size (but probably the smaller JPEGs you have does not contain EXIF Orientation). My [TwelveMonkeys ImageIO](https://github.com/haraldk/TwelveMonkeys/) library contains an `EXIFUtilities` class. It has `readWithOrientation` methods that solves this for you. – Harald K May 13 '22 at 07:50
  • @HaraldK I have added TwelveMonekys Imago unable to locate `EXIFUtilities` could you please share the package name ? Also In a longrun can I use this library ? – aarav May 13 '22 at 15:03
  • It's in the contrib module for now, `com.twelvemonkeys.contrib.exif.EXIFUtilities`. Sorry, didn't get the last question... What do you mean by *"In a longrun"*? – Harald K May 13 '22 at 15:12
  • I need to again convert IIOImage to BufferedImage in order to cut the image into pieces and sending piece of image (divide by row of 2) to a OCR for reading image text. Is there any util method available to convert IIOImage to BufferedImage ? Or I need to crop the IIOImage as well as for _tesseract. – aarav May 13 '22 at 15:48
  • No "conversion" needed. Just `getRenderedImage()` and cast to `BufferedImage`. Always safe for this call. – Harald K May 13 '22 at 16:42

0 Answers0