在java中组合图像
这是我的代码:
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:
what I need is to have white background for the image. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这对我来说看起来很危险:
在我看来,您可能正在创建两个非常不同的 Graphics 对象,一个是 Graphics2D 对象,另一个是 Graphics 对象。当您在 Graphics2D 对象中设置背景颜色时,您会清除 Graphics 对象中的矩形,因此它可以解释为什么您的背景不是白色的。为什么不直接创建一个 Graphics2D 对象并将其用于所有用途:
This looks risky to me:
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:
添加图像之前,绘制一个图像大小的白色矩形:
Before you add the images, draw a white rectangle the size of your image: