简体   繁体   中英

Combining Images in java

Here's my code:

    Image partNumberImage = Toolkit.getDefaultToolkit().getImage("D:/partNumber.png");
    Image lotNumberImage = Toolkit.getDefaultToolkit().getImage("D:/lotNumber.png");
    Image dteImage = Toolkit.getDefaultToolkit().getImage("D:/dte.png");
    Image quantityImage = Toolkit.getDefaultToolkit().getImage("D:/quantity.png");

    BufferedImage combinedImage = new BufferedImage(486, 
                                          151, 
                                          BufferedImage.TYPE_INT_RGB);


    Graphics g = combinedImage.getGraphics();

    combinedImage.createGraphics().setBackground(Color.white);

    g.clearRect(0,0, 486, 151);
    g.drawImage(partNumberImage, x, 18, null);
    g.drawImage(lotNumberImage, x, 48, null);
    g.drawImage(dteImage, x, 58, null);
    g.drawImage(quantityImage, x, 68, null);


    g.dispose();

    Iterator writers = ImageIO.getImageWritersByFormatName("png");
    ImageWriter writer = (ImageWriter) writers.next();
    if (writer == null) {
        throw new RuntimeException("PNG not supported?!");
    }

     ImageOutputStream out = ImageIO.createImageOutputStream(
                                new File("D:/Combined.png" ));
    writer.setOutput(out);
    writer.write(combinedImage);
    out.close();
}

My problem is the code will output this image:

在此处输入图片说明

what I need is to have white background for the image. Thanks!

This looks risky to me:

Graphics g = combinedImage.getGraphics(); // Graphics object #1

combinedImage.createGraphics().setBackground(Color.white);  // Graphics object #2
// so now you've set the background color for the second Graphics object only

g.clearRect(0,0, 486, 151);  // but clear the rect in the first Graphics object
g.drawImage(partNumberImage, x, 18, null);
g.drawImage(lotNumberImage, x, 48, null);
g.drawImage(dteImage, x, 58, null);
g.drawImage(quantityImage, x, 68, null);

It appears to me that you may be creating two very distinct Graphics objects, one a Graphics2D object and one a Graphics object. And while you're setting the background color in the Graphics2D object, your clearing a rect in the Graphics object, so it could explain why your background is not white. Why not instead just create one Graphics2D object and use it for everything:

Graphics2D g = combinedImage.createGraphics(); 
g.setBackground(Color.white);

//  Now there is only one Graphics object, and its background has been set
g.clearRect(0,0, 486, 151);  // This now uses the correct background color
g.drawImage(partNumberImage, x, 18, null);
g.drawImage(lotNumberImage, x, 48, null);
g.drawImage(dteImage, x, 58, null);
g.drawImage(quantityImage, x, 68, null);

Before you add the images, draw a white rectangle the size of your image:

g.clearRect(0,0, 486, 151);
g.setColor(Color.white);
g.fillRect(0,0,486,151);

g.drawImage(partNumberImage, x, 18, null);
g.drawImage(lotNumberImage, x, 48, null);
g.drawImage(dteImage, x, 58, null);
g.drawImage(quantityImage, x, 68, null);


g.dispose();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM