From 68ce3e9b15010bcdc544642bca403ca4381047dc Mon Sep 17 00:00:00 2001 From: wenc000 <450292408@qq.com> Date: Mon, 26 Aug 2019 18:02:17 +0800 Subject: [PATCH] =?UTF-8?q?APP=E4=B8=8B=E8=BD=BD=E6=94=BE=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/cm/common/filter/AppFilter.java | 3 +- .../java/com/cm/common/utils/QRCodeUtil.java | 86 +++++++++++++++++++ pom.xml | 9 +- 3 files changed, 96 insertions(+), 2 deletions(-) create mode 100644 cloud-common/src/main/java/com/cm/common/utils/QRCodeUtil.java diff --git a/cloud-common/src/main/java/com/cm/common/filter/AppFilter.java b/cloud-common/src/main/java/com/cm/common/filter/AppFilter.java index db569e0..7eafd46 100644 --- a/cloud-common/src/main/java/com/cm/common/filter/AppFilter.java +++ b/cloud-common/src/main/java/com/cm/common/filter/AppFilter.java @@ -34,6 +34,7 @@ public class AppFilter implements Filter { private static final Logger LOG = LoggerFactory.getLogger(AppFilter.class); private AntPathMatcher antPathMatcher; private static final String URL_LOGIN = "/app/sign/login"; + private static final String URL_DOWNLOAD_APP = "/app/appversion/downloadapp/**"; @Override public void init(FilterConfig filterConfig) throws ServletException { @@ -45,7 +46,7 @@ public class AppFilter implements Filter { HttpServletRequest request = (HttpServletRequest) servletRequest; HttpServletResponse response = (HttpServletResponse) servletResponse; String requestUri = request.getRequestURI(); - if (antPathMatcher.match(URL_LOGIN, requestUri)) { + if (antPathMatcher.match(URL_LOGIN, requestUri) || antPathMatcher.match(URL_DOWNLOAD_APP, requestUri)) { filterChain.doFilter(request, response); return; } diff --git a/cloud-common/src/main/java/com/cm/common/utils/QRCodeUtil.java b/cloud-common/src/main/java/com/cm/common/utils/QRCodeUtil.java new file mode 100644 index 0000000..58085fa --- /dev/null +++ b/cloud-common/src/main/java/com/cm/common/utils/QRCodeUtil.java @@ -0,0 +1,86 @@ +package com.cm.common.utils; + +import java.awt.Color; +import java.awt.Graphics2D; +import java.awt.Image; +import java.awt.RenderingHints; +import java.awt.image.BufferedImage; +import java.io.File; +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + +import javax.imageio.ImageIO; + +import com.google.zxing.BarcodeFormat; +import com.google.zxing.EncodeHintType; +import com.google.zxing.MultiFormatWriter; +import com.google.zxing.common.BitMatrix; +import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; + +public class QRCodeUtil { + + public static BufferedImage createQrCode(int width, int height, String content, String logoPath) { + Map qrParams = new HashMap<>(3); + // 编码 + qrParams.put(EncodeHintType.CHARACTER_SET, "UTF-8"); + // 纠错等级 + qrParams.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L); + // 边框 + qrParams.put(EncodeHintType.MARGIN, 1); + // 生成二维码 + BufferedImage bufferedImage = null; + try { + BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, + width, height); + bufferedImage = getBufferImage(bitMatrix); + if (null != logoPath && !logoPath.isEmpty()) { + addLogo(bufferedImage, logoPath); + } + } catch (Exception e) { + e.printStackTrace(); + } + return bufferedImage; + } + + private static BufferedImage getBufferImage(BitMatrix bitMatrix) { + int bmWidth = bitMatrix.getWidth(); + int bmHeight = bitMatrix.getHeight(); + BufferedImage bufferedImage = new BufferedImage(bmWidth, bmHeight, + BufferedImage.TYPE_INT_ARGB); + for (int x = 0; x < bmWidth; x++) { + for (int y = 0; y < bmHeight; y++) { + bufferedImage.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF); + } + } + return bufferedImage; + } + + private static void addLogo(BufferedImage bufferedImage, String logoPath) throws IOException { + Graphics2D graphics2d = bufferedImage.createGraphics(); + Image logo = ImageIO.read(new File(logoPath)); + // 设置比例 + int logoWidth = bufferedImage.getWidth() / 6; + int logoHeight = bufferedImage.getHeight() / 6; + + // 填充logo + int x = bufferedImage.getWidth() / 2 - logoWidth / 2; + int y = bufferedImage.getHeight() / 2 - logoHeight / 2; + // 设置抗锯齿 + graphics2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, + RenderingHints.VALUE_ANTIALIAS_ON); + // 填充填充背景 + graphics2d.setColor(Color.WHITE); + graphics2d.fillRoundRect(x, y, logoWidth, logoHeight, 10, 10); + + // 画边框 + graphics2d.setColor(new Color(220, 220, 220)); + graphics2d.drawRoundRect(x, y, logoWidth, logoHeight, 10, 10); + + // 画LOGO + graphics2d.drawImage(logo, x + 2, y + 2, logoWidth - 4, logoHeight - 4, null); + graphics2d.dispose(); + logo.flush(); + } + +} diff --git a/pom.xml b/pom.xml index 8ccbf54..81ad12e 100644 --- a/pom.xml +++ b/pom.xml @@ -52,8 +52,8 @@ 0.4.8 5.1.47 2.9.0 - 2.5.0 + 3.3.3 @@ -309,6 +309,13 @@ ${swagger.version} + + + com.google.zxing + core + ${zxing.version} + +