如下所示,我在另一个 BufferedImage 上有一个 BufferedImage。它们都是png,我希望叠加图像上没有背景。我确信有办法做到这一点,但我不确定 api 在哪里。
这是有问题的方法:
private static BufferedImage finalizeImage(BufferedImage originalImage, String tokenImage, Integer occurrences, int height, int width, int type){
//Font font = new Font("Courier New", Font.PLAIN, 12);
BufferedImage resizedImage = new BufferedImage(width, height, type);
Graphics2D g = resizedImage.createGraphics();
FontMetrics fm = g.getFontMetrics();
int strWidth = (fm.stringWidth(tokenImage));
int imageWidth = resizedImage.getWidth();
int textBegin = (imageWidth - strWidth) / 2;
//g.setFont(font);
g.drawImage(originalImage, 0, 0, width, height, null);
g.setColor(Color.black);
int textHeight = (fm.getAscent() + (TOKEN_HEIGHT - (fm.getAscent() + fm.getDescent())) / 2);
g.drawString(tokenImage, textBegin, textHeight);
//for multiple occurrences
try {
BufferedImage numOnSubscript = ImageIO.read(Thread.currentThread().getContextClassLoader().getResourceAsStream("images/ui/tokens/subscript.fw.png"));
g.drawImage(numOnSubscript, width - 20, height-20, 20, 20, null);
g.setColor(Color.white);
g.drawString(occurrences.toString(), width - 16, (height-20)*2 - 1);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
g.dispose();
return resizedImage;
}
这是正在发生的事情。