微信收款码和支付宝收款码二合一小程序
一.前端是微信小程序,后端java
二.这里讲述后端
客户扫描二维码区分来源是支付宝还是微信,然后做不同的处理;
@GetMapping("/{openid}/{codeId}") public void scan( @PathVariable String openid, @PathVariable Long codeId, HttpServletRequest request, HttpServletResponse response, Model model) throws Exception { final Code code = codeRepository.findByIdAndUserOpenid(codeId, openid); String agent = request.getHeader("User-Agent").toLowerCase(); if (agent.indexOf("micromessenger") > 0) { QrCodeUtil.encode(code.getWx(), code.getName(), response.getOutputStream()); } else if (agent.indexOf("alipayclient") > 0) { response.sendRedirect(code.getAlipay()); }else { response.sendRedirect(code.getAlipay()); } }- 如果是支付宝则直接重定向;
- 如果是微信则根据微信收款码的地址生成二维码;以下是生成二维码的代码:
-
private static final String CHARSET = "utf-8"; private static final String FORMAT = "PNG"; // 二维码尺寸 private static final int BG_WIDTH = 320; private static final int BG_HEIGHT = 570; private static final int QRCODE_SIZE = 200; private static BufferedImage createBg(String name) throws Exception { BufferedImage image = new BufferedImage(BG_WIDTH, BG_HEIGHT, BufferedImage.TYPE_INT_RGB); Graphics2D g2 = (Graphics2D)image.getGraphics(); g2.setBackground(new Color(92,147,240)); g2.clearRect(0, 0, BG_WIDTH, BG_HEIGHT); g2.setPaint(Color.WHITE); byte[] title = new String("长按二维码识别付款").getBytes("utf-8"); Font font1 = new Font("SimHei", Font.BOLD, 25); g2.setFont(font1); double x1 = (BG_WIDTH - QRCODE_SIZE) / 2; double y1 = (BG_HEIGHT - QRCODE_SIZE) / 4; g2.drawString(new String(title), (int)x1, (int)y1); Font font = new Font("SimHei", Font.PLAIN, 10); if(name == null || name.isEmpty()) { name = "***"; } name = "向 " + name + " 付款"; byte[] names = new String(name).getBytes("utf-8"); double x = (BG_WIDTH - QRCODE_SIZE); double y = BG_HEIGHT - (BG_HEIGHT - QRCODE_SIZE) / 3 - 15; Graphics2D g3 = (Graphics2D)image.getGraphics(); g3.drawString(new String(names), (int)x, (int)y); g3.setFont(font); return image; } private static BufferedImage createQrcode(String url) throws Exception { Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>(); hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); hints.put(EncodeHintType.CHARACTER_SET, CHARSET); hints.put(EncodeHintType.MARGIN, 2); BitMatrix bitMatrix = new MultiFormatWriter().encode(url, BarcodeFormat.QR_CODE, QRCODE_SIZE, QRCODE_SIZE, hints); int width = bitMatrix.getWidth(); int height = bitMatrix.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, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF); } } return image; } /** * 插入二维码 * @param bgImge 背景图片 * @param qrcode 二维码 * @return * @throws Exception */ private static BufferedImage insertImage(BufferedImage bgImge, Image qrcode) throws Exception { int width = qrcode.getWidth(null); int height = qrcode.getHeight(null); // 插入二维码 Graphics2D graph = bgImge.createGraphics(); int x = (BG_WIDTH - width) / 2; int y = (BG_HEIGHT - height) / 2; graph.drawImage(qrcode, x, y, width, height, null); graph.dispose(); return bgImge; } /** * 生成二维码(外嵌北京) * @param content * @param logoPath * @param output * @throws Exception */ public static void encode(String content, String name, OutputStream output) throws Exception { BufferedImage bgImage = QrCodeUtil.createBg(name); BufferedImage qrcode = QrCodeUtil.createQrcode(content); BufferedImage image = QrCodeUtil.insertImage(bgImage, qrcode); ImageIO.write(image, FORMAT, output); } 这个是成品。欢迎大家提意见。














