java合成图片并添加文字
2017-08-31 18:29
441 查看
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; }
相关文章推荐
- 用java 对图片的截图、拼接、合成处理、添加文字
- JAVA工具类之多图片合成与图片添加文字
- java实现给图片添加水印和文字
- 用java实现给图片增加图片水印或者文字水印(也支持视频图像帧添加水印)
- java 通过 itext生成pdf添加,插入文字,图片
- java给图片添加水印文字
- 用java实现给图片增加图片水印或者文字水印(也支持视频图像帧添加水印)
- java给图片添加文字水印并消除锯齿
- java图片添加文字
- JAVA合成图片和文字
- java实现给图片添加水印和文字
- 用java实现给图片增加图片水印或者文字水印(也支持视频图像帧添加水印)
- java给图片添加水印文字
- Java之图片添加文字
- Java添加水印+图片水印+文字水印
- php 图片添加文字水印 以及 图片合成(微信快码传播)
- 关于java 向图片添加文字时的乱码问题
- Java--图片中动态添加文字
- JAVA给图片添加水印和文字
- Java给图片添加图片、文字水印