發環境及框架后端:國產java極速框架JFinal(超級好用有木有?。┣岸耍篧EUI WXSS版(微信團隊出品的,自己寫樣式不管怎么寫都丑?。祿欤篗ySQL服務器:Linux Nginx Tomcat開發流程 ...
開發環境及框架
開發流程
后端關鍵代碼詳情
2個方法,一個是接收前臺傳入的參數生成圖片并返回處理結果的,一個是獲取用戶openid時候的前置請求
private static final WeixinInfo weixinInfo = WeixinInfo.dao.getWeixinInfoById(5);
/**
* 創建一個接口,用戶需傳遞姓名和微信openid
* 每個用戶只能創建一張(為服務器考慮,重復生成則覆蓋上一張)
* 返回結果有fail和ok兩種
*/
public void getMarryPic() {
Map map = new HashMap<>();
String result = "fail";
String openid = getPara("openid");
String name = getPara("name");
if (name != null && !name.equals("")) {
ImgMarkUtil.mark(getRequest().getServletContext().getRealPath("/") + "/jiehun/yuan2.jpg", getRequest().getServletContext().getRealPath("/") + "jiehun/" + openid + ".jpg", name);
result = "ok";
map.put("imgurl", weixinInfo.get("host") + "/jiehun/" + openid + ".jpg");
}
map.put("result", result);
renderJson(map);
}
public void getWxCode() {
String js_code = getPara("js_code");
String alipayURL = "https://api.weixin.qq.com/sns/jscode2session?";
renderText(HttpUtils.post(alipayURL, "appid=" + weixinInfo.get("appid") + "&secret=" + weixinInfo.get("appsecret")
+ "&grant_type=authorization_code" + "&js_code=" + js_code));
}
通過讀取本地原圖片,把用戶傳入的姓名弄成水印附在圖片上,隨后根據openid生成文件名。
public static void mark(String srcImgPath, String outImgPath, String waterMarkContent) {
try {
// 讀取原圖片信息
File srcImgFile = new File(srcImgPath);
Image srcImg = ImageIO.read(srcImgFile);
int srcImgWidth = srcImg.getWidth(null);
int srcImgHeight = srcImg.getHeight(null);
// 加水印