在java中组合图像

发布于 2024-12-06 20:47:16 字数 1425 浏览 0 评论 0原文

这是我的代码:

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

我的问题是代码将输出此图像:

在此处输入图像描述

我需要的是白色背景对于图像。谢谢!

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:

enter image description here

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

像极了他 2024-12-13 20:47:16

这对我来说看起来很危险:

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

在我看来,您可能正在创建两个非常不同的 Graphics 对象,一个是 Graphics2D 对象,另一个是 Graphics 对象。当您在 Graphics2D 对象中设置背景颜色时,您会清除 Graphics 对象中的矩形,因此它可以解释为什么您的背景不是白色的。为什么不直接创建一个 Graphics2D 对象并将其用于所有用途:

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

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);
累赘 2024-12-13 20:47:16

添加图像之前,绘制一个图像大小的白色矩形:

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

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文