本段代码是生成微信二维码图片,并利用字节流将其输出至js页面端.也适用于其他想要获得生成动态图片的需求.可结合实际情况进行取舍.代码如下:
@RequestMapping(value="/loan/showQRcode")
public void showQRcode(HttpServletRequest request, Model model, HttpServletResponse response,
@RequestParam (value="rechargeMoney",required=true) Double rechargeMoney,
@RequestParam (value="rechargeNo",required=true) String rechargeNo,
@RequestParam (value="rechargeDesc",required=true) String rechargeDesc) throws WriterException, IOException {
//准备调用pay工程微信支付接口的请求参数
int total_fee = (int) (rechargeMoney * 100);//将单位转换为分
String out_trade_no = rechargeNo;
String body = rechargeDesc;
//发送请求 -》 获取返回信息
Map<String,Object> paramMap = new ConcurrentHashMap<String,Object>();
paramMap.put("total_fee", total_fee);
paramMap.put("out_trade_no", out_trade_no);
paramMap.put("body", body);
String result = HttpClinentUtils.doPost(config.getP2p_wxpay_pay_url(), paramMap);
//解析返回的内容
//将返回的json格式字符串转换为json对象
JSONObject jsonObject = JSON.parseObject(result);
//获取return_code
String return_code = jsonObject.getString("return_code");
if("SUCCESS".equals(return_code)) {
String result_code = jsonObject.getString("result_code");
if("SUCCESS".equals(result_code)) {
String code_url = jsonObject.getString("code_url");
int width = 200;//宽度
int height = 200;//高度
Map<EncodeHintType,Object> hints = new HashMap<EncodeHintType,Object>();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
//生成矩阵
BitMatrix bitMatrix = new MultiFormatWriter().encode(code_url, BarcodeFormat.QR_CODE, width, height, hints);
//字节数组输出流
ByteArrayOutputStream imageOut = new ByteArrayOutputStream();
MatrixToImageWriter.writeToStream(bitMatrix, "jpg", imageOut);
//字节数组输入流
ByteArrayInputStream imageIn = new ByteArrayInputStream(imageOut.toByteArray());
BufferedImage bImage = ImageIO.read(imageIn);
OutputStream out = response.getOutputStream();
ImageIO.write(bImage, "jpg", out);
bImage.flush();
out.flush();
out.close();
}
}
//获取到code_url,生成二维码
}