public static void main(String[] args) throws Exception {
String bus = "D:/1/商家.jpg";
String qr = "D:/1/二维码.png";
String bg = "D:/1/背景图片.png";
String context = "2021xxxxxxxx";
String name = "xxxxxxxxx";
String no = "9527";
File bgImgFile = new File(bg);
File qrFile = new File(qr);
File busFile = new File(bus);
Image qri = ImageIO.read(qrFile);
Image busi = ImageIO.read(busFile );
BufferedImage backgroundImage = ImageIO.read(bgImgFile);
int bgWidth = backgroundImage.getWidth();
System.out.println("bgWidth:" + bgWidth);
Graphics2D rng = backgroundImage.createGraphics();
//画图
rng.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 1.0f));
rng.drawImage(busi , 200, 250, 400, 400, null);
rng.drawImage(qri, 500, 900, 200, 200, null);
Color textColor = Color.white;
rng.setColor(textColor);
rng.drawImage(backgroundImage, 0, 0, null);
rng.setFont(new Font("黑体,Arial", Font.BOLD, 30));
rng.setColor(Color.black);
int strWidth = rng.getFontMetrics().stringWidth(context);
int nameWidth = rng.getFontMetrics().stringWidth(name);
int noWidth = rng.getFontMetrics().stringWidth(no);
System.out.println("context:" + strWidth);
int s = (bgWidth - strWidth) / 2;
//写字
int b = (bgWidth - nameWidth) / 2;
int c = (bgWidth - noWidth) / 2;
rng.drawString(context, s, 180);
rng.drawString(name, b, 700);
rng.drawString(no, c, 810);
rng.dispose();
FileOutputStream outImgStream = new FileOutputStream("D:/cc.png");
ImageIO.write(backgroundImage, "png", outImgStream);
outImgStream.flush();
outImgStream.close();
}
java Graphics2D 制作广告图片
最新推荐文章于 2022-08-30 23:10:08 发布