| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- package com.happy.Unitil_nsh;
- import com.google.zxing.BarcodeFormat;
- import com.google.zxing.EncodeHintType;
- import com.google.zxing.MultiFormatWriter;
- import com.google.zxing.client.j2se.MatrixToImageWriter;
- import com.google.zxing.common.BitMatrix;
- import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
- import javax.servlet.ServletOutputStream;
- import javax.servlet.http.HttpServletResponse;
- import java.util.HashMap;
- import java.util.Map;
- /**
- * 微信公众号接口工具类
- * @author lujunjie
- * @date 2018/03/01
- */
- public class WxUtil {
- /**
- * 生成支付二维码
- * @param response 响应
- * @param contents url链接
- * @throws Exception
- */
- public static void writerPayImage(HttpServletResponse response, String contents) throws Exception{
- ServletOutputStream out = response.getOutputStream();
- try {
- Map<EncodeHintType,Object> hints = new HashMap<EncodeHintType,Object>();
- hints.put(EncodeHintType.CHARACTER_SET,"UTF-8");
- hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
- hints.put(EncodeHintType.MARGIN, 0);
- BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, BarcodeFormat.QR_CODE,300,300,hints);
- MatrixToImageWriter.writeToStream(bitMatrix,"jpg",out);
- }catch (Exception e){
- throw new Exception("生成二维码失败!");
- }finally {
- if(out != null){
- out.flush();
- out.close();
- }
- }
- }
- }
|