大战熟女丰满人妻av-荡女精品导航-岛国aaaa级午夜福利片-岛国av动作片在线观看-岛国av无码免费无禁网站-岛国大片激情做爰视频

專(zhuān)注Java教育14年 全國(guó)咨詢(xún)/投訴熱線(xiàn):400-8080-105
動(dòng)力節(jié)點(diǎn)LOGO圖
始于2009,口口相傳的Java黃埔軍校
首頁(yè) 學(xué)習(xí)攻略 Java學(xué)習(xí) Java生成條形碼的方法

Java生成條形碼的方法

更新時(shí)間:2022-12-27 12:36:54 來(lái)源:動(dòng)力節(jié)點(diǎn) 瀏覽3749次

Java生成條形碼的方法是什么?動(dòng)力節(jié)點(diǎn)小編來(lái)告訴大家。條形碼生成有多重方式,例如google-zxing、barcode4j、jbarcode等,本文使用的是zxing,首先添加依賴(lài)

<!-- 截止2021-10-1,zxing的最新版本是3.4.1 -->
<dependency>
    <groupId>com.google.zxing</groupId>
    <artifactId>core</artifactId>
    <version>3.4.1</version>
</dependency>
<dependency>
    <groupId>com.google.zxing</groupId>
    <artifactId>javase</artifactId>
    <version>3.4.1</version>
</dependency>

工具類(lèi)代碼如下:

