Barcode生成条形码

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Base64;

import javax.imageio.ImageIO;

import org.jbarcode.encode.BarSet;
import org.jbarcode.encode.BarcodeEncoder;
import org.jbarcode.encode.Code128Encoder;
import org.jbarcode.encode.EAN13Encoder;
import org.jbarcode.encode.InvalidAtributeException;

public class BarcodeUtil {

    private int width;
    private int barWidth;
    private double ratio;
    private BufferedImage img;
    private Boolean showCodeText = false;
    private Boolean showLabel = false;

    public BarcodeUtil(int barWidth, int ratio) {
        this.barWidth = barWidth;
        this.ratio = ratio;
    }

    public BarcodeUtil(int barWidth) {
        this(barWidth, 0);
    }

    public BarcodeUtil() {
        this(1, 0);
    }

    public int getWidth() {
        return this.width;
    }
    
    public double getRatio() {
        return this.ratio;
    }

    public void setBarWidth(int barWidth) {
        this.barWidth = barWidth;
    }
    
    public void setRatio(double ratio) {
        this.ratio = ratio;
    }

    public BufferedImage getImage() {
        return this.img;
    }
   
    public String createCode(String moduleNumber, String label) throws InvalidAtributeException, IOException {
        BarcodeEncoder encoder;
        String code = moduleNumber;
        if (12 == moduleNumber.length()) {
            encoder = EAN13Encoder.getInstance();
            code += encoder.computeCheckSum(moduleNumber);
        } else {
            encoder = Code128Encoder.getInstance();
        }
        BarSet [] encoded = encoder.encode(code);
        img = paintBar(encoded, barWidth, ratio);
        if (label.isEmpty()||label.equals("-")) paintCodeText(code, 8);
        else paintLabel(label, 10);
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ImageIO.write(img, "BMP", outputStream);
        byte[] bytes = outputStream.toByteArray();
        return Base64.getEncoder().encodeToString(bytes).trim();
    }
    
    public void createCode(String moduleNumber) throws InvalidAtributeException, IOException {
        createCode(moduleNumber, "");
    }

    public BufferedImage paintBar(BarSet[] barcode, int barWidth, double ratio) {
        width = 0;
        for (int i = 0; i < barcode.length; i++) {
            width += barcode[i].length();
        }
        width *= barWidth;
        if (0 >= ratio) ratio = 0.54;
        int barHeight = (int)(ratio * width);
        BufferedImage result = new BufferedImage(width, barHeight, BufferedImage.TYPE_BYTE_BINARY);
        Graphics2D g2d = result.createGraphics();
        int x = 0;
        for (int i = 0; i < barcode.length; i++) {
            for (int j = 0; j < barcode[i].length(); j++) {
                g2d.setColor(barcode[i].get(j) ? Color.BLACK : Color.WHITE);
                g2d.fillRect(x, 0, barWidth, barHeight);
                x += barWidth;
            }
        }
        g2d.dispose();
        return result;
    }

    public void paintCodeText(String code, int fontSize) {
        showCodeText = true;
        if (13 != code.length()) fontSize = fontSize * code.length() / 14;
        Font font = new Font("monospace", Font.PLAIN, fontSize * barWidth);
        Graphics g2d = img.getGraphics();
        g2d.setFont(font);
        FontMetrics fontMetrics = g2d.getFontMetrics();
        if (13 == code.length()) {
            int digitWidht = barWidth * 7;
            int textHeight = fontMetrics.getAscent();
            int textXShift = (digitWidht - fontMetrics.stringWidth("0"))/2;
            int textYPos = img.getHeight() - textHeight*3/10;
            int y = img.getHeight() - textHeight*6/5;
            g2d.setColor(Color.WHITE);
            g2d.fillRect(0, img.getHeight() - textHeight*3/5, img.getWidth(), textHeight*3/5);
            if (!showLabel) g2d.fillRect(0, 0, img.getWidth(), img.getHeight()*1/20);
            g2d.fillRect(barWidth * 11, y, digitWidht*6,  textHeight);
            g2d.fillRect(barWidth * 57, y, digitWidht*6,  textHeight);
            g2d.setColor(Color.BLACK);
            g2d.drawString(code.substring(0, 1), 8 * barWidth - fontMetrics.stringWidth("0") - textXShift, textYPos);
            for (int i=1; i<13; i++) {
                int textXPos = ((i < 7 ? 4 : 8) + 7*i) * barWidth + textXShift;
                g2d.drawString("" + code.charAt(i), textXPos, textYPos);
            }
        } else {
            int textWidth = fontMetrics.stringWidth(code);
            int textHeight = fontMetrics.getAscent();
            int textYPos = img.getHeight() - textHeight * 3 / 10;
            g2d.setColor(Color.WHITE);
            g2d.fillRect(0, img.getHeight() - textHeight * 6 / 5, img.getWidth(), textHeight * 6 / 5);
            if (!showLabel) g2d.fillRect(0, 0, img.getWidth(), img.getHeight() * 1 / 20);
            g2d.setColor(Color.BLACK);
            g2d.drawString(code, (img.getWidth() - textWidth) / 2, textYPos);
        }
        g2d.dispose();
    }
    
    public void paintLabel(String label, int fontSize) {
        showLabel = true;
        if (fontSize < 3) return;
        Font font = new Font("monospace", Font.PLAIN, fontSize * barWidth);
        Graphics g2d = img.getGraphics();
        g2d.setFont(font);
        FontMetrics fontMetrics = g2d.getFontMetrics();
        int textWidth = fontMetrics.stringWidth(label);
        if (img.getWidth() - textWidth < 8 * barWidth) {
            paintLabel(label, fontSize - 1);
            return;
        }
        int textHeight = fontMetrics.getAscent();
        g2d.setColor(Color.WHITE);
        g2d.fillRect(0, 0, img.getWidth(), textHeight*7/5+2*barWidth);
        if (!showCodeText) g2d.fillRect(0, img.getHeight() - 4*barWidth, img.getWidth(), 4*barWidth);
        g2d.setColor(Color.BLACK);
        g2d.drawString(label, (img.getWidth() - textWidth)/2, textHeight*11/10+barWidth*3/2);
        g2d.dispose();
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

开源字节

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值