Java Graphics 2D生成比赛牌

import org.springframework.core.io.ClassPathResource;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class GameCardUtil {

    public static BufferedImage generatorCard(Registration registration, InputStream photoInputStream, InputStream qrCodeInputStream) throws IOException {

        //获取背景图片
        BufferedImage backgroundImage = ImageIO.read(new ClassPathResource("/static/img/gameCard.jpg").getInputStream());

        //获取参赛牌头像
        BufferedImage registerPhoto = ImageIO.read(photoInputStream);

        //二维码
        BufferedImage qrCode = ImageIO.read(qrCodeInputStream);

        //重新定义参赛牌头像大小
        registerPhoto = restImageSize(GameCardConstant.PHOTO_WIDTH, GameCardConstant.PHOTO_HEIGHT, registerPhoto);

        //获取画布 这里用2D
        Graphics2D g = backgroundImage.createGraphics();

        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);

        g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 1.0F));

        g.drawImage(registerPhoto, GameCardConstant.PHOTO_X, GameCardConstant.PHOTO_Y, registerPhoto.getWidth(), registerPhoto.getHeight(), null);

        //中心点位置
        int centerX = backgroundImage.getWidth() / 2;

        Font nameFont = new Font("思源黑体", Font.BOLD, 48);
        graphicsString(g, registration.getApplicant().getName(), nameFont, new Color(238, 151, 11), centerX, GameCardConstant.NAME_Y);

        Font contestFont = new Font("思源黑体", Font.BOLD, 32);
        graphicsString(g, registration.getContest().getContestType(), contestFont, Color.black, centerX, GameCardConstant.CONTEST_Y);
        graphicsString(g, registration.getApplicant().getPhoneNo(), contestFont, Color.black, centerX, GameCardConstant.PHONE_Y);

        Font bottomFont = new Font("思源黑体", Font.PLAIN, 24);

        String groupName = registration.getSysGroup().getName();
        String teamName = registration.getContestTeam().getName();

        graphicsString(g, groupName, bottomFont, Color.BLACK, GameCardConstant.GROUP_WIDTH, GameCardConstant.GROUP_HEIGHT, GameCardConstant.GROUP_WIDTH2, GameCardConstant.GROUP_HEIGHT2, GameCardConstant.MAX_WIDTH);
        graphicsString(g, teamName, bottomFont, Color.BLACK, GameCardConstant.TEAM_WIDTH, GameCardConstant.TEAM_HEIGHT, GameCardConstant.TEAM_WIDTH2, GameCardConstant.TEAM_HEIGHT2, GameCardConstant.MAX_WIDTH);

        //重新定义二维码大小
        qrCode = restImageSize(GameCardConstant.QR_CODE_WIDTH, GameCardConstant.QR_CODE_HEIGHT, qrCode);
        g.drawImage(qrCode, GameCardConstant.QR_CODE_X, GameCardConstant.QR_CODE_Y, qrCode.getWidth(), qrCode.getHeight(), null);

        return backgroundImage;
    }


    private static BufferedImage restImageSize(int width, int height, BufferedImage image) {

        BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        bufferedImage.getGraphics().drawImage(image.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null);

        return bufferedImage;
    }

    private static void graphicsString(Graphics2D g, String str, Font font, Color color, int centerX, int height) {

        g.setFont(font);
        FontMetrics nameFm = g.getFontMetrics(font);
        int nameWidth = nameFm.stringWidth(str);
        g.setColor(color);
        g.drawString(str, centerX - (nameWidth / 2), height);
    }

    private static void graphicsString(Graphics2D g, String str, Font font, Color color, int width, int height, int width2, int height2, int maxWidth) {

        g.setFont(font);
        FontMetrics nameFm = g.getFontMetrics(font);
        int nameWidth = nameFm.stringWidth(str);
        g.setColor(color);

        if (nameWidth > maxWidth) {

            String sub_1 = str.substring(0, 15);
            String sub_2 = str.substring(15);

            g.drawString(sub_1, width, height);
            g.drawString(sub_2, width2, height2);
            return;
        }

        g.drawString(str, width, height);
    }

    public static InputStream parseInputStream(final OutputStream out) {

        ByteArrayOutputStream outputStream = (ByteArrayOutputStream) out;

        return new ByteArrayInputStream(outputStream.toByteArray());
    }

    private static class GameCardConstant {

        public static final int PHOTO_WIDTH = 231;
        public static final int PHOTO_HEIGHT = 321;

        public static final int PHOTO_X = 298;
        public static final int PHOTO_Y = 249;

        public static final int NAME_Y = 630;
        public static final int CONTEST_Y = 790;
        public static final int PHONE_Y = 900;

        public static final int GROUP_WIDTH = 220;
        public static final int GROUP_HEIGHT = 1005;
        public static final int GROUP_WIDTH2 = 220;
        public static final int GROUP_HEIGHT2 = 1045;

        public static final int TEAM_WIDTH = 220;
        public static final int TEAM_HEIGHT = 1089;
        public static final int TEAM_WIDTH2 = 220;
        public static final int TEAM_HEIGHT2 = 1123;

        public static final int MAX_WIDTH = 360;

        public static final int QR_CODE_WIDTH = 169;
        public static final int QR_CODE_HEIGHT = 169;
        public static final int QR_CODE_X = 600;
        public static final int QR_CODE_Y = 971;

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值