Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Putting multiple images together into one

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!

like image 892
Matěj Kripner Avatar asked Dec 18 '25 05:12

Matěj Kripner


1 Answers

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

like image 146
JHead Avatar answered Dec 20 '25 17:12

JHead



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!