生成条形码和打印条形码

package org.hengyi.modules.tdmMonthPlan.utils;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.oned.Code128Writer;
import net.sourceforge.barbecue.Barcode;
import net.sourceforge.barbecue.BarcodeFactory;
import net.sourceforge.barbecue.BarcodeImageHandler;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Base64;
import java.util.Hashtable;
@Component
public class BarcodeGenerator {

    private static final String router = "data:image/png;base64,";

    private static String upLoadPath;

    @Value("${jeecg.path.upload}")
    public void setConfigValue(String value) {
        this.upLoadPath = value;
    }

    /**
     * 生成Base64的条形码
     * @param code 转工单_提交单号
     * @return
     */
    public static String generateBarcode(String code) {
        int width = 200;
        int height = 50;

        Hashtable<EncodeHintType, String> hintMap = new Hashtable<>();
        hintMap.put(EncodeHintType.CHARACTER_SET, "UTF-8");

        try {
            BitMatrix bitMatrix = new Code128Writer().encode(code, BarcodeFormat.CODE_128, width, height, hintMap);
            BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

            for (int x = 0; x < width; x++) {
                for (int y = 0; y < height; y++) {
                    image.setRGB(x, y, bitMatrix.get(x, y) ? Color.BLACK.getRGB() : Color.WHITE.getRGB());
                }
            }

            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            ImageIO.write(image, "png", byteArrayOutputStream);
            byte[] byteArray = byteArrayOutputStream.toByteArray();

            return router + Base64.getEncoder().encodeToString(byteArray);
        } catch (WriterException | IOException e) {
            e.printStackTrace();
            return null;
        }
    }


    /**
     * 生成打印条形码
     * @param code
     * @return
     */
    public static String generateReportBarCode(String code){
        byte[] data = null;
        try {
            // 生成条形码
            Barcode barcode = BarcodeFactory.createCode128(code);
            String signImgUrl = upLoadPath + "/monthImg/barcode.png";
            barcode.setDrawingText(true);
            File file = new File(signImgUrl);
            BarcodeImageHandler.savePNG(barcode, file);

            FileInputStream fileInputStream = new FileInputStream(file);
            int available = fileInputStream.available();
            data = new byte[available];
            fileInputStream.read(data);
            fileInputStream.close();
            return router + Base64.getEncoder().encodeToString(data);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值