pom.xml | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
src/main/java/com/zy/asrs/controller/MatCodeController.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
src/main/java/com/zy/common/utils/BarcodeUtils.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
src/main/java/com/zy/common/utils/QrCode.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
src/main/webapp/views/matCode/matCode.html | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 |
pom.xml
@@ -82,6 +82,11 @@ <version>3.3.0</version> </dependency> <dependency> <groupId>com.google.zxing</groupId> <artifactId>javase</artifactId> <version>3.3.0</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>easyexcel</artifactId> <version>2.0.5</version> src/main/java/com/zy/asrs/controller/MatCodeController.java
@@ -14,6 +14,7 @@ import com.core.common.R; import com.zy.asrs.entity.MatCode; import com.zy.asrs.service.MatCodeService; import com.zy.common.utils.BarcodeUtils; import com.zy.common.utils.excel.matcode.MatCodeExcel; import com.zy.common.utils.excel.matcode.MatCodeExcelListener; import com.zy.common.web.BaseController; @@ -21,7 +22,9 @@ import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import javax.imageio.ImageIO; import javax.servlet.http.HttpServletResponse; import java.awt.image.BufferedImage; import java.io.IOException; import java.net.URLEncoder; import java.util.*; @@ -139,6 +142,20 @@ return R.ok(); } // todo resource // 物料打印 @RequestMapping(value = "/macCode/print/auth") // @ManagerAuth(memo = "物料编码打印") public R matCodePrint(HttpServletResponse response) throws IOException { BufferedImage img = BarcodeUtils.encode("123456789"); if (!ImageIO.write(img, "jpg", response.getOutputStream())) { throw new IOException("Could not write an image of format jpg"); } response.getOutputStream().flush(); response.getOutputStream().close(); return R.ok(); } @RequestMapping(value = "/matCodeQuery/auth") @ManagerAuth public R query(String condition) { src/main/java/com/zy/common/utils/BarcodeUtils.java
New file @@ -0,0 +1,72 @@ package com.zy.common.utils; import com.google.zxing.*; import com.google.zxing.client.j2se.BufferedImageLuminanceSource; import com.google.zxing.client.j2se.MatrixToImageWriter; import com.google.zxing.common.BitMatrix; import com.google.zxing.common.HybridBinarizer; import com.google.zxing.oned.Code128Writer; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.File; import java.util.HashMap; /** * 条形码工具 * Created by vincent on 2020/6/6 */ public class BarcodeUtils { private static final int DEFAULT_WIDTH = 264; private static final int DEFAULT_HEIGHT = 48; /** * 条形码编码 */ public static BufferedImage encode(String string){ return encode(string, null, null); } public static BufferedImage encode(String string, Integer width, Integer height){ try { Code128Writer writer = new Code128Writer(); BitMatrix bar = writer.encode(string, BarcodeFormat.CODE_128, width==null?DEFAULT_WIDTH:width, height==null?DEFAULT_HEIGHT:height, new HashMap<>()); return MatrixToImageWriter.toBufferedImage(bar); } catch (WriterException e){ throw new RuntimeException(e); } } /** * 条形码解码 */ public static String decode(String imgPath) { BufferedImage image = null; Result result = null; try { image = ImageIO.read(new File(imgPath)); if (image == null) { throw new RuntimeException("the decode image may be not exit."); } LuminanceSource source = new BufferedImageLuminanceSource(image); BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); result = new MultiFormatReader().decode(bitmap, null); return result.getText(); } catch (Exception e) { e.printStackTrace(); } return null; } public static void main(String[] args) { String imgPath = "F:/zxing_EAN-13.png"; String contents = "6926557300360"; int width = 105, height = 50; encode(contents, width, height); System.out.println("finished zxing EAN-13 encode."); String decodeContent = decode(imgPath); System.out.println("解码内容如下:" + decodeContent); System.out.println("finished zxing EAN-13 decode."); } } src/main/java/com/zy/common/utils/QrCode.java
@@ -22,7 +22,6 @@ // 二维码尺寸 private static final int QRCODE_SIZE = 60; public static BufferedImage createImg(String content) throws WriterException { ConcurrentHashMap hints = new ConcurrentHashMap(); hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); src/main/webapp/views/matCode/matCode.html
@@ -41,7 +41,7 @@ <script type="text/html" id="operate"> <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">详情</a> <a class="layui-btn layui-btn-xs btn-edit" lay-event="edit">编辑</a> <!-- <a class="layui-btn layui-btn-xs btn-edit" lay-event="edit">编辑</a>--> </script> <script type="text/javascript" src="/static/js/jquery/jquery-3.3.1.min.js"></script>