From 697da8e94d2609474492d0c2f0b9f5d0f8e4fbad Mon Sep 17 00:00:00 2001 From: "JinLu.Yin" <411641505@qq.com> Date: Sun, 31 Jul 2022 22:29:16 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=9D=A1=E7=A0=81=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ktg/common/utils/barcode/BarcodeUtil.java | 68 +++++++++++++++++++ ktg-mes/pom.xml | 6 +- 2 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 ktg-common/src/main/java/com/ktg/common/utils/barcode/BarcodeUtil.java diff --git a/ktg-common/src/main/java/com/ktg/common/utils/barcode/BarcodeUtil.java b/ktg-common/src/main/java/com/ktg/common/utils/barcode/BarcodeUtil.java new file mode 100644 index 0000000..4ff582a --- /dev/null +++ b/ktg-common/src/main/java/com/ktg/common/utils/barcode/BarcodeUtil.java @@ -0,0 +1,68 @@ +package com.ktg.common.utils.barcode; + +import com.ktg.common.utils.StringUtils; +import org.krysalis.barcode4j.impl.code39.Code39Bean; +import org.krysalis.barcode4j.output.bitmap.BitmapCanvasProvider; +import org.krysalis.barcode4j.tools.UnitConv; + +import java.io.ByteArrayOutputStream; +import java.awt.image.BufferedImage; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.io.File; + +public class BarcodeUtil { + + public static File generateFile(String msg, String path) { + File file = new File(path); + try { + generate(msg, new FileOutputStream(file)); + } catch (FileNotFoundException e) { + throw new RuntimeException(e); + } + return file; + } + + public static byte[] generate(String msg) { + ByteArrayOutputStream ous = new ByteArrayOutputStream(); + generate(msg, ous); + return ous.toByteArray(); + } + + + public static void generate(String msg, OutputStream ous) { + if (StringUtils.isEmpty(msg) || ous == null) { + return; + } + + Code39Bean bean = new Code39Bean(); + + // 精细度 + final int dpi = 150; + // module宽度 + final double moduleWidth = UnitConv.in2mm(1.0f / dpi); + + // 配置对象 + bean.setModuleWidth(moduleWidth); + bean.setWideFactor(3); + bean.doQuietZone(false); + + String format = "image/png"; + try { + + // 输出到流 + BitmapCanvasProvider canvas = new BitmapCanvasProvider(ous, format, dpi, + BufferedImage.TYPE_BYTE_BINARY, false, 0); + + // 生成条形码 + bean.generateBarcode(canvas, msg); + + // 结束绘制 + canvas.finish(); + } catch (IOException e) { + throw new RuntimeException(e); + } + } +} diff --git a/ktg-mes/pom.xml b/ktg-mes/pom.xml index 8e5c264..9c0b487 100644 --- a/ktg-mes/pom.xml +++ b/ktg-mes/pom.xml @@ -47,7 +47,11 @@ com.ktg ktg-framework - + + io.swagger + swagger-models + 1.6.2 + \ No newline at end of file