1.實現分享圖的效果java
enter image description herelinux
2.開發環境git
2.1 JDKgithub
oracle‘s jdk 1.8以上
2.2 字體sql
3.1 加載背景圖小程序
這是使用白色框底來做背景圖1.
java code //這裏從項目resources加載背景圖,讀文件到輸入流,代碼做了簡列 InputStream background = null BufferedImage zoomPicture = ImageIO.read(background);架構
4.1 頭像裁剪併發
頭像裁剪成半徑oracle
//頭像半徑
public static final int PROFILE_RADIUS = 80;
// 2. 頭像裁剪成圓形
BufferedImage roundedImage = SharedImageUtils.createRoundedImage(new URL(userProfileUrl).openStream(),
SharedImageUtils.PROFILE_RADIUS);
4.2 背景圖上繪畫頭像分佈式
繪畫位置
/ 要放置的頭像y座標 /
public static final int PROFILE_Y = 1056;
/ 要放置的頭像X座標 /
public static final int PROFILE_X = 90;
//頭像旁邊附帶文字(ps:字體是微軟雅黑,linux不具有有,須要安裝,)
BufferedImage profileImage = SharedImageUtils.mergePicture(zoomPicture,
roundedImage,
nickName + " 爲你推薦網批貨源",
SharedImageUtils.PROFILE_X,
SharedImageUtils.PROFILE_Y,
SharedImageUtils.PROFILE_RADIUS,
SharedImageUtils.PROFILE_RADIUS);
5.1 繪畫的位置和長寬
public static final int COPYWRITER_X = 150;
/ 商店圖案Y位置 /
public static final int SHOP_PIC_Y = 70;
/商店圖案位置/
public static final int SHOP_PIC_X = 93;
/ 商店圖案寬度 /
public static final int SHOP_PIC_WIDTH = 900;
/ 商店圖案長度 /
public static final int SHOP_PIC_LENGTH = 950;
5.2 繪畫圖案
shopImage = SharedImageUtils.mergePicture(profileImage, shopImage, null,
SharedImageUtils.SHOP_PIC_X,
SharedImageUtils.SHOP_PIC_Y,
SharedImageUtils.SHOP_PIC_WIDTH, SharedImageUtils.SHOP_PIC_LENGTH);
6.1 文案繪畫的位置與字體大小
BufferedImage textImage = SharedImageUtils.drawTextInImage(shopImage, "檔口: " + shopName, 150, 1200);
//添加文案
BufferedImage mergeImage = SharedImageUtils.drawTextInImage(textImage, "地址: " + shopAddr, 150, 1280);
7.1 二維碼的大小與位置
/ 要放置的二維碼寬度 /
public static final int QRCODE_WIDTH = 230;
/ 要放置的二維碼長度 /
public static final int QRCODE_LENGTH = 230;
/ 要放置的二維碼Y位置 往下爲大值,往上爲小值 /
public static final int QRCODE_Y = 1070;
/要放置的二維碼X位置 往下爲大值,往上爲小值 /
public static final int QRCODE_X = 740;
7.2 二維碼的繪畫
BufferedImage shopQrBuffer = ImageIO.read(new URL(shopQrUrl));
mergeImage = SharedImageUtils.mergeQrcode(mergeImage,
shopQrBuffer,
"掃描或長按小程序碼",
SharedImageUtils.QRCODE_X,
SharedImageUtils.QRCODE_Y,
SharedImageUtils.QRCODE_WIDTH,
SharedImageUtils.QRCODE_LENGTH);
// 5. 生成分享圖
ImageIO.write(mergeImage, "jpg", response.getOutputStream());