微信小程序> 微信小程序生成分享带参数二维码图片并添加文字功能

微信小程序生成分享带参数二维码图片并添加文字功能

浏览量:5783 时间: 来源:专业的程序员

       笔者最近接到一个新的任务,不是很难的功能,就是之前没有接触过,后端生成带参数的小程序二维码图片,并在图片下面添加一些文字。想在将代码分享给大家,期望可以给大家提供帮助。

一、首先生成小程序的分享二维码有三种方式

  • 接口 A: 适用于需要的码数量较少的业务场景
    • 生成小程序码,可接受 path 参数较长,生成个数受限。
  • 接口 B:适用于需要的码数量极多的业务场景
    • 生成小程序码,可接受页面参数较短,生成个数不受限。
  • 接口 C:适用于需要的码数量较少的业务场景
    • 生成二维码,可接受 path 参数较长,生成个数受限。 

具体的情况请参照 https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/qr-code.html

二、笔者使用第一种方式来生成分享二维码

  1、第一步,获取access_token

private String getAccessToken(){    String result = null;    try {        // 自己的 appid和secret        result = HttpUtil.get("https://api.weixin.qq.com/cgi-bin/token" +                "?grant_type=client_credential&appid=yourappId&secret=yoursecret",null,null);    } catch (Exception e) {        e.printStackTrace();    }    JSONObject jsonObject = JSON.parseObject(result);    result = jsonObject.getString("access_token");    System.out.println(result);    return  result;}

  2、第二步,请求分享二维码的二进制流

String URL = "https://api.weixin.qq.com/wxa/getwxacode?access_token=" + accessToken;JSONObject jsonObject = new JSONObject();jsonObject.put("path","pages/lsjindex/lsjindex");InputStream inputStream = null;try {    inputStream = HttpUtil.postStream(URL, jsonObject.toJSONString(), null);} catch (Exception e) {    e.printStackTrace();}BufferedImage bi = ImageIO.read(inputStream);

3、拼接文字

public BufferedImage createNewPic(String title,BufferedImage bi){    BufferedImage image = new BufferedImage(500, 550, BufferedImage.TYPE_INT_RGB);    //设置图片的背景色    Graphics2D main = image.createGraphics();    main.setColor(Color.white);    main.fillRect(0, 0, 500, 550);    //***********************插入中间广告图    Graphics mainPic = image.getGraphics();    if(logo!=null){        mainPic.drawImage(logo, 40, 40, 400, 400, null);        mainPic.dispose();    }    //***********************页面底部文字    Graphics titleG = image.createGraphics();    //设置区域颜色    //titleG.setColor(Color.white);    //填充区域并确定区域大小位置    //titleG.fillRect(450, 50, 450, 50);    //设置字体颜色,先设置颜色,再填充内容    titleG.setColor(Color.BLACK);    //设置字体    Font titleFont = new Font("宋体", Font.BOLD, 14);    titleG.setFont(titleFont);    titleG.drawString(title, 200, 500);    return image;}

4、最后生成图片并保存

File targetFile = new File("1.png");ImageIO.write(image, "png", targetFile);

 

完成的代码如下:

public class SmallAppTest {    public static void main(String[] args) throws IOException {        SmallAppTest test = new SmallAppTest();        // 获得access_token        String accessToken = test.getAccessToken();             // 获得小程序二维码        // POST https://api.weixin.qq.com/wxa/getwxacode?access_token=ACCESS_TOKEN        String URL = "https://api.weixin.qq.com/wxa/getwxacode?access_token=" + accessToken;        JSONObject jsonObject = new JSONObject();        jsonObject.put("path","pages/lsjindex/lsjindex");        InputStream inputStream = null;        try {            inputStream = HttpUtil.postStream(URL, jsonObject.toJSONString(), null);        } catch (Exception e) {            e.printStackTrace();        }        BufferedImage bi = ImageIO.read(inputStream);        BufferedImage image = test.createNewPic("测试", bi);        // 加文字        File targetFile = new File("1.png");        ImageIO.write(image, "png", targetFile);        // 关闭流        inputStream.close();    }    public BufferedImage createNewPic(String title,BufferedImage logo){        BufferedImage image = new BufferedImage(500, 550, BufferedImage.TYPE_INT_RGB);        //设置图片的背景色        Graphics2D main = image.createGraphics();        main.setColor(Color.white);        main.fillRect(0, 0, 500, 550);        //***********************插入中间广告图        Graphics mainPic = image.getGraphics();        if(logo!=null){            mainPic.drawImage(logo, 40, 40, 400, 400, null);            mainPic.dispose();        }        //***********************页面头部        Graphics titleG = image.createGraphics();        //设置区域颜色        //titleG.setColor(Color.white);        //填充区域并确定区域大小位置        //titleG.fillRect(450, 50, 450, 50);        //设置字体颜色,先设置颜色,再填充内容        titleG.setColor(Color.BLACK);        //设置字体        Font titleFont = new Font("宋体", Font.BOLD, 14);        titleG.setFont(titleFont);        titleG.drawString(title, 200, 500);        return image;    }        private String getAccessToken(){        String result = null;        try {            result = HttpUtil.get("https://api.weixin.qq.com/cgi-bin/token" +                    "?grant_type=client_credential&appid=appId&secret=secret",null,null);        } catch (Exception e) {            e.printStackTrace();        }        JSONObject jsonObject = JSON.parseObject(result);        result = jsonObject.getString("access_token");        System.out.println(result);        return  result;    }}

用到的工具类:Http工具类 

https://download.csdn.net/download/money9sun/10973850

结果图

 

 

 

  推荐:史上最全的java开发工具类    地址:https://github.com/EricLoveMia/JavaTools

 

小程序

版权声明

即速应用倡导尊重与保护知识产权。如发现本站文章存在版权问题,烦请提供版权疑问、身份证明、版权证明、联系方式等发邮件至197452366@qq.com ,我们将及时处理。本站文章仅作分享交流用途,作者观点不等同于即速应用观点。用户与作者的任何交易与本站无关,请知悉。

产品经理

手机 : 13312967497

擅长 : 小程序流量变现

扫码领取礼包

最新资讯

热门模板

  • 头条
  • 搜狐
  • 微博
  • 百家
  • 一点资讯
  • 知乎