Java 创建带参数小程序二维码

        生成小程序二维码有三个接口,根据自己的业务场景选择合适的接口,这里我选择的是接口B

 注意:所带参数的长度不得超过32字符。access_token是小程序的

package com.qihong.file.controller;

import com.alibaba.fastjson.JSONObject;
import com.qihong.file.config.CustomizeException;
import com.qihong.file.domain.dto.WechatQRcodeDto;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.io.*;

import static com.qihong.file.utils.sendPostUtil.sendPost;

/**
 * @author zhg
 * @create 2022/8/4
 */
@Log4j2
public class QrCodeConeroller {
    /**
     * 获取小程序菊花码
     **/
    @PostMapping("/createWechatQR")
    public String getWXCode(@RequestBody WechatQRcodeDto wechatQRcodeDto)  {
        //上传本地路径
        String filePath = "D:\\home\\uploadPath\\temp"+wechatQRcodeDto.getUid()+".png";

        FileOutputStream fileOutputStream = null;
        ByteArrayInputStream inputStream = null;
        try {
            //这里调用的是之前博客的获取access_token方法通过传参
            //这里获取的access_token所需的appid与secret是小程序的
            String access_token = wechatQRcodeDto.getToken();

            String url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + access_token;

            JSONObject paramJson = new JSONObject();
            //这就是你二维码里携带的参数 String型 名称不可变 不超过32个字符
            String scene = wechatQRcodeDto.getUid() + "&" + wechatQRcodeDto.getType();
            paramJson.put("scene", scene);
            //注意该接口传入的是page而不是path
            //这是设置扫描二维码后跳转的页面
            paramJson.put("page", wechatQRcodeDto.getAppletUrl());
            paramJson.put("width", 430);
            paramJson.put("is_hyaline", true);
            paramJson.put("auto_color", true);
            inputStream = sendPost(url, paramJson.toString());
            //这里判断的是返回的图片还是错误信息,一般错误信息不会大于200
            if (inputStream.available() <= 200) {
                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                int i;
                byte[] buffer = new byte[200];
                while ((i = inputStream.read(buffer)) != -1) {
                    byteArrayOutputStream.write(buffer, 0, i);
                }
                String str = new String(byteArrayOutputStream.toByteArray());
                //错误信息的格式在官方文档里有
                JSONObject jsonObject = JSONObject.parseObject(str);
                log.info("生成二维码错误信息-----"+jsonObject);
                if ("41030".equals(jsonObject.getString("errcode"))) {
                    log.error("所传page页面不存在,或者小程序没有发布");
                    throw new CustomizeException("所传page页面不存在,或者小程序没有发布");
                } else if ("45009".equals(jsonObject.getString("errcode"))) {
                    log.error("调用分钟频率受限");
                    throw new CustomizeException("调用分钟频率受限");
                }
                byteArrayOutputStream.close();
                inputStream = sendPost(url, paramJson.toString());
                log.info("再次请求字节大小------" + inputStream.available());
            }
            //输出到本地的代码
            fileOutputStream = new FileOutputStream(new File(filePath));
            int i;
            byte[] buffer = new byte[1024];
            while ((i = inputStream.read(buffer)) != -1) {
                fileOutputStream.write(buffer, 0, i);
            }
            fileOutputStream.flush();

            log.info("二维码生成成功");
        } catch (Exception e) {
            log.error("获取二维码异常");
            try {
                throw new CustomizeException(e.getMessage());
            } catch (CustomizeException ex) {
                ex.printStackTrace();
            }
        }finally {
            try {
                if(fileOutputStream != null){
                    fileOutputStream.close();
                }
                if(inputStream != null){
                    inputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        log.info("打开地址查看生成的小程序二维码:" + filePath);
        //这里需要把filePath保存起来,我这里返回到所以服务保存。我用的永久有效,数量暂无限制的接口
        return filePath;
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Java生成参数小程序二维码,可以使用第三方库或者API来实现。以下是一种可能的方法: 首先,你需要选择一个适合的二维码生成库。在这里,我们以Zxing库为例进行说明。你可以在Maven中添加对Zxing库的依赖,然后在Java项目中使用它。 接下来,你需要创建一个字符串,以包含参数小程序链接。例如,你可以将参数附加到小程序链接的末尾,如下所示:https://xxx.xxx.xxx?param1=xxx&param2=xxx。请根据你的具体需求自行替换链接和参数。 然后,你可以使用Zxing库生成参数小程序二维码。以下是一个代码示例: ```java import com.google.zxing.BarcodeFormat; import com.google.zxing.MultiFormatWriter; import com.google.zxing.WriterException; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.QRCodeWriter; import javax.imageio.ImageIO; import java.awt.*; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; public class QrCodeGenerator { public static void main(String[] args) { String content = "https://xxx.xxx.xxx?param1=xxx&param2=xxx"; // 替换为你的参数小程序链接 int width = 300; int height = 300; try { BitMatrix bitMatrix = new QRCodeWriter().encode(content, BarcodeFormat.QR_CODE, width, height); BufferedImage qrCodeImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { qrCodeImage.setRGB(x, y, bitMatrix.get(x, y) ? Color.BLACK.getRGB() : Color.WHITE.getRGB()); } } File outputFile = new File("qr_code.jpg"); // 保存二维码的文件名和路径 ImageIO.write(qrCodeImage, "jpg", outputFile); } catch (WriterException | IOException e) { e.printStackTrace(); } } } ``` 运行上述代码后,它将生成一个名为"qr_code.jpg"的图像文件,其中包含参数小程序二维码。 当然,以上只是一种实现方法,你也可以使用其他类库或API来生成参数小程序二维码。希望能对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值