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

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/money9sun/article/details/87911426

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

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

具体的情况请参照 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工具类 

扫描二维码关注公众号,回复: 5338734 查看本文章

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

结果图

  

猜你喜欢

转载自blog.csdn.net/money9sun/article/details/87911426