开发者

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, 
                           开发者_如何学Python               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:

Combining Images in java

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();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