?1.生成基礎二維碼
/** * 生成微信小程序二維碼,帶參數(shù),最終轉(zhuǎn)成base64 * @param page 當前小程序相對頁面 必須是已經(jīng)發(fā)布的小程序存在的頁面(否則報錯),例如 pages/index/index, 根路徑前不要填加 /,不能攜帶參數(shù)(參數(shù)請放在scene字段里),如果不填寫這個字段,默認跳主頁面 * @param scene 最大32個可見字符,只支持數(shù)字,大小寫英文以及部分特殊字符:!#$&'()*+,/:;=?@-._~,其它字符請自行編碼為合法字符(因不支持%,中文無法使用 urlencode 處理,請使用其他編碼方式) * @param accessToken 接口調(diào)用憑證 */ public static String generateQrCode(String page, String scene,String accessToken) { BufferedImage bi= null; try { URL url = new URL("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + accessToken); HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection(); httpURLConnection.setRequestMethod("POST"); httpURLConnection.setDoOutput(true); httpURLConnection.setDoInput(true); PrintWriter printWriter = new PrintWriter(httpURLConnection.getOutputStream()); JSONObject paramJson = new JSONObject(); paramJson.put("scene", scene); paramJson.put("page", page); paramJson.put("width", 430); paramJson.put("auto_color", false); JSONObject lineColor = new JSONObject(); lineColor.put("r", 0); lineColor.put("g", 0); lineColor.put("b", 0); paramJson.put("line_color", lineColor); printWriter.write(paramJson.toString()); printWriter.flush(); BufferedInputStream bis = new BufferedInputStream(httpURLConnection.getInputStream()); bi = ImageIO.read(bis); printWriter.close(); ByteArrayOutputStream stream = new ByteArrayOutputStream(); try { // 設置圖片格式 ImageIO.write(bi, "jpg", stream); } catch (IOException e) { e.printStackTrace(); } byte[] bytes = Base64.encodeBase64(stream.toByteArray()); String base64 = new String(bytes); return "data:image/jpeg;base64," + base64; } catch (Exception e) { e.printStackTrace(); } return null; }
2.自定義logo
加入以下代碼:
//要替換的圖片路徑 BufferedImage logoImage = ImageIO.read(new URL("https://nk-mall.oss-cn-shenzhen.aliyuncs.com/WDMPV_MP/1698932836550.png")); // logo圖的寬高 int width = logoImage.getWidth(); int height = logoImage.getHeight(); // 保存正方形的邊長 int size = Math.min(width, height); // 判斷那條邊的邊更長 // 裁剪:獲取正中間的正方形,邊長為圖片寬的值 后面.size方法必須調(diào)用 否則異常 logoImage = Thumbnails.of(logoImage).sourceRegion(Positions.CENTER, size, size).size(size, size).asBufferedImage(); // 轉(zhuǎn)成圓形 logoImage = convertCircular(logoImage); // 縮放:放大微信二維碼的底圖 目的為了減少對用戶上傳的圖片縮放過小圖片失真 bi = Thumbnails.of(bi).size(bi.getHeight() * 2, bi.getHeight() * 2).asBufferedImage(); // 使用Graphics2D合并圖片 Graphics2D g2 = null; // 讀取微信二維碼圖片 g2 = bi.createGraphics(); // 合并:并設置偏移量,logo圖片大小。具體需要自己按照實際的大小調(diào)整 g2.drawImage(logoImage, 232 , 232, 395, 395, null); g2.dispose();
完整代碼:文章來源:http://www.zghlxwxcb.cn/news/detail-744249.html
? /** * 生成微信小程序二維碼,帶參數(shù),最終轉(zhuǎn)成base64 * @param page 當前小程序相對頁面 必須是已經(jīng)發(fā)布的小程序存在的頁面(否則報錯),例如 pages/index/index, 根路徑前不要填加 /,不能攜帶參數(shù)(參數(shù)請放在scene字段里),如果不填寫這個字段,默認跳主頁面 * @param scene 最大32個可見字符,只支持數(shù)字,大小寫英文以及部分特殊字符:!#$&'()*+,/:;=?@-._~,其它字符請自行編碼為合法字符(因不支持%,中文無法使用 urlencode 處理,請使用其他編碼方式) * @param accessToken 接口調(diào)用憑證 */ public static String generateQrCode(String page, String scene,String accessToken) { BufferedImage bi= null; try { URL url = new URL("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + accessToken); HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection(); httpURLConnection.setRequestMethod("POST"); httpURLConnection.setDoOutput(true); httpURLConnection.setDoInput(true); PrintWriter printWriter = new PrintWriter(httpURLConnection.getOutputStream()); JSONObject paramJson = new JSONObject(); paramJson.put("scene", scene); paramJson.put("page", page); paramJson.put("width", 430); paramJson.put("auto_color", false); JSONObject lineColor = new JSONObject(); lineColor.put("r", 0); lineColor.put("g", 0); lineColor.put("b", 0); paramJson.put("line_color", lineColor); printWriter.write(paramJson.toString()); printWriter.flush(); BufferedInputStream bis = new BufferedInputStream(httpURLConnection.getInputStream()); bi = ImageIO.read(bis); printWriter.close(); //要替換的圖片路徑 BufferedImage logoImage = ImageIO.read(new URL("https://nk-mall.oss-cn-shenzhen.aliyuncs.com/WDMPV_MP/1698932836550.png")); // logo圖的寬高 int width = logoImage.getWidth(); int height = logoImage.getHeight(); // 保存正方形的邊長 int size = Math.min(width, height); // 判斷那條邊的邊更長 // 裁剪:獲取正中間的正方形,邊長為圖片寬的值 后面.size方法必須調(diào)用 否則異常 logoImage = Thumbnails.of(logoImage).sourceRegion(Positions.CENTER, size, size).size(size, size).asBufferedImage(); // 轉(zhuǎn)成圓形 logoImage = convertCircular(logoImage); // 縮放:放大微信二維碼的底圖 目的為了減少對用戶上傳的圖片縮放過小圖片失真 bi = Thumbnails.of(bi).size(bi.getHeight() * 2, bi.getHeight() * 2).asBufferedImage(); // 使用Graphics2D合并圖片 Graphics2D g2 = null; // 讀取微信二維碼圖片 g2 = bi.createGraphics(); // 合并:并設置偏移量,logo圖片大小。具體需要自己按照實際的大小調(diào)整 g2.drawImage(logoImage, 232 , 232, 395, 395, null); g2.dispose(); ByteArrayOutputStream stream = new ByteArrayOutputStream(); try { // 設置圖片格式 ImageIO.write(bi, "jpg", stream); } catch (IOException e) { e.printStackTrace(); } byte[] bytes = Base64.encodeBase64(stream.toByteArray()); String base64 = new String(bytes); return "data:image/jpeg;base64," + base64; } catch (Exception e) { e.printStackTrace(); } return null; } ?
?文章來源地址http://www.zghlxwxcb.cn/news/detail-744249.html
到了這里,關(guān)于生成小程序的二維碼的base64碼(中間logo可以自定義)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!