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();

	}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值