关于java:生成二维码

34次阅读

共计 3586 个字符,预计需要花费 9 分钟才能阅读完成。

生成二维码

<!-- 生成二维码依赖 -->
<dependency>
    <groupId>com.google.zxing</groupId>
    <artifactId>core</artifactId>
    <version>3.3.0</version>
</dependency>

## 生成一般二维码

public class CodeService {
    /* 二维码跳转的 url*/
 @Value("${code.url}")
    private String url;
    // 嵌入二维码的图片门路
 @Value("${code.imgpath}")
    private String imgPath;
     // 生成的二维码的门路
 private String destPath;
    // 嵌入二维码的图片保留地址
 private String imgPath_new;
 
 // 上传服务器类
 @Autowired
 private FileUploadController uploadController;
/**
 * 生成二维码 * @param qrCode
 * @return
 */
 public String addQrCode(QrCode qrCode) throws Exception {URL url = new URL(imgPath);
        // 关上 URL 连贯
        HttpsURLConnection con = (HttpsURLConnection) 
                url.openConnection();
        // 失去 URL 的输出流
        InputStream input = con.getInputStream();
        // 设置数据缓冲
        byte[] bs = new byte[1024 * 2];
        // 读取到的数据长度
        int len;
        // 输入的文件流保留图片至本地
        File file = new File("");
        String uuid = GeneratorUtil.genFileName();
        String filePath = file.getCanonicalPath()+"srcmainresourcesstatic";// 获取以后我的项目门路 + 保留的门路
        imgPath_new=filePath+uuid+".jpg"; // 图标保留地址
        OutputStream os = new FileOutputStream(filePath+uuid+".jpg");
        while ((len = input.read(bs)) != -1) {os.write(bs, 0, len);
        }
        destPath=filePath+uuid+".jpg"; // 二维码寄存地址
        String url_new="跳转地址"+"?qrId="+qrCode.getId()+
                "&communityId="+
        qrCode.getCommunityId()+"&name="+
            qrCode.getRemarks();
        QRCodeUtil.encode(url_new,
            imgPath_new,destPath,true);
        InputStream stream=new FileInputStream(destPath);
        String path = uploadController.inputUplaod(stream);         // 上传到服务器
        os.close();
        input.close();
        stream.close();
        return path;
    }
    
    }

生成小程序码 (B 接口)

// 获取 AccessToken 门路
private static final String AccessToken_URL
 = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";// 小程序 id
// 获取二维码门路
private static final String WxCode_URL
 = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=ACCESS_TOKEN";// 小程序密钥
 
 /**
 * 生成微信二维码 * */public static String addWxCode(String accessToken,QrCode qrCodes) {
    String path = "";
    String filePath = "c://zsyy_test/";
    File file = new File(filePath);
    if (!file.exists()) {file.mkdirs();
    }
    String picName = UUID.randomUUID().toString() + ".jpg";
    String savePath = filePath+picName;
    try{
        String wxCodeURL =WxCode_URL.replace
            ("ACCESS_TOKEN",accessToken);
        URL url = new URL(wxCodeURL);
        HttpURLConnection httpURLConnection = 
            (HttpURLConnection) url.openConnection();
        // 提交模式
        httpURLConnection.setRequestMethod("POST");
        // conn.setConnectTimeout(10000);// 连贯超时 单位毫秒 
        // conn.setReadTimeout(2000);// 读取超时 单位毫秒 
        // 发送 POST 申请  必须设置如下两行  
        httpURLConnection.setDoOutput(true);
        httpURLConnection.setDoInput(true);
        // 获取 URLConnection 对象对应的输入流
        PrintWriter printWriter = new PrintWriter
            (httpURLConnection.getOutputStream());
        String param = "qrId="+qrCodes.getId()+
            "&communityId="+qrCodes.getCommunityId();
        // 发送申请参数
        JSONObject paramJson = new JSONObject();
        paramJson.put("scene", param);
        // 跳转曾经公布的小程序门路
        paramJson.put("page", "pages/proper/photograph");
        paramJson.put("width", 430);
        paramJson.put("is_hyaline", true);
        paramJson.put("auto_color", true);
        //line_color 失效
        paramJson.put("auto_color", false);
        JSONObject lineColor = new JSONObject();
        lineColor.put("r", 0);
        lineColor.put("g", 0);
        lineColor.put("b", 0);
        paramJson.put("line_color", lineColor);
        printWriter.write(paramJson.toString());
        // flush 输入流的缓冲
        printWriter.flush();
        // 开始获取数据
        BufferedInputStream bis = new BufferedInputStream
            (httpURLConnection.getInputStream());
        OutputStream os = new FileOutputStream(new 
            File(savePath));
        int len;
        byte[] arr = new byte[1024];
        while ((len = bis.read(arr)) != -1)
        {os.write(arr, 0, len);
            os.flush();}
        File getFile = new File(savePath);
        InputStream stream=new FileInputStream(getFile);
        FileUploadController upload = new 
                FileUploadController();
        path = upload.inputUplaod(stream); // 上传到服务器
        os.close();
        stream.close();
        // 删除本地文件
        File filePaths = new   
               File("c://zsyy_test/"+picName);
        filePaths.delete();
        File getParentFile = new File(filePath);
        getParentFile.delete();}
    catch (Exception e)
    {e.printStackTrace();
    }
    return path;
}

正文完
 0