From 17f0c4d68b1770e490a6acd70cd769eebae4cd8f Mon Sep 17 00:00:00 2001 From: liurunyu <lry9898@163.com> Date: 星期三, 24 四月 2024 16:38:09 +0800 Subject: [PATCH] 1、封装生成二维码的公共类; 2、实现生成标识类二维码。 --- pms-parent/pms-common/src/main/java/com/dy/common/util/QrCodeGenerator.java | 190 +++++++++++++++ pms-parent/pms-common/src/main/java/com/dy/common/util/QrCodeTest2.java | 91 +++++++ pms-parent/pms-web-base/src/main/java/com/dy/pmsBase/log/LogCtrl.java | 2 pms-parent/pms-web-base/src/main/java/com/dy/pmsBase/code/ResultVo.java | 10 pms-parent/pms-common/src/main/java/com/dy/common/util/GraphicsUtils.java | 61 +++++ pms-parent/pms-global/src/main/java/com/dy/pmsGlobal/util/QrCodeConstant.java | 15 + pms-parent/pms-web-base/src/main/java/com/dy/pmsBase/log/QueryVo.java | 16 + pms-parent/pms-common/src/main/java/com/dy/common/util/QrCodeTest1.java | 172 ++++++++++++++ pms-parent/pms-web-base/src/main/java/com/dy/pmsBase/log/LogSv.java | 49 ++++ pms-parent/pms-web-base/src/main/java/com/dy/pmsBase/code/MarkCodeCtrl.java | 57 ++++ pms-parent/pms-global/src/main/resources/images/logo.png | 0 11 files changed, 661 insertions(+), 2 deletions(-) diff --git a/pms-parent/pms-common/src/main/java/com/dy/common/util/GraphicsUtils.java b/pms-parent/pms-common/src/main/java/com/dy/common/util/GraphicsUtils.java new file mode 100644 index 0000000..07d6d8d --- /dev/null +++ b/pms-parent/pms-common/src/main/java/com/dy/common/util/GraphicsUtils.java @@ -0,0 +1,61 @@ +package com.dy.common.util; + +import java.awt.*; +import java.awt.image.BufferedImage; + +public class GraphicsUtils { + + // 鍒涘缓涓�涓┖鐧界殑 BufferedImage 瀵硅薄 + public static BufferedImage createEmptyImage(int width, int height) { + return new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); + } + + // 鍦ㄥ浘鍍忎笂璁剧疆鑳屾櫙棰滆壊 + public static void drawBackground(Graphics2D g2d,int x, int y, int width, int height, Color color) { + g2d.setBackground(color); + g2d.clearRect(x, y, width, height); + } + + + // 鍦ㄥ浘鍍忎笂缁樺埗鏂囨湰 + public static void drawText(Graphics2D g2d, String text, int x, int y, Font font, Color color) { + g2d.setColor(color); + g2d.setFont(font); + g2d.drawString(text, x, y); + } + + // 鍦ㄥ浘鍍忎笂缁樺埗鐭╁舰 + public static void drawRectangle(Graphics2D g2d, int x, int y, int width, int height, Color color) { + g2d.setColor(color); + g2d.drawRect(x, y, width, height); + } + + // 鍦ㄥ浘鍍忎笂缁樺埗妞渾 + public static void drawEllipse(Graphics2D g2d, int x, int y, int width, int height, Color color) { + g2d.setColor(color); + g2d.drawOval(x, y, width, height); + } + + // 鍦ㄥ浘鍍忎笂缁樺埗绾挎 + public static void drawLine(Graphics2D g2d, int x1, int y1, int x2, int y2, Color color) { + g2d.setColor(color); + g2d.drawLine(x1, y1, x2, y2); + } + + // 鍦ㄥ浘鍍忎笂缁樺埗鍥剧墖 + public static void drawImage(Graphics2D g2d, BufferedImage imageToDraw, int x, int y) { + g2d.drawImage(imageToDraw, x, y, null); + } + + // 鍦ㄥ浘鍍忔寚瀹氬尯鍩熺粯鍒堕鑹� + public static void drawFillRect(Graphics2D g2d, int x, int y, int width, int height, Color color) { + g2d.setColor(color); + g2d.fillRect(x, y, width, height); + } + + // 鍦ㄥ浘鍍忔寚瀹氬尯鍩熺粯鍒堕鑹� + public static void drawFont(Graphics2D g2d,Font font, Color color) { + g2d.setFont(font); + g2d.setColor(color); + } +} diff --git a/pms-parent/pms-common/src/main/java/com/dy/common/util/QrCodeGenerator.java b/pms-parent/pms-common/src/main/java/com/dy/common/util/QrCodeGenerator.java new file mode 100644 index 0000000..bce628d --- /dev/null +++ b/pms-parent/pms-common/src/main/java/com/dy/common/util/QrCodeGenerator.java @@ -0,0 +1,190 @@ +package com.dy.common.util; + +import cn.hutool.core.codec.Base64; +import cn.hutool.core.util.ObjectUtil; +import cn.hutool.core.util.StrUtil; +import com.google.zxing.*; +import com.google.zxing.client.j2se.BufferedImageLuminanceSource; +import com.google.zxing.client.j2se.MatrixToImageConfig; +import com.google.zxing.client.j2se.MatrixToImageWriter; +import com.google.zxing.common.BitMatrix; +import com.google.zxing.common.HybridBinarizer; +import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; + +import javax.imageio.ImageIO; +import java.awt.*; +import java.awt.image.BufferedImage; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.IOException; +import java.util.Hashtable; + +public class QrCodeGenerator { + + + private static final String DEFAULT_CHAR_SET = "UTF-8"; + + private static final String DEFAULT_FORMAT_NAME = "JPG"; + + + // 浜岀淮鐮佸搴� + private static final int DEFAULT_QR_CODE_WIDTH = 300; + // 浜岀淮鐮侀珮搴� + private static final int DEFAULT_QR_CODE_HEIGHT = 300; + + /** + * 鍒涘缓BitMatrix姣旂壒鐭╅樀 + * @Param contents 浜岀淮鐮侀噷鐨勫唴瀹� + * @Param width 浜岀淮鐮佸搴� + * @param height 浜岀淮鐮侀珮搴� + * @return com.google.zxing.common.BitMatrix + */ + public static BitMatrix createBitMatrix(String contents, int width , int height) throws WriterException, IOException { + if (ObjectUtil.isNull(width)) { + width = DEFAULT_QR_CODE_WIDTH; + } + if (ObjectUtil.isNull(height)) { + height = DEFAULT_QR_CODE_HEIGHT; + } + + Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>(); + hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); // 绾犻敊绛夌骇L,M,Q,H + hints.put(EncodeHintType.CHARACTER_SET, DEFAULT_CHAR_SET);// 缂栫爜utf-8 + hints.put(EncodeHintType.MARGIN, 1); // 杈硅窛 + + // 鍒涘缓姣旂壒鐭╅樀 + BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, + BarcodeFormat.QR_CODE, width, height, hints); + return bitMatrix; + + } + + /** + * 鍒涘缓浜岀淮鐮侊紝杩斿洖瀛楄妭鏁扮粍 + * @Param contents 浜岀淮鐮侀噷鐨勫唴瀹� + * @Param imageFormat 鍥剧墖鍚庣紑鍚� + * @Param width 浜岀淮鐮佸搴� + * @param height 浜岀淮鐮侀珮搴� + * @return byte[] + */ + public static byte[] createQrCode(String contents , String imageFormat , int width , int height) throws WriterException, IOException { + if (StrUtil.isBlank(imageFormat)){ + imageFormat = DEFAULT_FORMAT_NAME; + } + BitMatrix bitMatrix = createBitMatrix(contents , width, height); + ByteArrayOutputStream os = new ByteArrayOutputStream(); + MatrixToImageWriter.writeToStream(bitMatrix, imageFormat, os); + return os.toByteArray(); + } + + /** + * 鍒涘缓浜岀淮鐮侊紝杩斿洖base64瀛楃涓� + * @Date 2023/09/24 22:30 + * @Param contents 浜岀淮鐮侀噷鐨勫唴瀹� + * @Param imageFormat 鍥剧墖鍚庣紑鍚� + * @Param width 浜岀淮鐮佸搴� + * @param height 浜岀淮鐮侀珮搴� + * @return byte[] + */ + public static String createQrCodeBase64(String contents , String imageFormat , int width , int height) throws WriterException, IOException { + byte[] bytes = createQrCode(contents , imageFormat , width, height); + return Base64.encode(bytes); + } + + /** + * 瑙g爜浜岀淮鐮� + * @Param [image] + * @return java.lang.String + */ + public static String decodeQrCode(BufferedImage image) throws Exception { + if (image == null) return StrUtil.EMPTY; + BufferedImageLuminanceSource source = new BufferedImageLuminanceSource(image); + BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); + Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>(); + hints.put(DecodeHintType.CHARACTER_SET, DEFAULT_CHAR_SET); + Result result = new MultiFormatReader().decode(bitmap, hints); + return result.getText(); + } + + /** + * 杞崲涓築ufferedImage + * @Param [bitMatrix] + * @return java.awt.image.BufferedImage + */ + public static BufferedImage toBufferedImage(BitMatrix bitMatrix) throws IOException, WriterException { + MatrixToImageConfig matrixToImageConfig = new MatrixToImageConfig(0xFF000001, 0xFFFFFFFF); + BufferedImage bufferedImage = MatrixToImageWriter.toBufferedImage(bitMatrix, matrixToImageConfig); + return bufferedImage; + } + + /** + * 缁欎簩缁寸爜娣诲姞logo + * @Param [bufferedImage, logoFile] + * @return java.awt.image.BufferedImage + */ + public static BufferedImage addQrCodeLogo(BufferedImage bufferedImage, File logoFile) throws IOException { + Graphics2D graphics = bufferedImage.createGraphics(); + int matrixWidth = bufferedImage.getWidth(); + int matrixHeigh = bufferedImage.getHeight(); + + // 璇诲彇logo鍥剧墖鏂囦欢 + BufferedImage logo = ImageIO.read(logoFile); + int logoWidth = logo.getWidth(); + int logoHeight = logo.getHeight(); + + // 璁$畻logo鏀剧疆浣嶇疆 + int x = bufferedImage.getWidth() / 5*2; + int y = bufferedImage.getHeight() / 5*2; + int width = matrixWidth / 5; + int height = matrixHeigh / 5; + + // 寮�濮嬬粯鍒跺浘鐗� + graphics.drawImage(logo, x, y, width, height, null); + graphics.drawRoundRect(x, y, logoWidth, logoHeight, 15, 15); + graphics.setStroke(new BasicStroke(5.0F, 1, 1)); + graphics.setColor(Color.white); + graphics.drawRect(x, y, logoWidth, logoHeight); + + graphics.dispose(); + bufferedImage.flush(); + return bufferedImage; + } + + /** + * BufferedImage杞瓧鑺傛暟缁� + * @param image + * @param imageFormat + * @return + */ + public static byte[] bufferedImageToByteArray(BufferedImage image, String imageFormat) { + if (StrUtil.isBlank(imageFormat)){ + imageFormat = DEFAULT_FORMAT_NAME; + } + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + try { + // 浣跨敤ImageIO灏咮ufferedImage鍐欏叆outputStream + ImageIO.write(image, imageFormat, outputStream); + return outputStream.toByteArray(); + } catch (IOException e) { + e.printStackTrace(); + return null; + } finally { + try { + outputStream.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + + public static void main(String[] args) throws Exception { + BufferedImage bufferedImage = toBufferedImage(createBitMatrix("https://blog.csdn.net", 300, 300)); + ImageIO.write(bufferedImage, "JPG", new File("D:/qrcode.jpg")); + + System.out.println(decodeQrCode(bufferedImage)); + + BufferedImage logoQrCode = addQrCodeLogo(bufferedImage, new File("D://logo.png")); + ImageIO.write(logoQrCode, "JPG", new File("D:/logoQrcode.jpg")); + } + +} \ No newline at end of file diff --git a/pms-parent/pms-common/src/main/java/com/dy/common/util/QrCodeTest1.java b/pms-parent/pms-common/src/main/java/com/dy/common/util/QrCodeTest1.java new file mode 100644 index 0000000..ed8442f --- /dev/null +++ b/pms-parent/pms-common/src/main/java/com/dy/common/util/QrCodeTest1.java @@ -0,0 +1,172 @@ +package com.dy.common.util; + +import com.google.zxing.BarcodeFormat; +import com.google.zxing.EncodeHintType; +import com.google.zxing.WriterException; +import com.google.zxing.client.j2se.MatrixToImageWriter; +import com.google.zxing.common.BitMatrix; +import com.google.zxing.qrcode.QRCodeWriter; +import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; + +import javax.imageio.ImageIO; +import java.awt.*; +import java.awt.image.BufferedImage; +import java.io.File; +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + +public class QrCodeTest1 { + + public static void main(String[] args) throws Exception { + + String[] text = {"12345678;瓒呭0娉㈡按琛�;姘戞椇閬�10鍙�;澶х鐮旂┒闄�;020-12345678;106.872615383,11.466578703"}; + + String imgpath = "D:\\demo\\"; + + for (int i = 0; i < text.length; i++) { + + String[] s = text[i].split(";"); + + String[] lines = {"璁惧缂栧彿:" + s[0], "璁惧绫诲瀷:" + s[1], "閬撹矾鍚嶇О:" + s[2], "绠℃姢鍗曚綅:" + s[3], "鎶ヤ慨鐢佃瘽:" + s[4]}; + String[] line = {s[0], s[1], s[2], s[3], s[4]}; + + createq(lines, line, imgpath + "test.jpeg"); + } + } + + static void createq(String[] lines, String[] line, String imgpath) { + + String qrCodeText = line[0]; + + //浜岀淮鐮佸ぇ灏� + int size = 400; + + //鐢诲竷澶у皬 + int combinedWidth = size + 800; + int combinedHeight = size + 400 ; + + //鏍囬楂樺害 + int topsize = 160; + + //璁剧疆鏍囬y鍧愭爣 + int yTitle = 110; + + //璁剧疆鐢诲竷鍐呰竟璺� + int padding = 5; + + //璁剧疆鏍囬鍐呭 + String title = "澶х闆嗗洟"; + + //璁剧疆鍥剧墖dpi + int dpi = 300; + + Color blue = new Color(75, 125, 178); + + //璁剧疆浜岀淮鐮佸弬鏁� + Map hints = new HashMap(); + + //璁剧疆UTF-8锛� 闃叉涓枃涔辩爜 + hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); + + // 璁剧疆浜岀淮鐮佺殑瀹归敊鎬� + hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); + + //鐣欑櫧 榛樿4 + hints.put(EncodeHintType.MARGIN, 1); + + // 鐮佺増鏈紝鍙栧�间负 1 鍒� 40 ,鏍规嵁浜岀淮鐮佸唴瀹瑰喅瀹氭渶浣庣増鏈� + hints.put(EncodeHintType.QR_VERSION, 3); + + //鍒涘缓鐮佷綅鍥� + BitMatrix bitMatrix = null; + try { + bitMatrix = new QRCodeWriter().encode(qrCodeText, BarcodeFormat.QR_CODE, size, size, hints); + } catch (WriterException e) { + e.printStackTrace(); + } + + //灏嗙敓鎴愮爜浣嶅浘杞崲鎴怋ufferedImage + BufferedImage qrImage = MatrixToImageWriter.toBufferedImage(bitMatrix); + + //鍒涘缓绌虹櫧BufferedImage + BufferedImage combined = new BufferedImage(combinedWidth, combinedHeight, BufferedImage.TYPE_INT_RGB); + + //浠嶣ufferedImage瀵硅薄涓幏鍙栫敾甯冿紙Graphics2D锛夊璞� + Graphics2D graphics = combined.createGraphics(); + + //璁剧疆娓叉煋鎻愮ず涓哄惎鐢ㄦ枃鏈姉閿娇 + graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); + + //榛樿寮�鍚粯鍥炬姉閿娇 + //graphics.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL,RenderingHints.VALUE_STROKE_DEFAULT); + + //璁剧疆绌虹櫧鐢诲竷鏁翠綋鑳屾櫙棰滆壊 + GraphicsUtils.drawBackground(graphics,0, 0, combinedWidth, combinedHeight,Color.WHITE); + + //缁樺埗琛ㄦ牸鐢荤嚎 + //缁樺埗绾垮湪鍐呰竟璺濆唴锛屼笉褰卞搷鍏跺畠鍐呭锛�-1鎿嶄綔 + int linepadding = padding - 1 ; + //缁樺埗鐭╁舰 + GraphicsUtils.drawRectangle(graphics,linepadding,linepadding,combinedWidth-linepadding*2,combinedHeight-linepadding*2,Color.BLACK); + //缁樺埗鍐呬笁妯嚎 + GraphicsUtils.drawLine(graphics,linepadding,padding+topsize+1,combinedWidth-linepadding,padding+topsize+1,Color.BLACK); + GraphicsUtils.drawLine(graphics,linepadding,padding+topsize+size+2,combinedWidth-linepadding,padding+topsize+size+2,Color.BLACK); + GraphicsUtils.drawLine(graphics,linepadding,combinedHeight-(combinedHeight-padding*2-topsize-size+2)/2 - padding,combinedWidth-linepadding,combinedHeight-(combinedHeight-padding*2-topsize-size+2)/2 - padding,Color.BLACK); + //缁樺埗浜岀淮鐮佺珫绾垮強鍙宠竟2妯嚎 + GraphicsUtils.drawLine(graphics,padding+size+1,padding+topsize+1,padding+size+1,padding+topsize+size+1,Color.BLACK); + GraphicsUtils.drawLine(graphics,padding+size+1,padding+topsize+1+size/3,combinedWidth-linepadding,padding+topsize+1+size/3,Color.BLACK); + GraphicsUtils.drawLine(graphics,padding+size+1,padding+topsize+1+size/3*2,combinedWidth-linepadding,padding+topsize+1+size/3*2,Color.BLACK); + + //璇ユ柟娉曠敤浜庣粯鍒跺ご閮ㄧ粯鍒剁煩褰㈠苟濉厖鎸囧畾棰滆壊 + GraphicsUtils.drawFillRect(graphics,padding, padding, combinedWidth-(padding*2), topsize,blue); + + //浜岀淮鐮佹彃鍏ョ敾甯� + GraphicsUtils.drawImage(graphics,qrImage, padding, topsize+padding+2); + + //璇ユ柟娉曠敤浜庣粯鍒跺ご閮ㄧ粯鍒剁煩褰㈠苟濉厖鎸囧畾棰滆壊 + GraphicsUtils.drawBackground(graphics,padding, padding, combinedWidth-(padding*2), topsize,blue); + + //缁樺埗澶撮儴瀛椾綋 + Font titleFont = new Font("瀹嬩綋", Font.BOLD, 70); + GraphicsUtils.drawFont(graphics,titleFont,Color.white); + FontMetrics fontMetrics = graphics.getFontMetrics(); + int fontWidth = fontMetrics.stringWidth(title); + int xTitle = ((combinedWidth - fontWidth) / 2); + GraphicsUtils.drawText(graphics,title,xTitle, yTitle+padding,titleFont,Color.white); + + //缁樺埗闈炲ご閮ㄥ瓧浣� + + //璁剧疆瀛椾綋鍙婇鑹� + Font downFont = new Font("瀹嬩綋", Font.BOLD, 50); + GraphicsUtils.drawFont(graphics,downFont,Color.BLACK); + + fontMetrics = graphics.getFontMetrics(); + + int fontHeight = fontMetrics.getHeight(); + + for (int i = 0; i < lines.length; i++) { + + if (i < 3){ + graphics.drawString(lines[i], size+padding+5, topsize+padding+ (size/3) * i + (size/3+fontHeight)/2 ); + }else if (i == 3){ + int test = combinedHeight-(combinedHeight-padding*2-topsize-size)/4*3 - padding + fontHeight/2 ; + graphics.drawString(lines[i], 5+padding, test); + } + else if (i == 4){ + int test = combinedHeight-(combinedHeight-padding*2-topsize-size)/4 - padding + fontHeight/2 ; + graphics.drawString(lines[i], 5+padding, test); + } + } + + graphics.dispose(); + + try { + ImageIO.write(combined, "jpeg",new File(imgpath) ); + } catch (IOException e) { + e.printStackTrace(); + } + + } + +} diff --git a/pms-parent/pms-common/src/main/java/com/dy/common/util/QrCodeTest2.java b/pms-parent/pms-common/src/main/java/com/dy/common/util/QrCodeTest2.java new file mode 100644 index 0000000..d858d21 --- /dev/null +++ b/pms-parent/pms-common/src/main/java/com/dy/common/util/QrCodeTest2.java @@ -0,0 +1,91 @@ +package com.dy.common.util; + +import com.google.zxing.BarcodeFormat; +import com.google.zxing.EncodeHintType; +import com.google.zxing.client.j2se.MatrixToImageWriter; +import com.google.zxing.common.BitMatrix; +import com.google.zxing.qrcode.QRCodeWriter; +import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; + +import javax.imageio.ImageIO; +import java.awt.*; +import java.awt.image.BufferedImage; +import java.io.File; +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + +public class QrCodeTest2 { + + public static void main(String[] args) throws Exception { + String imgpath = "D:\\demo\\"; + createq("1002001", "纭畾" , imgpath + "test2.jpg"); + } + + private static void createq(String code, String name, String imgpath) throws Exception{ + //浜岀淮鐮佸ぇ灏� + int size = 300; + + //璁剧疆浜岀淮鐮佸弬鏁� + Map hints = new HashMap(); + //璁剧疆UTF-8锛� 闃叉涓枃涔辩爜 + hints .put(EncodeHintType.CHARACTER_SET, "UTF-8"); + // 璁剧疆浜岀淮鐮佺殑瀹归敊鎬� + hints .put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); + //鐣欑櫧 榛樿4 + hints .put(EncodeHintType.MARGIN, 1); + // 鐮佺増鏈紝鍙栧�间负 1 鍒� 40 ,鏍规嵁浜岀淮鐮佸唴瀹瑰喅瀹氭渶浣庣増鏈� + //param.put(EncodeHintType.QR_VERSION, 3); + //鍒涘缓鐮佷綅鍥� + BitMatrix bitMatrix = new QRCodeWriter().encode(code, BarcodeFormat.QR_CODE, size, size, hints ); + + //灏嗙敓鎴愮爜浣嶅浘杞崲鎴怋ufferedImage + BufferedImage qrImage = MatrixToImageWriter.toBufferedImage(bitMatrix); + + int fontSize = 30 ; + //璁剧疆鐢诲竷鍐呰竟璺� + int padding = 5; + //鐢诲竷澶у皬 + int combinedWidth = size + padding * 2 ; + int combinedHeight = size + padding * 2 + fontSize; + + //鍒涘缓绌虹櫧BufferedImage + BufferedImage combined = new BufferedImage(combinedWidth, combinedHeight, BufferedImage.TYPE_INT_RGB); + //浠嶣ufferedImage瀵硅薄涓幏鍙栫敾甯冿紙Graphics2D锛夊璞� + Graphics2D graphics = combined.createGraphics(); + //璁剧疆娓叉煋鎻愮ず涓哄惎鐢ㄦ枃鏈姉閿娇 + graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); + //榛樿寮�鍚粯鍥炬姉閿娇 + //graphics.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL,RenderingHints.VALUE_STROKE_DEFAULT); + //璁剧疆绌虹櫧鐢诲竷鏁翠綋鑳屾櫙棰滆壊 + GraphicsUtils.drawBackground(graphics,0, 0, combinedWidth, combinedHeight,Color.WHITE); + + int linepadding = padding - 1 ; + //缁樺埗鐭╁舰 + GraphicsUtils.drawRectangle(graphics, linepadding, linepadding, + combinedWidth - linepadding * 2, + combinedHeight - linepadding * 2, Color.BLACK); + + //浜岀淮鐮佹彃鍏ョ敾甯� + GraphicsUtils.drawImage(graphics, qrImage, padding, padding); + + //缁樺埗澶撮儴瀛椾綋 + Font titleFont = new Font("瀹嬩綋", Font.BOLD, fontSize); + GraphicsUtils.drawFont(graphics,titleFont,Color.BLUE); + FontMetrics fontMetrics = graphics.getFontMetrics(); + int fontWidth = fontMetrics.stringWidth(name); + int xName = ((combinedWidth - fontWidth) / 2); + int yName = combinedHeight - padding - fontSize/2 ; + // int yName = combinedHeight - padding - fontWidth/2 ; + GraphicsUtils.drawText(graphics, name, xName, yName, titleFont, Color.BLUE); + + graphics.dispose(); + + try { + ImageIO.write(combined, "jpg",new File(imgpath) ); + } catch (IOException e) { + e.printStackTrace(); + } + + } +} diff --git a/pms-parent/pms-global/src/main/java/com/dy/pmsGlobal/util/DyCode.java b/pms-parent/pms-global/src/main/java/com/dy/pmsGlobal/util/QrCodeConstant.java similarity index 73% rename from pms-parent/pms-global/src/main/java/com/dy/pmsGlobal/util/DyCode.java rename to pms-parent/pms-global/src/main/java/com/dy/pmsGlobal/util/QrCodeConstant.java index ef7dcf1..6368073 100644 --- a/pms-parent/pms-global/src/main/java/com/dy/pmsGlobal/util/DyCode.java +++ b/pms-parent/pms-global/src/main/java/com/dy/pmsGlobal/util/QrCodeConstant.java @@ -1,6 +1,6 @@ package com.dy.pmsGlobal.util; -public final class DyCode { +public final class QrCodeConstant { ///////////////////////////////////////////// //绫诲埆缂栫爜 // ///////////////////////////////////////////// @@ -20,4 +20,17 @@ public static final String MarkWaste = "102006" ; //搴熷搧锛氳澶囩敓浜ц繃绋嬩腑鎴愪负搴熷搧锛屽疄涓哄簾寮冧簡璇ヨ澶囩紪鐮佸拰閮ㄥ垎閰嶄欢 public static final String MarkLogout = "102007" ; //娉ㄩ攢锛氱敓浜т綔涓氫汉鍛樺湪鐢熶骇鍚庢敞閿�宸ョ珯缁戝畾 + public static final int MarkQrCodeWidth = 300 ; + public static final int MarkQrCodeHeight = 300 ; + + public static String[][] Marks(){ + return new String[][]{{MarkOk, "纭畾"}, + {MarkCancel, "鍙栨秷"}, + {MarkRevoke, "鎾ら攢"}, + {MarkUnqualified, "涓嶅悎鏍�"}, + {MarkPreUnqualified, "涓婁綅涓嶅悎鏍�"}, + {MarkWaste, "搴熷搧"}, + {MarkLogout, "娉ㄩ攢"} } ; + } + } diff --git a/pms-parent/pms-global/src/main/resources/images/logo.png b/pms-parent/pms-global/src/main/resources/images/logo.png new file mode 100644 index 0000000..d48c0ca --- /dev/null +++ b/pms-parent/pms-global/src/main/resources/images/logo.png Binary files differ diff --git a/pms-parent/pms-web-base/src/main/java/com/dy/pmsBase/code/MarkCodeCtrl.java b/pms-parent/pms-web-base/src/main/java/com/dy/pmsBase/code/MarkCodeCtrl.java new file mode 100644 index 0000000..96c9e08 --- /dev/null +++ b/pms-parent/pms-web-base/src/main/java/com/dy/pmsBase/code/MarkCodeCtrl.java @@ -0,0 +1,57 @@ +package com.dy.pmsBase.code; + + +import cn.hutool.core.codec.Base64; +import com.dy.common.util.QrCodeGenerator; +import com.dy.common.webUtil.BaseResponse; +import com.dy.common.webUtil.BaseResponseUtils; +import com.dy.pmsGlobal.util.QrCodeConstant; +import lombok.extern.slf4j.Slf4j; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.awt.image.BufferedImage; +import java.io.File; +import java.net.URL; +import java.util.ArrayList; +import java.util.List; + +/** + * 鐢熸垚鏍囪瘑绫讳簩缁寸爜 + */ +@Slf4j +@RestController +@RequestMapping(path = "markCode") +@SuppressWarnings("unchecked")//java鐗堟湰瓒婇珮锛屽娉涘瀷绾︽潫瓒婁弗锛屾墍浠ラ厤缃甋uppressWarnings("unchecked") +public class MarkCodeCtrl { + + /** + * 瀹㈡埛绔姹傚緱鍒伴粯璁ゅ瘑鐮� + * @return 榛樿瀵嗙爜 + */ + @GetMapping(path = "show") + public BaseResponse<List<ResultVo>> show() { + try{ + List<ResultVo> list = new ArrayList<>() ; + URL logoUrl = MarkCodeCtrl.class.getResource("/images/logo.png") ; + String[][] marks = QrCodeConstant.Marks() ; + for(String[] mark : marks){ + ResultVo vo = new ResultVo() ; + vo.code = mark[0] ; + vo.name = mark[1] ; + BufferedImage bufferedImage = QrCodeGenerator.toBufferedImage(QrCodeGenerator.createBitMatrix(mark[0], QrCodeConstant.MarkQrCodeWidth, QrCodeConstant.MarkQrCodeHeight)); + if(logoUrl != null){ + bufferedImage = QrCodeGenerator.addQrCodeLogo(bufferedImage, new File(logoUrl.getFile())); + } + byte[] codes = QrCodeGenerator.bufferedImageToByteArray(bufferedImage, "JPG"); + vo.imgBase64 = Base64.encode(codes); + list.add(vo) ; + } + return BaseResponseUtils.buildSuccess(list); + }catch (Exception e){ + log.error("鐢熸垚鏍囪瘑绫讳簩缁寸爜鏃跺紓甯�", e); + return BaseResponseUtils.buildException(e.getMessage()); + } + } +} diff --git a/pms-parent/pms-web-base/src/main/java/com/dy/pmsBase/code/ResultVo.java b/pms-parent/pms-web-base/src/main/java/com/dy/pmsBase/code/ResultVo.java new file mode 100644 index 0000000..7185cd7 --- /dev/null +++ b/pms-parent/pms-web-base/src/main/java/com/dy/pmsBase/code/ResultVo.java @@ -0,0 +1,10 @@ +package com.dy.pmsBase.code; + +import lombok.Data; + +@Data +public class ResultVo { + public String code ; //缂栫爜 + public String name ; //鍚嶇О + public String imgBase64 ; //浜岀淮鐮佸浘鐗嘼ase64缂栫爜 +} diff --git a/pms-parent/pms-web-base/src/main/java/com/dy/pmsBase/log/LogCtrl.java b/pms-parent/pms-web-base/src/main/java/com/dy/pmsBase/log/LogCtrl.java index e0be1b7..7747f30 100644 --- a/pms-parent/pms-web-base/src/main/java/com/dy/pmsBase/log/LogCtrl.java +++ b/pms-parent/pms-web-base/src/main/java/com/dy/pmsBase/log/LogCtrl.java @@ -4,7 +4,6 @@ import com.dy.common.webUtil.BaseResponse; import com.dy.common.webUtil.BaseResponseUtils; import com.dy.common.webUtil.QueryResultVo; -import com.dy.pmsGlobal.aop.LogSv; import com.dy.pmsGlobal.aop.Log; import com.dy.pmsGlobal.pojoBa.BaLog; import lombok.extern.slf4j.Slf4j; @@ -19,6 +18,7 @@ @RequestMapping(path = "log") @SuppressWarnings("unchecked") public class LogCtrl { + private LogSv sv; @Autowired public void setSv(LogSv sv) { diff --git a/pms-parent/pms-web-base/src/main/java/com/dy/pmsBase/log/LogSv.java b/pms-parent/pms-web-base/src/main/java/com/dy/pmsBase/log/LogSv.java new file mode 100644 index 0000000..2ea768b --- /dev/null +++ b/pms-parent/pms-web-base/src/main/java/com/dy/pmsBase/log/LogSv.java @@ -0,0 +1,49 @@ +package com.dy.pmsBase.log; + +import com.dy.common.webUtil.QueryResultVo; +import com.dy.pmsGlobal.daoBa.BaLogMapper; +import com.dy.pmsGlobal.pojoBa.BaLog; +import org.apache.dubbo.common.utils.PojoUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.Date; +import java.util.List; +import java.util.Map; + +@Service +public class LogSv { + + @Autowired + private BaLogMapper dao; + + + /** + * 寰楀埌鏃ュ織 + * + * @param id 鏃ュ織ID + * @return 瀹炰綋 + */ + public BaLog selectById(Long id) { + return dao.selectByPrimaryKey(id); + } + + + /** + * 鑾峰彇鏃ュ織鍒楄〃 + */ + public QueryResultVo<List<BaLog>> selectSome(QueryVo queryVo) { + Map<String, Object> params = (Map<String, Object>) PojoUtils.generalize(queryVo); + + //鏌ヨ绗﹀悎鏉′欢鐨勮褰曟�绘暟 + Long itemTotal = dao.selectSomeCount(params); + + QueryResultVo<List<BaLog>> rsVo = new QueryResultVo<>(queryVo.pageSize, queryVo.pageCurr) ; + //璁$畻鍒嗛〉绛変俊鎭� + rsVo.calculateAndSet(itemTotal, params); + + //鏌ヨ绗﹀悎鏉′欢鐨勮褰� + rsVo.obj = this.dao.selectSome(params) ; + return rsVo ; + } +} diff --git a/pms-parent/pms-web-base/src/main/java/com/dy/pmsBase/log/QueryVo.java b/pms-parent/pms-web-base/src/main/java/com/dy/pmsBase/log/QueryVo.java new file mode 100644 index 0000000..735b946 --- /dev/null +++ b/pms-parent/pms-web-base/src/main/java/com/dy/pmsBase/log/QueryVo.java @@ -0,0 +1,16 @@ +package com.dy.pmsBase.log; + + +import com.dy.common.webUtil.QueryConditionVo; +import lombok.*; + +@Data +@EqualsAndHashCode(callSuper = false) +@ToString(callSuper = true) +@NoArgsConstructor +@AllArgsConstructor +@Builder +public class QueryVo extends QueryConditionVo { + public String name; +} + -- Gitblit v1.8.0