java制图问题求教
我们的要求只是很简单的在图上写字,以前一直都没问题
最近换了一个背景图,是白色透明边框的
这时候再运行以前的程序发现边框自动被替换为了黑色
代码如下
private File pressTextToImage(BannerPropertiesDto bannerPropertiesDto) throws IOException {
File img = new File(bannerPropertiesDto.getBackgroundImageFile());
Image src = ImageIO.read(img);
int width = src.getWidth(null);
int height = src.getHeight(null);
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = image.createGraphics();
g.drawImage(src, 0, 0, width, height, null);
g.setColor(bannerPropertiesDto.getFontColor());
g.setFont(new Font(bannerPropertiesDto.getFontStyle(), bannerPropertiesDto.getFontThickness(), bannerPropertiesDto.getFontSize()));
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, bannerPropertiesDto.getFontTransparency()));
g.drawString(bannerPropertiesDto.getPressText(), (width - (getLength(bannerPropertiesDto.getPressText()) * bannerPropertiesDto.getFontSize())) / 2 + bannerPropertiesDto.getCoordinatesX(),
(height - bannerPropertiesDto.getFontSize()) / 2 + bannerPropertiesDto.getCoordinatesY());
g.dispose();
File file = new File(bannerPropertiesDto.getBannerImageFile());
ImageIO.write((BufferedImage) image, "PNG", file);
return file;
}