Itext 5.0 怎么生成显示条形码

 官方的demo中有条形码的工具,但是生成出来的样式不好调整,这里还是放出官方的demo地址,大家可以test试试:https://itextpdf.com/en/resources/examples/itext-5-legacy/generating-and-displaying-bar-codes

怎么使用我们自己生成的条形码呢?

安装依赖

<properties>
        <zxing.version>3.3.3</zxing.version>
    </properties>

<!-- google zxing 多格式1D / 2D条码图像处理库-->
        <dependency>
            <groupId>com.google.zxing</groupId>
            <artifactId>core</artifactId>
            <version>${zxing.version}</version>
        </dependency>
        <dependency>
            <groupId>com.google.zxing</groupId>
            <artifactId>javase</artifactId>
            <version>${zxing.version}</version>
        </dependency>

封装工具:

public class BarCodeUtil {
    /**
     * 获取base64格式条形码
     * @param contents
     * @return
     * @throws WriterException
     * @throws IOException
     */
    public static String getBase64Encode(String contents) throws WriterException, IOException {
        return new String("data:image/jpeg;base64,"+Base64.encode(getBytes(contents)));
    }

    /**
     * 获取字节码
     * @param contents
     * @return
     * @throws WriterException
     * @throws IOException
     */
    public static byte[] getBytes(String contents) throws WriterException, IOException {
        BufferedImage bufferedImage = encode(contents);
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ImageIO.write(bufferedImage,"jpeg",outputStream);
        outputStream.close();
        return outputStream.toByteArray();
    }

    /**
     * 生成条形码
     * @param contents 内容
     * @return
     */
    public static BufferedImage encode(String contents) throws WriterException {
        //配置参数
        Map<EncodeHintType,Object> hints = new HashMap<>();
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        // 容错级别 这里选择最高H级别
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        //设置条码边距 默认为10
        hints.put(EncodeHintType.MARGIN, 0);
        //宽度为条码自动生成规则的宽度
        int width = new Code128Writer().encode(contents).length;
        //高度可由前端控制 不影响编码识别
        int height = 70;
        //条码放大倍数
        int codeMultiples = 1;
        //条码内容宽度
        int codeWidth = width * codeMultiples;
        // 参数分别为 编码内容,编码类型,内容宽度,内容高度,设置参数
        BitMatrix bitMatrix = new MultiFormatWriter().encode(contents,
                BarcodeFormat.CODE_128, codeWidth, height, hints);
        return  MatrixToImageWriter.toBufferedImage(bitMatrix);
    }

    /**
     * 解析条形码
     * @param inputStream
     * @return
     */
    public static String decode(InputStream inputStream) {
        BufferedImage image = null;
        Result result = null;
        try {
            image = ImageIO.read(inputStream);
            if (image == null) {
                throw new RuntimeException("the decode image may be not exists.");
            }
            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;
    }

}

 在Itext5中根据数据生成对应条形码,这样生成条形码的时候,调整起来方便很多

// 条形码
Image barCodeImg = Image.getInstance(BarCodeUtil.getBytes(no));
barCodeImg.scaleAbsolute(120, 40);
cell = new PdfPCell(barCodeImg, false);

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章