调用微信小程序二维码接口生成二维码

package com.eq.common;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
import java.util.Iterator;
import java.util.Map;

public class UrlUtil {
	/**
	 * 向指定 URL 发送POST方法的请求
	 * 
	 * @param url
	 *            发送请求的 URL
	 * @param param
	 *            请求参数
	 * @return 所代表远程资源的响应结果
	 */
	public static String sendPost(String url, Map<String, ?> paramMap) {
		PrintWriter out = null;
		BufferedReader in = null;
		String result = "";

		String param = "";
		Iterator<String> it = paramMap.keySet().iterator();

		while (it.hasNext()) {
			String key = it.next();
			param += key + "=" + paramMap.get(key) + "&";
		}

		try {
			URL realUrl = new URL(url);
			// 打开和URL之间的连接
			URLConnection conn = realUrl.openConnection();
			// 设置通用的请求属性
			conn.setRequestProperty("accept", "*/*");
			conn.setRequestProperty("connection", "Keep-Alive");
			conn.setRequestProperty("Accept-Charset", "utf-8");
			conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
			// 发送POST请求必须设置如下两行
			conn.setDoOutput(true);
			conn.setDoInput(true);
			// 获取URLConnection对象对应的输出流
			out = new PrintWriter(conn.getOutputStream());
			// 发送请求参数
			out.print(param);
			// flush输出流的缓冲
			out.flush();
			// 定义BufferedReader输入流来读取URL的响应
			in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
			String line;
			while ((line = in.readLine()) != null) {
				result += line;
			}
		} catch (Exception e) {
			System.out.println(e);
		}
		// 使用finally块来关闭输出流、输入流
		finally {
			try {
				if (out != null) {
					out.close();
				}
				if (in != null) {
					in.close();
				}
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}
		return result;
	}

}
package com.eq.common;

import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.LinkedHashMap;
import java.util.Map;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;

import com.alibaba.fastjson.JSONObject;
import com.eq.common.BaseConfig;
import com.eq.qrCode.QrCodeUtil;

/**
 * 生成小程序二维码
 * author : Wcb
 */
public class TwoCode {
	
	/*
     * 获取 token
   * 普通的 get 可获 token
     */
    public  static String getToken() {
        try {
 
            Map<String, String> map = new LinkedHashMap<String, String>();
            map.put("grant_type", "client_credential");
            map.put("appid",BaseConfig.eatProperties.get("wechat.appid"));//改成自己的appid
            map.put("secret",BaseConfig.eatProperties.get("wechat.secret"));
 
            String rt = UrlUtil.sendPost("https://api.weixin.qq.com/cgi-bin/token", map);
            
            JSONObject json = JSONObject.parseObject(rt);
 
            if (json.getString("access_token") != null || json.getString("access_token") != "") {
                return json.getString("access_token");
            } else {
                return null;
            }
       } catch (Exception e) {
   
            e.printStackTrace();
            return null;
        }
 
    }
    
    /**
     * 生成小程序二维码<p>
     * @param accessToken<p>
     * @param logoPath 背景图logo<p>
     * @param savePath 二维码存放路径<p>
     * @param jumpPath 扫二维码跳转的路径<p>
     */
    public static void getminiqrQrAddLogo(String accessToken,String logoPath,String savePath,String jumpPath) {
    	BufferedInputStream bis = null;
    	OutputStream os = null;
    	 try
         {
             URL url = new URL("https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token="+accessToken);
             HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
             httpURLConnection.setRequestMethod("POST");// 提交模式
             // 发送POST请求必须设置如下两行
             httpURLConnection.setDoOutput(true);
             httpURLConnection.setDoInput(true);
             // 获取URLConnection对象对应的输出流
             PrintWriter printWriter = new PrintWriter(httpURLConnection.getOutputStream());
             // 发送请求参数
             JSONObject paramJson = new JSONObject();
             paramJson.put("path",jumpPath);
             paramJson.put("width", 430);
             printWriter.write(paramJson.toString());
             // flush输出流的缓冲
             printWriter.flush();
             //开始获取数据
             bis = new BufferedInputStream(httpURLConnection.getInputStream());
             BufferedImage image =ImageIO.read(bis);
             //这一行是调用工具类换背景图的
             QrCodeUtil.drawLogo(image, logoPath, 0.3);
             os = new FileOutputStream(new File(savePath));
             ImageIO.write(image,"jpg", os);
         }
         catch (Exception e)
         {
             e.printStackTrace();
         }finally{
			try {
				if (bis != null) {
					bis.close();
				}
				if(os != null){
					os.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
         }
    }
}

猜你喜欢

转载自blog.csdn.net/AAA821/article/details/82805518