Putting multiple images together into one
<p>I have bunch of photos sorted in a folder so that there is always one photo of type <code>A</code> and one photo of type <code>B</code> immediately after that.</p>

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

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

<p><strong>Here is the code :</strong> </p>

<pre><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));
</code></pre>

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

<p><strong>How can I detect that flag and eventually rotate the photo as needed before combining? Thanks!</strong></p>