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;
}
                            
                        
                        
                        總結
以上是生活随笔為你收集整理的java合成图片并添加文字的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: 云之讯官方测试Demo音频版源码阅读(编
 - 下一篇: 为什么Markdown+R有较大概率成为