3

I have bunch of photos sorted in a folder so that there is always one photo of type A and one photo of type B immediately after that.

Unfortunately, some of them needed to be rotated and I did so using standard Windows file explorer.

What I need is to combine each pair of photos A and B into new single photo so that the first source photo is displayed above the second. Both photos have same width.

Here is the code :

    File first = ...;
    File second = ...;

    BufferedImage A = ImageIO.read(first);
    BufferedImage B = ImageIO.read(second);

    int resultHeight = A.getHeight() + B.getHeight();
    int resultWidth = A.getWidth();
    BufferedImage combined = new BufferedImage(resultWidth, resultHeight, BufferedImage.TYPE_INT_ARGB);


    Graphics g = combined.getGraphics();
    g.drawImage(A, 0, 0, null);
    g.drawImage(B, 0, A.getHeight(), null);
    g.dispose();

    ImageIO.write(combined, "PNG", new File(destDirectory, destName));

Unfortunately, although all pictures seem to have same orientation when viewing with the Windows app, they are rotated differently in the result photos. I know there is some kind of flag that seems to be ignored by the BufferedImage.

How can I detect that flag and eventually rotate the photo as needed before combining? Thanks!

1 Answers1

2

I suggest to use the MetadataExtractor to get the orientation of the images. See this question.

Community
  • 1
  • 1
JHead
  • 356
  • 2
  • 12
  • If you extract the rotation you can use [this answer](http://stackoverflow.com/questions/9749121/java-image-rotation-with-affinetransform-outputs-black-image-but-works-well-whe) to rotate the image. – fhofmann May 23 '16 at 13:19
  • Thanks! I used that library and it seems promising. – Matěj Kripner May 24 '16 at 14:03