package fun.psgame.test.util;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.Writer;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.oned.Code128Writer;
import com.google.zxing.pdf417.PDF417Writer;
import org.apache.commons.lang3.StringUtils;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.HashMap;
import java.util.Map;
public class BarCodeUtils {
    /**
     * 默認(rèn)圖片寬度
     */
    private static final int DEFAULT_PICTURE_WIDTH = 300;
    /**
     * 默認(rèn)圖片高度
     */
    private static final int DEFAULT_PICTURE_HEIGHT = 200;
    /**
     * 默認(rèn)條形碼寬度
     */
    private static final int DEFAULT_BAR_CODE_WIDTH = 200;
    /**
     * 默認(rèn)條形碼高度
     */
    private static final int DEFAULT_BAR_CODE_HEIGHT = 70;
    /**
     * 默認(rèn)字體大小
     */
    private static final int DEFAULT_FONT_SIZE = 15;
    /**
     * 設(shè)置 條形碼參數(shù)
     */
    private static final Map<EncodeHintType, Object> hints = new HashMap<>();
    static {
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
    }
    /**
     * 獲取條形碼圖片
     *
     * @param codeValue 條形碼內(nèi)容
     * @return 條形碼圖片
     */
    public static BufferedImage getBarCodeImage(String codeValue) {
        return getBarCodeImage(codeValue, DEFAULT_BAR_CODE_WIDTH, DEFAULT_BAR_CODE_HEIGHT);
    }
    /**
     * 獲取條形碼圖片
     *
     * @param codeValue 條形碼內(nèi)容
     * @param width     寬度
     * @param height    高度
     * @return 條形碼圖片
     */
    public static BufferedImage getBarCodeImage(String codeValue, int width, int height) {
        // CODE_128是最常用的條形碼格式
        return getBarCodeImage(codeValue, width, height, BarcodeFormat.CODE_128);
    }
    /**
     * 獲取條形碼圖片
     *
     * @param codeValue     條形碼內(nèi)容
     * @param width         寬度
     * @param height        高度
     * @param barcodeFormat 條形碼編碼格式
     * @return 條形碼圖片
     */
    public static BufferedImage getBarCodeImage(String codeValue, int width, int height, BarcodeFormat barcodeFormat) {
        Writer writer;
        switch (barcodeFormat) {
            case CODE_128:
                // 最常見(jiàn)的條形碼,但是不支持中文
                writer = new Code128Writer();
                break;
            case PDF_417:
                // 支持中文的條形碼格式
                writer = new PDF417Writer();
                break;
            // 如果使用到其他格式,可以在這里添加
            default:
                writer = new Code128Writer();
        }
        // 編碼內(nèi)容, 編碼類(lèi)型, 寬度, 高度, 設(shè)置參數(shù)
        BitMatrix bitMatrix;
        try {
            bitMatrix = writer.encode(codeValue, barcodeFormat, width, height, hints);
        } catch (WriterException e) {
            throw new RuntimeException("條形碼內(nèi)容寫(xiě)入失敗");
        }
        return MatrixToImageWriter.toBufferedImage(bitMatrix);
    }
    /**
     * 獲取條形碼
     *
     * @param codeValue 條形碼內(nèi)容
     * @param bottomStr 底部文字
     * @return
     */
    public static BufferedImage getBarCodeWithWords(String codeValue, String bottomStr) {
        return getBarCodeWithWords(codeValue, bottomStr, "", "");
    }
    /**
     * 獲取條形碼
     *
     * @param codeValue   條形碼內(nèi)容
     * @param bottomStr   底部文字
     * @param topLeftStr  左上角文字
     * @param topRightStr 右上角文字
     * @return
     */
    public static BufferedImage getBarCodeWithWords(String codeValue,
                                                    String bottomStr,
                                                    String topLeftStr,
                                                    String topRightStr) {
        return getCodeWithWords(getBarCodeImage(codeValue),
                bottomStr,
                topLeftStr,
                topRightStr,
                DEFAULT_PICTURE_WIDTH,
                DEFAULT_PICTURE_HEIGHT,
                0,
                0,
                0,
                0,
                0,
                0,
                DEFAULT_FONT_SIZE);
    }
    /**
     * 獲取條形碼
     *
     * @param codeImage       條形碼圖片
     * @param bottomStr       底部文字
     * @param topLeftStr      左上角文字
     * @param topRightStr     右上角文字
     * @param pictureWidth    圖片寬度
     * @param pictureHeight   圖片高度
     * @param codeOffsetX     條形碼寬度
     * @param codeOffsetY     條形碼高度
     * @param topLeftOffsetX  左上角文字X軸偏移量
     * @param topLeftOffsetY  左上角文字Y軸偏移量
     * @param topRightOffsetX 右上角文字X軸偏移量
     * @param topRightOffsetY 右上角文字Y軸偏移量
     * @param fontSize        字體大小
     * @return 條形碼圖片
     */
    public static BufferedImage getCodeWithWords(BufferedImage codeImage,
                                                 String bottomStr,
                                                 String topLeftStr,
                                                 String topRightStr,
                                                 int pictureWidth,
                                                 int pictureHeight,
                                                 int codeOffsetX,
                                                 int codeOffsetY,
                                                 int topLeftOffsetX,
                                                 int topLeftOffsetY,
                                                 int topRightOffsetX,
                                                 int topRightOffsetY,
                                                 int fontSize) {
        BufferedImage picImage = new BufferedImage(pictureWidth, pictureHeight, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2d = picImage.createGraphics();
        // 抗鋸齒
        setGraphics2D(g2d);
        // 設(shè)置白色
        setColorWhite(g2d, picImage.getWidth(), picImage.getHeight());
        // 條形碼默認(rèn)居中顯示
        int codeStartX = (pictureWidth - codeImage.getWidth()) / 2 + codeOffsetX;
        int codeStartY = (pictureHeight - codeImage.getHeight()) / 2 + codeOffsetY;
        // 畫(huà)條形碼到新的面板
        g2d.drawImage(codeImage, codeStartX, codeStartY, codeImage.getWidth(), codeImage.getHeight(), null);
        // 畫(huà)文字到新的面板
        g2d.setColor(Color.BLACK);
        // 字體、字型、字號(hào)
        g2d.setFont(new Font("微軟雅黑", Font.PLAIN, fontSize));
        // 文字與條形碼之間的間隔
        int wordAndCodeSpacing = 3;
        if (StringUtils.isNotEmpty(bottomStr)) {
            // 文字長(zhǎng)度
            int strWidth = g2d.getFontMetrics().stringWidth(bottomStr);
            // 文字X軸開(kāi)始坐標(biāo),這里是居中
            int strStartX = codeStartX + (codeImage.getWidth() - strWidth) / 2;
            // 文字Y軸開(kāi)始坐標(biāo)
            int strStartY = codeStartY + codeImage.getHeight() + fontSize + wordAndCodeSpacing;
            // 畫(huà)文字
            g2d.drawString(bottomStr, strStartX, strStartY);
        }
        if (StringUtils.isNotEmpty(topLeftStr)) {
            // 文字長(zhǎng)度
            int strWidth = g2d.getFontMetrics().stringWidth(topLeftStr);
            // 文字X軸開(kāi)始坐標(biāo)
            int strStartX = codeStartX + topLeftOffsetX;
            // 文字Y軸開(kāi)始坐標(biāo)
            int strStartY = codeStartY + topLeftOffsetY - wordAndCodeSpacing;
            // 畫(huà)文字
            g2d.drawString(topLeftStr, strStartX, strStartY);
        }
        if (StringUtils.isNotEmpty(topRightStr)) {
            // 文字長(zhǎng)度
            int strWidth = g2d.getFontMetrics().stringWidth(topRightStr);
            // 文字X軸開(kāi)始坐標(biāo),這里是居中
            int strStartX = codeStartX + codeImage.getWidth() - strWidth + topRightOffsetX;
            // 文字Y軸開(kāi)始坐標(biāo)
            int strStartY = codeStartY + topRightOffsetY - wordAndCodeSpacing;
            // 畫(huà)文字
            g2d.drawString(topRightStr, strStartX, strStartY);
        }
        g2d.dispose();
        picImage.flush();
        return picImage;
    }
    /**
     * 設(shè)置 Graphics2D 屬性  (抗鋸齒)
     *
     * @param g2d Graphics2D提供對(duì)幾何形狀、坐標(biāo)轉(zhuǎn)換、顏色管理和文本布局更為復(fù)雜的控制
     */
    private static void setGraphics2D(Graphics2D g2d) {
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_DEFAULT);
        Stroke s = new BasicStroke(1, BasicStroke.CAP_ROUND, BasicStroke.JOIN_MITER);
        g2d.setStroke(s);
    }
    /**
     * 設(shè)置背景為白色
     *
     * @param g2d Graphics2D提供對(duì)幾何形狀、坐標(biāo)轉(zhuǎn)換、顏色管理和文本布局更為復(fù)雜的控制
     */
    private static void setColorWhite(Graphics2D g2d, int width, int height) {
        g2d.setColor(Color.WHITE);
        //填充整個(gè)屏幕
        g2d.fillRect(0, 0, width, height);
        //設(shè)置筆刷
        g2d.setColor(Color.BLACK);
    }
}

