本文主要是介绍java Graphics2D 制作广告图片,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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 制作广告图片的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!