場景
項(xiàng)目需求為用戶創(chuàng)建活動,且生成了小程序碼,進(jìn)而再生成小程序碼分享圖,即在原小程序碼的基礎(chǔ)上繪制一張完整的分享圖,并展示給用戶進(jìn)行分享。個(gè)中過程均交由后臺生成并繪制(原因:用戶在小程序直接點(diǎn)擊某選項(xiàng),直接轉(zhuǎn)至分享圖頁面,而圖片存儲七牛云之上,若由前端生成再交由后臺處理并返回,便影響響應(yīng)速度)
說明
開發(fā)環(huán)境:JDK1.8
開發(fā)語言:Java
正題
生成小程序碼
調(diào)用微信SDK,最好的文檔便是官網(wǎng)文檔了,雖然坑是真的多...
進(jìn)入正題!!!
參考文檔:https://mp.weixin.qq.com/debug/wxadoc/dev/api/qrcode.html
根據(jù)業(yè)務(wù)需求選擇調(diào)用的相應(yīng)接口,博主所做的小程序業(yè)務(wù)需求是用于臨時(shí)場景,且需要多次分享、頻繁分享,因此選了接口B類型。
步驟
- 看文檔
坑點(diǎn):
1、注意場景值scene的說明,這個(gè)也很重要
2、注意page參數(shù)的說明,“必須是已經(jīng)發(fā)布的小程序頁面”(之前做的小程序由于前期未通過審核沒發(fā)布,很同意略過關(guān)鍵字眼。經(jīng)驗(yàn):未發(fā)布的小程序,無需傳該參數(shù),可正常獲取小程序碼,與scene不一樣,scene會涉及后續(xù)的業(yè)務(wù)需求,里面的值會有具體的業(yè)務(wù)操作)
- 編碼
說明:
1、獲取小程序碼需要獲取access_token。
2、scene值需跟前端進(jìn)行規(guī)約,具體的傳值格式(以下代碼scene值以加密形式傳入,可自定義加密規(guī)則)
3、以下 七牛上傳 功能使用的便是博主介紹到的七牛云模塊化,想了解的見上篇 《七牛云模塊化文檔》
public String getminiqrQr(String sceneStr, String accessToken) {
RestTemplate rest = new RestTemplate();
InputStream inputStream = null;
String path = null;
try {
String url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + accessToken;
Map<String, Object> param = new HashMap<>();
param.put("scene", "activity_" + sceneStr);
// param.put("page", "pages/Activity/Situation/Situation");
param.put("width", 200);
logger.info("調(diào)用生成微信URL接口傳參:" + param);
MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
org.springframework.http.HttpEntity requestEntity = new org.springframework.http.HttpEntity(param, headers);
ResponseEntity<byte[]> entity = rest.exchange(url, HttpMethod.POST, requestEntity, byte[].class);
logger.info("調(diào)用小程序生成微信小程序碼URL接口返回結(jié)果:" + entity.getBody());
byte[] result = entity.getBody();
logger.info(Base64.encodeBase64String(result));
inputStream = new ByteArrayInputStream(result);
//七牛上傳
path = qiniuService.uploadByStream(inputStream);
logger.info("----------------path:{}", path);
} catch (Exception e) {
logger.error("調(diào)用小程序生成微信小程序碼URL接口異常", e);
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return path;
}
以上最終便會返回小程序碼的url,算挺簡單的吧!
繪制小程序碼分享圖
效果如下:
說明
1、使用awt在畫布上一層層畫上去,文字、圖片均可以
2、畫上去的文字及圖片有坐標(biāo)的概念,需要底圖和上層圖片素材,以及坐標(biāo)(用方形框住圖片或文字,左上角即為原點(diǎn),團(tuán)隊(duì)上的協(xié)作可交由設(shè)計(jì)人員給出即可)編碼
以下代碼已附上注釋,夠詳細(xì)了!
public byte[] drawPicture(String backImg, String qrCodeImg, String kaiImg, String avatarImg,String activityText, String text1) throws IOException {
//底圖
ClassPathResource redResource = new ClassPathResource(backImg);
BufferedImage red = ImageIO.read(redResource.getInputStream());
//頭像
URL avatarUrl = new URL(avatarImg);
BufferedImage avatar = ImageIO.read(avatarUrl);
//小程序碼
URL qrCodeUrl = new URL(qrCodeImg);
BufferedImage qrCode = ImageIO.read(qrCodeUrl);
//開
ClassPathResource kaiResource = new ClassPathResource(kaiImg);
BufferedImage kai = ImageIO.read(kaiResource.getInputStream());
//白底
ClassPathResource whiteResource = new ClassPathResource("white.jpeg");
BufferedImage white = ImageIO.read(whiteResource.getInputStream());
// --- 畫圖 ---
//底層空白 bufferedImage
BufferedImage imgB = new BufferedImage(red.getWidth(), red.getHeight(), BufferedImage.TYPE_4BYTE_ABGR);
//畫上頭像
drawImgInImg(imgB, avatar, 277, 90, 145, 145);
//白底
drawImgInImg(imgB, white, 200, 400, 250, 250);
//畫上小程序碼
drawImgInImg(imgB, qrCode, 255, 434, 190, 190);
//畫上圖片
drawImgInImg(imgB, red, 0, 0, red.getWidth(), red.getHeight());
//畫上開
drawImgInImg(imgB, kai, 309, 488, 83, 83);
//寫上文字,上
drawTextInImg(imgB, text1, 350, 315);
//寫上文字,活動
drawTextInImg(imgB, activityText, 350, 395);
//轉(zhuǎn)jpg
BufferedImage result = new BufferedImage(imgB.getWidth(), imgB
.getHeight(), BufferedImage.TYPE_3BYTE_BGR);
result.getGraphics().drawImage(imgB, 0, 0, null);
ByteArrayOutputStream bs = new ByteArrayOutputStream();
ImageIO.write(result, "jpg", bs);
//最終byte數(shù)組
return bs.toByteArray();
}
private static BufferedImage drawImgInImg(BufferedImage bimageB, BufferedImage bimageT, int x, int y, int w, int h) {
Graphics2D g = bimageB.createGraphics();
g.drawImage(bimageT, x, y, w, h, null);
g.dispose();
return bimageB;
}
private static BufferedImage drawTextInImg(BufferedImage bimage, String text, int left, int top) {
Graphics2D g = bimage.createGraphics();
g.setColor(Color.white);
g.setBackground(Color.white);
Font font = new Font("微軟雅黑", Font.PLAIN, 34);
FontMetrics metrics = new FontMetrics(font) {
};
Rectangle2D bounds = metrics.getStringBounds(text, null);
int textHeight = (int) bounds.getHeight();
int textWidth = (int) bounds.getWidth();
g.setFont(font);
g.drawString(text, left - textWidth / 2, top - textHeight / 2);
g.dispose();
return bimage;
}
總結(jié)
以上即為小程序碼生成及繪制分享圖的常見場景,在此基礎(chǔ)上拓展出業(yè)務(wù)所需的功能,基本足夠了!?。」裁銅~~