SpringBoot整合ZXing實現(xiàn)二維碼和條形碼的創(chuàng)建
更新時間:2023年12月12日 09:27:41 作者:-代號9527
如今我們越來越多的東西需要用到二維碼或者條形碼,商品的條形碼,付款的二維碼等等,所以本文小編給大家介紹了SpringBoot整合ZXing實現(xiàn)二維碼和條形碼的創(chuàng)建,文章通過代碼示例給大家介紹的非常詳細,需要的朋友可以參考下
以下為整合zxing實現(xiàn)二維碼和條形碼的生成。
1、引入依賴
引入ZXing依賴的坐標:
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.4.1</version>
</dependency>
2、Service層實現(xiàn)
Service接口略,實現(xiàn)類:
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.HashMap;
import java.util.Map;
/**
* @author LLG
* @date 2023/12/8
*/
@Service
@Slf4j
public class CodeService {
/**
* 生成二維碼
* @param data 掃描二維碼后得到的信息
* @param width 二維碼的寬
* @param height 二維碼的高
* @return image
*/
public BufferedImage generateCode(String data, int width, int height) {
BufferedImage image = null;
try {
Map<EncodeHintType, Object> hints = new HashMap<>();
//字符編碼
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
//錯誤糾正級別
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
//二維碼邊距
hints.put(EncodeHintType.MARGIN, 2);
MultiFormatWriter writer = new MultiFormatWriter();
//樣式選擇QR_CODE,其余枚舉類中的可選樣式可自己玩
BitMatrix bitMatrix = writer.encode(data, BarcodeFormat.QR_CODE, width, height, hints);
image = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
//每個框框的顏色
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
image.setRGB(i, j, bitMatrix.get(i, j) ? Color.BLACK.getRGB() : Color.WHITE.getRGB());
}
}
/*以下為選擇保存二維碼
String filePath = "test.png";
File codeFile = new File(filePath);
ImageIO.write(image,"png",codeFile);
log.info("QR碼生成成功,保存路徑{}",filePath);*/
} catch (Exception e) {
e.printStackTrace();
}
return image;
}
/**
* 生成條形碼
* @param data 掃描二維碼后得到的信息
* @param width 條形碼的寬
* @param height 條形碼的高
* @return image
*/
public BufferedImage generateBarCode(String data, int width, int height) {
BufferedImage image = null;
try {
Map<EncodeHintType, Object> hints = new HashMap<>();
//字符編碼
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
MultiFormatWriter writer = new MultiFormatWriter();
//碼的樣式這次選CODE_128
BitMatrix bitMatrix = writer.encode(data, BarcodeFormat.CODE_128, width, height, hints);
image = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
image.setRGB(i, j, bitMatrix.get(i, j) ? 0 : 0xFFFFFF); //黑白條形碼
}
}
} catch (WriterException e) {
e.printStackTrace();
}
return image;
}
}
3、Controller
寫個簡單API調(diào)用下:
@RestController
public class CodeController {
@Resource
private CodeService codeService;
@GetMapping("/code/image")
public void getCodeImage(HttpServletRequest request, HttpServletResponse response){
//測試數(shù)據(jù)
String data = "code9527-test!";
BufferedImage image = codeService.generateCode(data, 100, 100);
try {
ImageIO.write(image,"png",response.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
@GetMapping("/code/barCode")
public void getBarCode(HttpServletRequest request,HttpServletResponse response){
//條形碼中的內(nèi)容
String data = "TB20231208154900";
BufferedImage barCode = codeService.generateBarCode(data, 400, 100);
try {
ImageIO.write(barCode,"png",response.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
}
4、效果
二維碼:

條形碼:

以上就是SpringBoot整合ZXing實現(xiàn)二維碼和條形碼的創(chuàng)建的詳細內(nèi)容,更多關(guān)于SpringBoot ZXing二維碼和條形碼的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Spring中PathMatcher路徑匹配器的實現(xiàn)
Spring框架中的PathMatcher是一個接口,本文主要介紹了Spring中PathMatcher路徑匹配器的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2024-07-07
MyBatis中RowBounds實現(xiàn)內(nèi)存分頁
RowBounds是MyBatis提供的一種內(nèi)存分頁方式,適用于小數(shù)據(jù)量的分頁場景,本文就來詳細的介紹一下,具有一定的參考價值,感興趣的可以了解一下2024-12-12
解決SpringMVC使用@RequestBody注解報400錯誤的問題
這篇文章主要介紹了解決SpringMVC使用@RequestBody注解報400錯誤的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09
springmvc Rest風格介紹及實現(xiàn)代碼示例
這篇文章主要介紹了springmvc Rest風格介紹及實現(xiàn)代碼示例,rest風格簡潔,分享了HiddenHttpMethodFilter 的源碼,通過Spring4.0實現(xiàn)rest風格源碼及簡單錯誤分析,具有一定參考價值,需要的朋友可以了解下。2017-11-11

