1、封装生成二维码的公共类;
2、实现生成标识类二维码。
New file |
| | |
| | | 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); |
| | | } |
| | | } |
New file |
| | |
| | | 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); |
| | | } |
| | | |
| | | /** |
| | | * 解码二维码 |
| | | * @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(); |
| | | } |
| | | |
| | | /** |
| | | * 转换为BufferedImage |
| | | * @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将BufferedImage写入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")); |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | 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;超声波水表;民旺道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(); |
| | | } |
| | | |
| | | //将生成码位图转换成BufferedImage |
| | | BufferedImage qrImage = MatrixToImageWriter.toBufferedImage(bitMatrix); |
| | | |
| | | //创建空白BufferedImage |
| | | BufferedImage combined = new BufferedImage(combinedWidth, combinedHeight, BufferedImage.TYPE_INT_RGB); |
| | | |
| | | //从BufferedImage对象中获取画布(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(); |
| | | } |
| | | |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | 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 ); |
| | | |
| | | //将生成码位图转换成BufferedImage |
| | | 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); |
| | | //从BufferedImage对象中获取画布(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(); |
| | | } |
| | | |
| | | } |
| | | } |
File was renamed from pms-parent/pms-global/src/main/java/com/dy/pmsGlobal/util/DyCode.java |
| | |
| | | package com.dy.pmsGlobal.util; |
| | | |
| | | public final class DyCode { |
| | | public final class QrCodeConstant { |
| | | ///////////////////////////////////////////// |
| | | //类别编码 // |
| | | ///////////////////////////////////////////// |
| | |
| | | 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, "注销"} } ; |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | 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版本越高,对泛型约束越严,所以配置SuppressWarnings("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()); |
| | | } |
| | | } |
| | | } |
New file |
| | |
| | | package com.dy.pmsBase.code; |
| | | |
| | | import lombok.Data; |
| | | |
| | | @Data |
| | | public class ResultVo { |
| | | public String code ; //编码 |
| | | public String name ; //名称 |
| | | public String imgBase64 ; //二维码图片base64编码 |
| | | } |
| | |
| | | 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; |
| | |
| | | @RequestMapping(path = "log") |
| | | @SuppressWarnings("unchecked") |
| | | public class LogCtrl { |
| | | |
| | | private LogSv sv; |
| | | @Autowired |
| | | public void setSv(LogSv sv) { |
New file |
| | |
| | | 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 ; |
| | | } |
| | | } |
New file |
| | |
| | | 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; |
| | | } |
| | | |