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


     1.生成基础二维码

    1. /**
    2. * 生成微信小程序二维码,带参数,最终转成base64
    3. * @param page 当前小程序相对页面 必须是已经发布的小程序存在的页面(否则报错),例如 pages/index/index, 根路径前不要填加 /,不能携带参数(参数请放在scene字段里),如果不填写这个字段,默认跳主页面
    4. * @param scene 最大32个可见字符,只支持数字,大小写英文以及部分特殊字符:!#$&'()*+,/:;=?@-._~,其它字符请自行编码为合法字符(因不支持%,中文无法使用 urlencode 处理,请使用其他编码方式)
    5. * @param accessToken 接口调用凭证
    6. */
    7. public static String generateQrCode(String page, String scene,String accessToken) {
    8. BufferedImage bi= null;
    9. try {
    10. URL url = new URL("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + accessToken);
    11. HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
    12. httpURLConnection.setRequestMethod("POST");
    13. httpURLConnection.setDoOutput(true);
    14. httpURLConnection.setDoInput(true);
    15. PrintWriter printWriter = new PrintWriter(httpURLConnection.getOutputStream());
    16. JSONObject paramJson = new JSONObject();
    17. paramJson.put("scene", scene);
    18. paramJson.put("page", page);
    19. paramJson.put("width", 430);
    20. paramJson.put("auto_color", false);
    21. JSONObject lineColor = new JSONObject();
    22. lineColor.put("r", 0);
    23. lineColor.put("g", 0);
    24. lineColor.put("b", 0);
    25. paramJson.put("line_color", lineColor);
    26. printWriter.write(paramJson.toString());
    27. printWriter.flush();
    28. BufferedInputStream bis = new BufferedInputStream(httpURLConnection.getInputStream());
    29. bi = ImageIO.read(bis);
    30. printWriter.close();
    31. ByteArrayOutputStream stream = new ByteArrayOutputStream();
    32. try {
    33. // 设置图片格式
    34. ImageIO.write(bi, "jpg", stream);
    35. } catch (IOException e) {
    36. e.printStackTrace();
    37. }
    38. byte[] bytes = Base64.encodeBase64(stream.toByteArray());
    39. String base64 = new String(bytes);
    40. return "data:image/jpeg;base64," + base64;
    41. } catch (Exception e) {
    42. e.printStackTrace();
    43. }
    44. return null;
    45. }

    2.自定义logo

    加入以下代码:

    1. //要替换的图片路径
    2. BufferedImage logoImage = ImageIO.read(new URL("https://nk-mall.oss-cn-shenzhen.aliyuncs.com/WDMPV_MP/1698932836550.png"));
    3. // logo图的宽高
    4. int width = logoImage.getWidth();
    5. int height = logoImage.getHeight();
    6. // 保存正方形的边长
    7. int size = Math.min(width, height);
    8. // 判断那条边的边更长
    9. // 裁剪:获取正中间的正方形,边长为图片宽的值 后面.size方法必须调用 否则异常
    10. logoImage = Thumbnails.of(logoImage).sourceRegion(Positions.CENTER, size, size).size(size, size).asBufferedImage();
    11. // 转成圆形
    12. logoImage = convertCircular(logoImage);
    13. // 缩放:放大微信二维码的底图 目的为了减少对用户上传的图片缩放过小图片失真
    14. bi = Thumbnails.of(bi).size(bi.getHeight() * 2, bi.getHeight() * 2).asBufferedImage();
    15. // 使用Graphics2D合并图片
    16. Graphics2D g2 = null;
    17. // 读取微信二维码图片
    18. g2 = bi.createGraphics();
    19. // 合并:并设置偏移量,logo图片大小。具体需要自己按照实际的大小调整
    20. g2.drawImage(logoImage, 232 , 232, 395, 395, null);
    21. g2.dispose();

    完整代码:

    1. /**
    2. * 生成微信小程序二维码,带参数,最终转成base64
    3. * @param page 当前小程序相对页面 必须是已经发布的小程序存在的页面(否则报错),例如 pages/index/index, 根路径前不要填加 /,不能携带参数(参数请放在scene字段里),如果不填写这个字段,默认跳主页面
    4. * @param scene 最大32个可见字符,只支持数字,大小写英文以及部分特殊字符:!#$&'()*+,/:;=?@-._~,其它字符请自行编码为合法字符(因不支持%,中文无法使用 urlencode 处理,请使用其他编码方式)
    5. * @param accessToken 接口调用凭证
    6. */
    7. public static String generateQrCode(String page, String scene,String accessToken) {
    8. BufferedImage bi= null;
    9. try {
    10. URL url = new URL("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + accessToken);
    11. HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
    12. httpURLConnection.setRequestMethod("POST");
    13. httpURLConnection.setDoOutput(true);
    14. httpURLConnection.setDoInput(true);
    15. PrintWriter printWriter = new PrintWriter(httpURLConnection.getOutputStream());
    16. JSONObject paramJson = new JSONObject();
    17. paramJson.put("scene", scene);
    18. paramJson.put("page", page);
    19. paramJson.put("width", 430);
    20. paramJson.put("auto_color", false);
    21. JSONObject lineColor = new JSONObject();
    22. lineColor.put("r", 0);
    23. lineColor.put("g", 0);
    24. lineColor.put("b", 0);
    25. paramJson.put("line_color", lineColor);
    26. printWriter.write(paramJson.toString());
    27. printWriter.flush();
    28. BufferedInputStream bis = new BufferedInputStream(httpURLConnection.getInputStream());
    29. bi = ImageIO.read(bis);
    30. printWriter.close();
    31. //要替换的图片路径
    32. BufferedImage logoImage = ImageIO.read(new URL("https://nk-mall.oss-cn-shenzhen.aliyuncs.com/WDMPV_MP/1698932836550.png"));
    33. // logo图的宽高
    34. int width = logoImage.getWidth();
    35. int height = logoImage.getHeight();
    36. // 保存正方形的边长
    37. int size = Math.min(width, height);
    38. // 判断那条边的边更长
    39. // 裁剪:获取正中间的正方形,边长为图片宽的值 后面.size方法必须调用 否则异常
    40. logoImage = Thumbnails.of(logoImage).sourceRegion(Positions.CENTER, size, size).size(size, size).asBufferedImage();
    41. // 转成圆形
    42. logoImage = convertCircular(logoImage);
    43. // 缩放:放大微信二维码的底图 目的为了减少对用户上传的图片缩放过小图片失真
    44. bi = Thumbnails.of(bi).size(bi.getHeight() * 2, bi.getHeight() * 2).asBufferedImage();
    45. // 使用Graphics2D合并图片
    46. Graphics2D g2 = null;
    47. // 读取微信二维码图片
    48. g2 = bi.createGraphics();
    49. // 合并:并设置偏移量,logo图片大小。具体需要自己按照实际的大小调整
    50. g2.drawImage(logoImage, 232 , 232, 395, 395, null);
    51. g2.dispose();
    52. ByteArrayOutputStream stream = new ByteArrayOutputStream();
    53. try {
    54. // 设置图片格式
    55. ImageIO.write(bi, "jpg", stream);
    56. } catch (IOException e) {
    57. e.printStackTrace();
    58. }
    59. byte[] bytes = Base64.encodeBase64(stream.toByteArray());
    60. String base64 = new String(bytes);
    61. return "data:image/jpeg;base64," + base64;
    62. } catch (Exception e) {
    63. e.printStackTrace();
    64. }
    65. return null;
    66. }

     

  • 相关阅读:
    C语言文件操作(超详细版)
    C语言-数组
    如何假装你懂机器学习?
    javascript大作业《web课程设计》用html做一个期末作业网站,梅西足球体育网页,css
    nodejs+vue+elementui电影在线播放交流网站express
    第15集丨知行合一
    Zynq学习笔记--了解中断配置方式
    GB28181,sdk,设备集成和平台测试
    MATLAB算法实战应用案例精讲-【智能优化算法】非支配排序遗传算法-NSGA-Ⅱ(附python和matlab代码)
    记录小技巧--前端等所有的请求都结束了再刷新页面,button对齐input
  • 原文地址:https://blog.csdn.net/qq_26112725/article/details/134208453