Java生成海报

package tech.yooo.ratel.member.controller.saas;


import lombok.extern.slf4j.Slf4j;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.*;

import javax.imageio.ImageIO;

@Slf4j
public class DemoPic {

  public static void main(String[] args) throws IOException {
    test();

  }

  public static void test() throws IOException {
      String pngString = "D:\\down\\goods\\sku2\\78567856.jpg";
      String goodsPath = "D:\\down\\goods\\sku1\\354634563.jpg";
      String prefix = "D:\\down\\goods\\1577169685.png";

      String savePath = "D:\\down";
      String imageName = System.currentTimeMillis() + ".png";

      BufferedImage bg = new BufferedImage(658, 1986, BufferedImage.TYPE_INT_RGB);
      Graphics2D g = bg.createGraphics();
      g.setBackground(Color.WHITE);
//      g.fillRect();
      g.setColor(Color.WHITE);
//      g.fillRect(0, 0, 750, 1250);// 填充整个屏幕
      BufferedImage platImage = ImageIO.read(new File(pngString));
      int y = 0;

      g.drawImage(platImage.getScaledInstance(658, 986, Image.SCALE_SMOOTH), 0, y, 658, 986, null);
      g.drawString("到手价" , 50, 50);
      y+=986;

      Rectangle rectangle = new Rectangle(0, 986, 658, 500);
      BufferedImage goodsImg = ImageIO.read(new File(goodsPath));
      g.drawImage(goodsImg.getScaledInstance(rectangle.width, rectangle.height, Image.SCALE_SMOOTH), rectangle.x, rectangle.y, null);
      y+=500;


      Double rebatePrice = 3.73;
      String info = "下单返" + rebatePrice.toString();
      g.setFont(new Font("Microsoft YaHei", Font.BOLD, 30));
      g.setColor(new Color(255, 255, 255));
      g.drawString(info , 110, y+25);

      g.setFont(new Font("Microsoft YaHei", Font.PLAIN, 28));
      g.setColor(new Color(255, 255, 255));
      g.drawString("超值好物,买就返现" , 90, y+65);

      BufferedImage qrCode = ImageIO.read(new File(prefix));
      g.drawImage(qrCode, 510, y+5, 230, 230, null);

      // 写入二维码
      g.setFont(new Font("Microsoft YaHei", Font.PLAIN, 22));
      g.setColor(Color.gray);
      g.drawString("长按识别二维码" , 550, y+260);
      y+=25;

      // 到手价背景
      g.drawRect(15, y +10,90,40);
      g.setColor(new Color(255, 0, 61));
      g.fillRect(15, y +50,90,40);

      // 到手价
      g.setFont(new Font("Microsoft YaHei", Font.BOLD, 25));
      g.setColor(Color.white);
      g.drawString("到手价" , 23, y+88);

      // 到手价人民币图标
      g.setFont(new Font("Microsoft YaHei", Font.PLAIN, 36));
      g.setColor(Color.red);
      g.drawString("¥" , 125, y+95);

      // 到手价金额
      g.setFont(new Font("Microsoft YaHei", Font.BOLD, 60));
      g.setColor(Color.red);
      g.drawString(String.valueOf(561.27) , 145, y+45);

      // 原价
      g.setFont(new Font("Microsoft YaHei", Font.PLAIN, 30));
      g.setColor(new Color(89, 89, 89));
      g.drawString("原价: " , 350, y+45);

      // 原价金额
      g.setFont(new Font("Microsoft YaHei", Font.PLAIN, 30));
      g.setColor(new Color(89, 89, 89));
      g.drawString("¥" + 565 , 415, y+45);
      y+=110;

      g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);						// 消除画图锯齿
      g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);	// 消除文字锯齿

      g.dispose();
      ImageIO.write(bg, "jpg", new File(savePath + File.separator + imageName));
  }

}

你可能感兴趣的:(Java生成海报)