当前位置: 首页 > news >正文

生成小程序的二维码的base64码(中间logo可以自定义)

 1.生成基础二维码

    /*** 生成微信小程序二维码,带参数,最终转成base64* @param page 当前小程序相对页面 必须是已经发布的小程序存在的页面(否则报错),例如 pages/index/index, 根路径前不要填加 /,不能携带参数(参数请放在scene字段里),如果不填写这个字段,默认跳主页面* @param scene 最大32个可见字符,只支持数字,大小写英文以及部分特殊字符:!#$&'()*+,/:;=?@-._~,其它字符请自行编码为合法字符(因不支持%,中文无法使用 urlencode 处理,请使用其他编码方式)* @param accessToken 接口调用凭证*/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方法必须调用 否则异常logoImage = Thumbnails.of(logoImage).sourceRegion(Positions.CENTER, size, size).size(size, size).asBufferedImage();// 转成圆形logoImage = convertCircular(logoImage);// 缩放:放大微信二维码的底图  目的为了减少对用户上传的图片缩放过小图片失真bi = Thumbnails.of(bi).size(bi.getHeight() * 2, bi.getHeight() * 2).asBufferedImage();// 使用Graphics2D合并图片Graphics2D g2 = null;// 读取微信二维码图片g2 = bi.createGraphics();// 合并:并设置偏移量,logo图片大小。具体需要自己按照实际的大小调整g2.drawImage(logoImage, 232 , 232, 395, 395, null);g2.dispose();

完整代码:

​
/*** 生成微信小程序二维码,带参数,最终转成base64* @param page 当前小程序相对页面 必须是已经发布的小程序存在的页面(否则报错),例如 pages/index/index, 根路径前不要填加 /,不能携带参数(参数请放在scene字段里),如果不填写这个字段,默认跳主页面* @param scene 最大32个可见字符,只支持数字,大小写英文以及部分特殊字符:!#$&'()*+,/:;=?@-._~,其它字符请自行编码为合法字符(因不支持%,中文无法使用 urlencode 处理,请使用其他编码方式)* @param accessToken 接口调用凭证*/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方法必须调用 否则异常logoImage = Thumbnails.of(logoImage).sourceRegion(Positions.CENTER, size, size).size(size, size).asBufferedImage();// 转成圆形logoImage = convertCircular(logoImage);// 缩放:放大微信二维码的底图  目的为了减少对用户上传的图片缩放过小图片失真bi = Thumbnails.of(bi).size(bi.getHeight() * 2, bi.getHeight() * 2).asBufferedImage();// 使用Graphics2D合并图片Graphics2D g2 = null;// 读取微信二维码图片g2 = bi.createGraphics();// 合并:并设置偏移量,logo图片大小。具体需要自己按照实际的大小调整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.lryc.cn/news/219750.html

相关文章:

  • 【音视频 | Ogg】Ogg封装格式详解——包含Ogg封装过程、数据包(packet)、页(page)、段(segment)等
  • ubuntu 22.04 安装ros2 iron
  • PHP语言、B/S手术麻醉临床信息管理系统源码
  • Win11安装网络打印机
  • 逆向学习记录(3)工具介绍jadx、gda和jeb
  • C#,数值计算——偏微分方程,Mglin的计算方法与源程序
  • 一机服务万人,拓世法宝AI智能商业数字人一体机,解锁文旅新表达
  • 【源码解析】聊聊SpringBean是如何初始化和创建
  • 【0基础学Java第六课】-- 数组的定义与使用
  • 后台项目Gradle打包jar,不包含依赖jar并放到外部路径
  • NSSCTF web刷题记录4
  • 什么是大模型?一文读懂大模型的基本概念
  • 数据结构之队的实现
  • 【实战Flask API项目指南】之三 路由和视图函数
  • mediasoup udp端口分配策略
  • 山西电力市场日前价格预测【2023-11-07】
  • Microsoft Dynamics 365 CE 扩展定制 - 5. 外部集成
  • 手机升级STM32单片机,pad下载程序,手机固件升级单片机,局域网程序下载,STM32单片机远程下载升级
  • 【漏洞复现】weblogic-SSRF漏洞
  • FreeSWTCH dialplan check nosdp
  • 微信小程序案例3-1 比较数字
  • 哈希表----数据结构
  • 可达矩阵-邻接矩阵-以及有向图的python绘制
  • react typescript @别名的使用
  • C++性能优化笔记-6-C++元素的效率差异-7-类型转换
  • c#中switch常用模式
  • Flink SQL 常用作业sql
  • nodejs国内镜像及切换版本工具nvm
  • 用Rust和Scraper库编写图像爬虫的建议
  • Java 语言环境搭建