SpringBoot生成條形碼的方案詳解
引言
在Spring Boot, Spring Cloud 項(xiàng)目中整合ZXing庫來生成條形碼在特定行業(yè)也是一個(gè)常見需求。
ZXing是google開源的一個(gè)功能強(qiáng)大的Java庫,專門用于二維碼/條形碼等的生成與解析。它不僅能夠生成QR碼/條形碼,還能解析包括QR碼/條形碼在內(nèi)的多種格式.
1. 添加zxing的依賴
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.5.2</version>
</dependency>
2. 生成條形碼
import com.google.zxing.BarcodeFormat;
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 org.springframework.stereotype.Service;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Path;
@Service
public class BarcodeService {
/**
* 生成條形碼并保存到指定路徑
* @param content 條形碼內(nèi)容
* @param path 保存路徑
* @param width 條形碼寬度
* @param height 條形碼高度
*/
public void generateBarcodeImage(String content, String path, int width, int height) {
Code128Writer barcodeWriter = new Code128Writer();
BitMatrix bitMatrix = null;
try {
bitMatrix = barcodeWriter.encode(content, BarcodeFormat.CODE_128, width, height);
Path filePath = FileSystems.getDefault().getPath(path);
MatrixToImageWriter.writeToPath(bitMatrix, "PNG", filePath);
} catch (WriterException | IOException e) {
e.printStackTrace();
}
}
}
3. 調(diào)用條形碼服務(wù)
最后,在Spring Boot的中調(diào)用這個(gè)服務(wù)生成條形碼:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class BarcodeController {
@Autowired
private BarcodeService barcodeService;
@GetMapping("/generate-barcode")
public String generateBarcode(@RequestParam String content, @RequestParam String path, @RequestParam int width, @RequestParam int height) {
barcodeService.generateBarcodeImage(content, path, width, height);
return "Barcode generated successfully at " + path;
}
}
現(xiàn)在,當(dāng)你訪問/generate-barcode端點(diǎn)并傳遞相應(yīng)的參數(shù)時(shí),它將生成一個(gè)條形碼并將其保存到指定的路徑。例如:
http://localhost:8080/generate-barcode?content=123456789&path=/path/to/barcode.png&width=300&height=100
這將生成一個(gè)內(nèi)容為123456789、寬度為300像素、高度為100像素的條形碼,并將其保存到/path/to/barcode.png路徑下。
4. 返回條形碼
如果需要將條形碼直接返回,簡單修改一下就好。
先修改BarcodeService以返回一個(gè)byte[]數(shù)組:
import com.google.zxing.*;
import com.google.zxing.client.j2se.MatrixToImageConfig;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.oned.Code128Writer;
import org.springframework.stereotype.Service;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
@Service
public class BarcodeService {
/**
* 生成條形碼的字節(jié)數(shù)據(jù)
* @param content 條形碼內(nèi)容
* @param width 條形碼寬度
* @param height 條形碼高度
* @return 條形碼的字節(jié)數(shù)據(jù)
*/
public byte[] generateBarcodeImage(String content, int width, int height) {
Code128Writer barcodeWriter = new Code128Writer();
BitMatrix bitMatrix = null;
try {
bitMatrix = barcodeWriter.encode(content, BarcodeFormat.CODE_128, width, height);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
MatrixToImageConfig config = new MatrixToImageConfig(MatrixToImageConfig.BLACK, MatrixToImageConfig.WHITE);
MatrixToImageWriter.writeToStream(bitMatrix, "PNG", outputStream, config);
return outputStream.toByteArray();
} catch (WriterException | IOException e) {
e.printStackTrace();
return null;
}
}
}
然后,修改BarcodeController以返回圖片的字節(jié)數(shù)據(jù):
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class BarcodeController {
@Autowired
private BarcodeService barcodeService;
@GetMapping("/generate-barcode")
public ResponseEntity<byte[]> generateBarcode(@RequestParam String content, @RequestParam int width, @RequestParam int height) {
byte[] barcodeImage = barcodeService.generateBarcodeImage(content, width, height);
if (barcodeImage != null) {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.IMAGE_PNG);
return new ResponseEntity<>(barcodeImage, headers, HttpStatus.OK);
} else {
return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
}
現(xiàn)在,當(dāng)你在Spring Boot, Spring Cloud 項(xiàng)目中訪問/generate-barcode端點(diǎn)并傳遞相應(yīng)的參數(shù)時(shí),它將生成一個(gè)條形碼并將其作為PNG圖片的字節(jié)數(shù)據(jù)返回。
到此這篇關(guān)于SpringBoot生成條形碼的方案詳解的文章就介紹到這了,更多相關(guān)SpringBoot生成條形碼內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用Prometheus+Grafana的方法監(jiān)控Springboot應(yīng)用教程詳解
這篇文章主要介紹了用Prometheus+Grafana的方法監(jiān)控Springboot應(yīng)用,本文通過實(shí)例代碼詳解給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03
關(guān)于SpringBoot的@ConfigurationProperties注解和松散綁定、數(shù)據(jù)校驗(yàn)
這篇文章主要介紹了關(guān)于SpringBoot的@ConfigurationProperties注解和松散綁定、數(shù)據(jù)校驗(yàn),@ConfigurationProperties主要作用就是將prefix屬性指定的前綴配置項(xiàng)的值綁定到這個(gè)JavaBean上?,通過指定的前綴,來綁定配置文件中的配置,需要的朋友可以參考下2023-05-05
spring Mvc配置xml使ResponseBody返回Json的方法示例
這篇文章主要給大家介紹了關(guān)于spring Mvc配置xml使ResponseBody返回Json的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2018-04-04
IDEA導(dǎo)入外部項(xiàng)目報(bào)Error:java: 無效的目標(biāo)發(fā)行版: 11的解決方法
這篇文章主要介紹了IDEA導(dǎo)入外部項(xiàng)目報(bào)Error:java: 無效的目標(biāo)發(fā)行版: 11,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09
Java中關(guān)于char類型變量能夠輸出中文的問題
這篇文章主要介紹了Java中關(guān)于char類型變量能夠輸出中文的問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12
Java視頻斷點(diǎn)上傳的實(shí)現(xiàn)示例
斷點(diǎn)續(xù)傳指的是在下載或上傳時(shí),將下載或上傳任務(wù)人為的劃分為幾個(gè)部分,本文主要介紹了Java視頻斷點(diǎn)上傳的實(shí)現(xiàn)示例,具有一定的參考價(jià)值,感興趣的可以了解一下2024-05-05
解決mybatisplus MetaObjectHandler 失效的問題
本文主要介紹了解決mybatisplus MetaObjectHandler 失效的問題,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02

