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

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

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

具体的情况请参照 获取小程序码 | 微信开放文档

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

  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工具类 

Http工具类-Java工具类资源-CSDN下载

结果图

  推荐:史上最全的java开发工具类    地址:GitHub - EricLoveMia/JavaTools: 基于JDK8 的工具类合集 maven项目

微信小程序可以使用微信提供的API生成参数二维码,具体步骤如下: 1. 在小程序管理后台中,进入“开发”->“开发设置”->“接口设置”,勾选“生成参数二维码”并保存。 2. 在小程序中调用wx.request()方法向微信服务器发送生成二维码的请求,请求URL为:https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode,请求方法为POST。 3. 在请求参数中,需要传入access_token(调用凭证)、path(小程序页面路径,可以参数)、width(二维码宽度,单位为像素,默认为430px),例如: ``` wx.request({ url: 'https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode', method: 'POST', header: { 'content-type': 'application/json' }, data: { access_token: 'ACCESS_TOKEN', path: 'pages/index/index?param1=xxx&param2=xxx', width: 430 }, success: function (res) { console.log(res.data) } }) ``` 4. 微信服务器会返回二进制数据,可以使用wx.arrayBufferToBase64()方法将其转换为base64字符串,再使用wx.createImage()方法生成图片。例如: ``` wx.request({ url: 'https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode', method: 'POST', header: { 'content-type': 'application/json' }, data: { access_token: 'ACCESS_TOKEN', path: 'pages/index/index?param1=xxx&param2=xxx', width: 430 }, responseType: 'arraybuffer', success: function (res) { var base64 = wx.arrayBufferToBase64(res.data) wx.createImage({ src: 'data:image/jpeg;base64,' + base64, success: function (res) { console.log(res) } }) } }) ``` 以上就是生成参数二维码的步骤,需要注意的是,调用接口时需要传入正确的access_token,而且access_token有时效性,需要定期更新。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值