Java生成條形碼code128(親測(cè)有效)
生成code 128條形碼工具類
maven依賴
<dependency> <groupId>net.sf.barcode4j</groupId> <artifactId>barcode4j</artifactId> <version>2.1</version> </dependency>
gradle依賴
compile("net.sf.barcode4j:barcode4j:2.1")
工具代碼
package com.tian.demo.admin.controller;
import org.apache.commons.lang.ObjectUtils;
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.ByteArrayOutputStream;
import java.io.IOException;
/**
* @ClassName BarCodeUtils
* @Description TODO
* @Author Harry
* @Date 2020/5/130:35
* @Version 1.0
**/
public class BarCodeUtils {
/**
* 生成code128條形碼
*
* @param height 條形碼的高度
* @param width 條形碼的寬度
* @param message 要生成的文本
* @param withQuietZone 是否兩邊留白
* @param hideText 隱藏可讀文本
* @return 圖片對(duì)應(yīng)的字節(jié)碼
*/
public static byte[] generateBarCode128(String message, Double height, Double width, boolean withQuietZone, boolean hideText) {
Code128Bean bean = new Code128Bean();
// 分辨率
int dpi = 512;
// 設(shè)置兩側(cè)是否留白
bean.doQuietZone(withQuietZone);
// 設(shè)置條形碼高度和寬度
bean.setBarHeight((double) ObjectUtils.defaultIfNull(height, 9.0D));
if (width != null) {
bean.setModuleWidth(width);
}
// 設(shè)置文本位置(包括是否顯示)
if (hideText) {
bean.setMsgPosition(HumanReadablePlacement.HRP_NONE);
}
// 設(shè)置圖片類型
String format = "image/png";
ByteArrayOutputStream ous = new ByteArrayOutputStream();
BitmapCanvasProvider canvas = new BitmapCanvasProvider(ous, format, dpi,
BufferedImage.TYPE_BYTE_BINARY, false, 0);
// 生產(chǎn)條形碼
bean.generateBarcode(canvas, message);
try {
canvas.finish();
} catch (IOException e) {
}
return ous.toByteArray();
}
}
控制層調(diào)用
@GetMapping("test")
public void test(HttpServletRequest request,HttpServletResponse response) throws IOException {
String parameter = request.getParameter("");
byte[] bytes = BarCodeUtils.generateBarCode128("4305383450594", 10.00, 0.3, true, false);
response.setContentType("image/png");
OutputStream output = response.getOutputStream();
InputStream in = new ByteArrayInputStream(bytes);
int len;
byte[] buf = new byte[1024];
while ((len = in.read(buf)) != -1) {
output.write(buf, 0, len);
}
output.flush();
//如果沒有下面兩行,可能出現(xiàn)getOutputStream() has already been called for this response的異常
// output.clear();
// out = pageContext.pushBody();
// Result result = new Result();
}

到此這篇關(guān)于Java生成條形碼code128(親測(cè)有效)的文章就介紹到這了,更多相關(guān)Java生成條形碼code128內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
spring通過導(dǎo)入jar包和配置xml文件啟動(dòng)的步驟詳解
這篇文章主要介紹了spring通過導(dǎo)入jar包和配置xml文件啟動(dòng),本文分步驟通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08
java開源項(xiàng)目jeecgboot的超詳細(xì)解析
JeecgBoot是一款基于BPM的低代碼平臺(tái),下面這篇文章主要給大家介紹了關(guān)于java開源項(xiàng)目jeecgboot的相關(guān)資料,文中通過圖文以及實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-10-10
Springboot啟用多個(gè)監(jiān)聽端口代碼實(shí)例
這篇文章主要介紹了Springboot啟用多個(gè)監(jiān)聽端口代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06
Java中對(duì)AtomicInteger和int值在多線程下遞增操作的測(cè)試
這篇文章主要介紹了Java中對(duì)AtomicInteger和int值在多線程下遞增操作的測(cè)試,本文得出AtomicInteger操作 與 int操作的效率大致相差在50-80倍上下的結(jié)論,需要的朋友可以參考下2014-09-09
Mybatis 如何批量刪除數(shù)據(jù)的實(shí)現(xiàn)示例
這篇文章主要介紹了Mybatis 如何批量刪除數(shù)據(jù)的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
使用java代碼獲取新浪微博應(yīng)用的access token代碼實(shí)例
這篇文章主要介紹了使用java代碼獲取新浪微博應(yīng)用的access token實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05

