微信长连接转短连接+谷歌zxing 生成二维码

Controller

@RequestMapping("/TwoCode") 
	public void TwoCode(HttpServletRequest request, HttpServletResponse response)  
            throws Exception {
		String id = request.getParameter("id");
		//调用微信接口,使长连接转成短连接,二维码更简单,用户扫码更快速
		String accessToken = "XXXXXX";
		StringBuffer sb = new StringBuffer("https://api.weixin.qq.com/cgi-bin/shorturl");
		sb.append("?access_token=" + accessToken);
		URL sendUrl = null;
		HttpURLConnection connection1 = null;
		String lines1 = null;
		try {
			sendUrl = new URL(sb.toString());
			connection1 = (HttpURLConnection) sendUrl.openConnection();
			connection1.setRequestMethod("POST");
			connection1.setDoOutput(true);
			connection1.setDoInput(true);
			connection1.setUseCaches(false);
			connection1.setInstanceFollowRedirects(true);
			connection1.setRequestProperty("Content-Type","application/json; charset=UTF-8");  
			connection1.connect();
			OutputStream out = connection1.getOutputStream();
			JSONObject obj = new JSONObject();
			obj.put("action", "long2short"); 
                        //obj.put("long_url", "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxe7be4c18c8a47149&redirect_uri=http%3a%2f%2fzctx919.com%2fld_supplier_ms%2fweChat%2fwXScanCallBack&response_type=code&scope=snsapi_userinfo&state=" + shopperId + "#wechat_redirect");
			obj.put("long_url", "http://XXXXXX?id=" + id);//扫描二维码之后要跳转的路径
			out.write(obj.toString().getBytes("UTF-8"));//这样可以处理中文乱码问题
			out.flush();
			out.close();
			BufferedReader reader1 = new BufferedReader(new InputStreamReader(connection1.getInputStream(),"utf-8"));//设置编码,否则中文乱码
			String flag = null;
			if((lines1 = reader1.readLine()) != null) {
				flag += lines1;
				System.out.println("获取二维码的ticket" + lines1);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		JSONObject jsonObj = JSONObject.parseObject(lines1); 
		//生成二维码
		String code = jsonObj.getString("short_url");
		if(code != null){
	        int width = 300;
	        int height = 300;
	        //二维码的图片格式 
	        String format = "gif";
	        Hashtable hints = new Hashtable();
	        //内容所使用编码 
	        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
	        BitMatrix bitMatrix = new MultiFormatWriter().encode(code, BarcodeFormat.QR_CODE, width, height, hints); 
	        //把二维码临时放到tomcat的docs文件夹里
	        String docsPath = request.getSession().getServletContext().getRealPath("docs");
		String filePath = docsPath + File.separator + id+".gif";
		//直接写入文件  
	        File outputFile = new File(filePath);
	        QRUtil.writeToFile(bitMatrix, format, outputFile);
	        //把路径放到session里,然后去下载
	        HttpSession session = request.getSession();
		session.setAttribute("filePath", filePath);
		}
	}
工具类

import com.google.zxing.common.BitMatrix; 
import javax.imageio.ImageIO; 
import java.io.File; 
import java.io.OutputStream; 
import java.io.IOException; 
import java.awt.image.BufferedImage;

public class QRUtil {
	
   private static final int BLACK = 0xFF000000; 
   private static final int WHITE = 0xFFFFFFFF; 
    
   public static BufferedImage toBufferedImage(BitMatrix matrix) { 
     int width = matrix.getWidth(); 
     int height = matrix.getHeight(); 
     BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); 
     for (int x = 0; x < width; x++) { 
       for (int y = 0; y < height; y++) { 
         image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE); 
       } 
     } 
     return image; 
   } 
    
      
   public static void writeToFile(BitMatrix matrix, String format, File file) 
       throws IOException { 
     BufferedImage image = toBufferedImage(matrix); 
     if (!ImageIO.write(image, format, file)) { 
       throw new IOException("Could not write an image of format " + format + " to " + file); 
     } 
   } 
    
      
   public static void writeToStream(BitMatrix matrix, String format, OutputStream stream) 
       throws IOException { 
     BufferedImage image = toBufferedImage(matrix); 
     if (!ImageIO.write(image, format, stream)) { 
       throw new IOException("Could not write an image of format " + format); 
     } 
   } 
}

二维码的生成细节和原理:http://coolshell.cn/articles/10590.html
微信开大文档,长连接转短连接:https://mp.weixin.qq.com/wiki/6/856aaeb492026466277ea39233dc23ee.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值