使用方式:

@Test
public void createBarCodeTest() throws IOException {
    BufferedImage image = BarCodeUtils.getBarCodeWithWords("0123456789",
            "學(xué)號(hào):0123456789",
            "三年二班",
            "王寶強(qiáng)");
    ImageIO.write(image, "jpg", new File("D:/Temp/barCode.jpg"));
}

最終效果:

提交申請(qǐng)后,顧問(wèn)老師會(huì)電話(huà)與您溝通安排學(xué)習(xí)

  • 全國(guó)校區(qū) 2025-10-10 搶座中
免費(fèi)課程推薦 >>
技術(shù)文檔推薦 >>
主站蜘蛛池模板: 亚洲人成一区二区不卡 | 色综合小说天天综合网 | 欧美日本高清动作片www网站 | 国产片一级aaa毛片视频 | 亚洲国产欧美日韩一区二区三区 | 国产精品99久久99久久久看片 | 99热资源| 免费成人小视频 | 久久91亚洲精品久久91综合 | 欧美一级成人影院免费的 | 国产精品u任我爽爆在线播放 | 就草草在线观看视频 | 欧美在线激情视频 | 久久青草免费91线频观看站街 | 2019中文字幕视频 | 中文字幕精品一区二区三区视频 | 久久久久国产精品免费 | 亚洲精品久久一区二区无卡 | 欧美成人xxxx| 99热成人精品热久久669 | 日本一级在线播放线观看免 | 亚洲视频在线视频 | 国产在线看不卡一区二区 | 99久久精品免费看国产四区 | 日本欧美一区二区三区在线 | 日本一级黄色录像 | 亚洲黄色a级片 | 久久精品站 | 国产精品久久久视频 | 国产成人久久精品二区三区 | 成人免费www在线高清观看 | 四虎影视免费看 | 性欧美视频a毛片在线播放 性欧美视频在线观看 | 国产成人精品一区二三区2022 | 久久国产精品久久 | 亚洲欧美日韩国产vr在线观 | 欧美一区二区三区不卡片 | 在线观看国产情趣免费视频 | 黄色短视频在线播放 | 欧美一级中文字幕 | 欧美 亚洲 中文字幕 |