Joining two images in java - an orange background is added

两盒软妹~` 提交于 2019-12-11 19:39:16

问题


This question is similar to…

Java BufferedImage saves with unwanted background color

…but regards .jpg and not .png.

I join two images together using the following Java code, and an orange background is mysteriously added. How can I get rid of it?

Note that I am trying to cover my bases using both setColor and setBackground, and both clearRect and fillRect.

Note that the source of the two images is a PDF, extracted using PDFBox

public static BufferedImage joinBufferedImage(BufferedImage img1, BufferedImage img2) {
    int offset = 0;
    int width = Math.max(img1.getWidth() , img2.getWidth()) + offset;
    int height = img1.getHeight() + img2.getHeight() + offset;

    BufferedImage newImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2 = newImage.createGraphics();

    g2.setColor(Color.WHITE);
    g2.setBackground(Color.WHITE);
    g2.clearRect(0, 0, width, height);
    g2.fillRect(0, 0, width, height);

    g2.drawImage(img1, null, 0, 0);
    g2.drawImage(img2, null, 0, img1.getHeight() + offset);
    g2.dispose();

    return newImage;
}
BufferedImage joined = joinBufferedImage(bim, bim2);
ImageIO.write(joined, "jpg", imageFile);    

来源:https://stackoverflow.com/questions/50159394/joining-two-images-in-java-an-orange-background-is-added

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!