Java 创建/识别条形码-动态焦点

2023-05-24 23:39:17博客园


(资料图)

项目刚好需要用到就记录一下

-- 依赖

net.sf.barcode4jbarcode4j2.1org.apache.avalon.frameworkavalon-framework-api4.3.1-- 工具类
package com.example.mybatis_plus;import org.apache.avalon.framework.configuration.ConfigurationException;import org.apache.avalon.framework.configuration.DefaultConfiguration;import org.apache.commons.io.FileUtils;import org.krysalis.barcode4j.BarcodeException;import org.krysalis.barcode4j.BarcodeGenerator;import org.krysalis.barcode4j.BarcodeUtil;import org.krysalis.barcode4j.HumanReadablePlacement;import org.krysalis.barcode4j.impl.code128.Code128Bean;import org.krysalis.barcode4j.output.bitmap.BitmapCanvasProvider;import java.awt.image.BufferedImage;import java.io.*;/** * 条形码工具类 */public class BarCodeUtils {/**************************** 条形码  ********************************/    /**     * 生成code128条形码     *     * @param message       要生成的文本     * @param withQuietZone 是否两边留白     * @param hideText      隐藏可读文本     * @param filePath      文件生成的路径     */    public static void generateBarCode128(String message, boolean withQuietZone, boolean hideText, String filePath) {        Code128Bean bean = new Code128Bean();        // 分辨率        int dpi = 512;        // 设置两侧是否留白        bean.doQuietZone(withQuietZone);        // 设置条形码高度和宽度(不填就是默认的)//        bean.setBarHeight((double) ObjectUtils.defaultIfNull(height, 9.0D));//        if (width != null) {//            bean.setModuleWidth(width);//        }        // 设置文本位置(包括是否显示)        if (hideText) {            bean.setMsgPosition(HumanReadablePlacement.HRP_NONE);        }        // 设置图片类型        String format = "image/png";        ByteArrayOutputStream ous = new ByteArrayOutputStream();        BitmapCanvasProvider canvas = new BitmapCanvasProvider(ous, format, dpi,                BufferedImage.TYPE_BYTE_BINARY, false, 0);        // 生产条形码        bean.generateBarcode(canvas, message);        try {            canvas.finish();            //字节码            byte[] bytes = ous.toByteArray();            FileUtils.writeByteArrayToFile(new File(filePath), bytes);        } catch (IOException e) {            e.printStackTrace();        }    }    /**************************** 二维码  ********************************/    /**     * 生成二维码     * @param message       要生成的文本     * @param filePath      文件生成的路径     */    public static void genQRCode(String message, String filePath){        try {            BarcodeUtil util = BarcodeUtil.getInstance();            DefaultConfiguration cfg = new DefaultConfiguration("barcode");            // Bar code type            DefaultConfiguration child = new DefaultConfiguration("datamatrix");            cfg.addChild(child);// Human readable text position            DefaultConfiguration attr = new DefaultConfiguration("human-readable");//        //设置高度(无效果)//        attr = new DefaultConfiguration("height");//        attr.setValue(50);//        child.addChild(attr);            //设置二维码宽度            attr = new DefaultConfiguration("module-width");            attr.setValue("2.0");            child.addChild(attr);            BarcodeGenerator gen = util.createBarcodeGenerator(cfg);            //分辨率            int resolution = 300;            // 设置图片类型            String format = "image/png";            ByteArrayOutputStream ous = new ByteArrayOutputStream();            BitmapCanvasProvider canvas = new BitmapCanvasProvider(ous, format, resolution,                    BufferedImage.TYPE_BYTE_BINARY, false, 0);            gen.generateBarcode(canvas, message);            canvas.finish();            //字节码            byte[] bytes = ous.toByteArray();            FileUtils.writeByteArrayToFile(new File(filePath), bytes);        } catch (ConfigurationException e) {            e.printStackTrace();        } catch (BarcodeException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }    }}-- 测试方法
package com.example.mybatis_plus;public class demo1 {    public static void main(String[] args) {        BarCodeUtils.generateBarCode128("SP0012", true, false,"D:\\aaab.jpg");    }}
--文档https://www.cnblogs.com/Yesi/p/11527369.html官方地址:http://barcode4j.sourceforge.net/嵌入到 Java 应用程序中(使用 JavaBean API) (sourceforge.net)https://barcode4j.sourceforge.net/2.1/embedding-bean.html

标签:

企业

央企接盘三年仍亏损 海南海药上半年减亏23亿元
2022-08-08
详细行程
厦门市细化落实《厦门经济特区优化营商环境条例》
2022-06-20
详细行程
2022互联网岳麓峰会因防控原因延期召开
2022-03-21
详细行程