zoukankan      html  css  js  c++  java
  • java合成图片并添加文字

    public static String generateCode(String codeUrl, Integer userId, String userName) {
        Font font = new Font("微软雅黑", Font.PLAIN, 30);// 添加字体的属性设置
     
        String projectUrl = PathKit.getWebRootPath() + "/before/codeImg/";
        String imgName = projectUrl + userId + ".png";
        try {
            // 加载本地图片
            String imageLocalUrl = projectUrl + "weixincode2.png";
            BufferedImage imageLocal = ImageIO.read(new File(imageLocalUrl));
            // 加载用户的二维码
            BufferedImage imageCode = ImageIO.read(new URL(codeUrl));
            // 以本地图片为模板
            Graphics2D g = imageLocal.createGraphics();
            // 在模板上添加用户二维码(地址,左边距,上边距,图片宽度,图片高度,未知)
            g.drawImage(imageCode, 575, imageLocal.getHeight() - 500, 350, 350, null);
            // 设置文本样式
            g.setFont(font);
            g.setColor(Color.BLACK);
            // 截取用户名称的最后一个字符
            String lastChar = userName.substring(userName.length() - 1);
            // 拼接新的用户名称
            String newUserName = userName.substring(0, 1) + "**" + lastChar + " 的邀请二维码";
            // 添加用户名称
            g.drawString(newUserName, 620, imageLocal.getHeight() - 530);
            // 完成模板修改
            g.dispose();
            // 获取新文件的地址
            File outputfile = new File(imgName);
            // 生成新的合成过的用户二维码并写入新图片
            ImageIO.write(imageLocal, "png", outputfile);
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 返回给页面的图片地址(因为绝对路径无法访问)
        imgName = Constants.PROJECT_URL + "codeImg/" + userId + ".png";
     
        return imgName;
    }

  • 相关阅读:
    MXCMS List标签说明
    c# 测量运行时间的方法
    简单的定时任务
    jQuery实现的简单文字提示效果模拟title
    ECSHOP设置默认配送方式和默认支付方式
    asp 里sql防止注入的方法
    asp.net上传图片并生成水印与缩略图的代码
    定时备份并清除系统日志的shell代码
    sql server 2005 万能分页存储过程
    01.部署NopCommerce时遇到的问题
  • 原文地址:https://www.cnblogs.com/lykbk/p/ssdsds435435454545454545.html
Copyright © 2011-2022 走看看