Java 生成帶Logo和文字的二維碼
ZXing 是一個(gè)開(kāi)放源碼的,用Java實(shí)現(xiàn)的多種格式的1D/2D條碼圖像處理庫(kù),它包含了聯(lián)系到其他語(yǔ)言的端口。Zxing 可以實(shí)現(xiàn)使用手機(jī)的內(nèi)置的攝像頭完成條形碼的掃描及解碼。本章講解用 ZXing 生成和掃碼二維碼。
依賴(lài)
在Java項(xiàng)目中pom.xml加入:
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>${version}</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>${version}</version>
</dependency>
當(dāng)前最新版本是3.4.1,如果是Java開(kāi)發(fā)的Android項(xiàng)目則引入 android-core 。
生成二維碼
生成普通二維碼
// 二維碼內(nèi)容
String text = "https://engr-z.com";
// 二維碼大小
int width = 500, height = 500;
// 二維碼輸出文件
File file = new File("/home/engr-z/qrcode.png");
QRCodeWriter writer = new QRCodeWriter();
BitMatrix m = writer.encode(text, BarcodeFormat.QR_CODE, width, height);
MatrixToImageWriter.writeToPath(m, "png", file.toPath());
如果內(nèi)容較多,需要增大二維碼尺寸。尺寸小內(nèi)容多,二維碼圖形越復(fù)雜越難識(shí)別。
生成帶Logo二維碼
// 二維碼內(nèi)容
String text = "https://engr-z.com";
// 二維碼大小
int width = 500, height = 500;
// 二維碼參數(shù)
CodeStyle style = new CodeStyle();
style.setWidth(width);
style.setHeight(height);
Map<EncodeHintType, Object> hints = new HashMap<>();
//內(nèi)容編碼格式
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
// 指定糾錯(cuò)等級(jí)
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
//設(shè)置二維碼邊的空度,非負(fù)數(shù)
hints.put(EncodeHintType.MARGIN, 1);
// 生成二維碼圖片
QRCodeWriter writer = new QRCodeWriter();
BitMatrix bm = writer.encode(text, BarcodeFormat.QR_CODE, style.getWidth(), style.getHeight(), hints);
int margin = style.getMargin();
int tempM = margin*2;
int[] rec = bm.getEnclosingRectangle(); //獲取二維碼圖案的屬性
int resWidth = rec[2] + tempM;
int resHeight = rec[3] + tempM;
BitMatrix resMatrix = new BitMatrix(resWidth, resHeight); // 按照自定義邊框生成新的BitMatrix
resMatrix.clear();
for (int i = margin; i < resWidth - margin; i++) { //循環(huán),將二維碼圖案繪制到新的bitMatrix中
for (int j = margin; j < resHeight - margin; j++){
if (bm.get(i - margin + rec[0], j - margin + rec[1])){
resMatrix.set(i, j);
}
}
}
bm = resMatrix;
int w = bm.getWidth();
int h = bm.getHeight();
BufferedImage qrcodeBuffImg = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
// 開(kāi)始利用二維碼數(shù)據(jù)創(chuàng)建Bitmap圖片
for (int x = 0; x < w; x++) {
for (int y = 0; y < h; y++) {
qrcodeBuffImg.setRGB(x, y, bm.get(x, y) ? style.getCodeColor() : style.getBackgroundColor());
}
}
/**
* 讀取Logo圖片
*/
File logoFile = new File("/home/engr-z/logo.png");
BufferedImage logo = ImageIO.read(logoFile);
/**
* 設(shè)置logo的大小,設(shè)置為二維碼圖片的20%
*/
int widthLogo = logo.getWidth(null) > qrcodeBuffImg.getWidth() * 3 / 10 ? (qrcodeBuffImg.getWidth() * 3 / 10)
: logo.getWidth(null),
heightLogo = logo.getHeight(null) > qrcodeBuffImg.getHeight() * 3 / 10 ? (qrcodeBuffImg.getHeight() * 3 / 10) : logo.getWidth(null);
/**
* logo在二維碼的位置
*/
int x = (qrcodeBuffImg.getWidth() - widthLogo) / 2;
int y = (qrcodeBuffImg.getHeight() - heightLogo) / 2;
Graphics2D qrcodeOutg = qrcodeBuffImg.createGraphics();
// 把logo寫(xiě)到二維碼圖片中間
qrcodeOutg.drawImage(logo, x, y, widthLogo, heightLogo, null);
qrcodeOutg.dispose();
qrcodeBuffImg.flush();
// 新的圖片,把帶logo的二維碼下面加上文字
String desc = "https://engr-z.com";
int textHeight = 26;
int textMargin = 10;
BufferedImage outImage = new BufferedImage(qrcodeBuffImg.getWidth(), qrcodeBuffImg.getHeight() + textHeight + (textMargin * 2), BufferedImage.TYPE_4BYTE_ABGR);
Graphics2D outg = outImage.createGraphics();
// 畫(huà)二維碼到新的面板
outg.drawImage(qrcodeBuffImg, 0, 0, qrcodeBuffImg.getWidth(), qrcodeBuffImg.getHeight(), null);
outg.setFont(new Font("宋體", Font.BOLD, 26)); // 字體、字型、字號(hào)
int strWidth = outg.getFontMetrics().stringWidth(desc);
outg.setColor(Color.BLACK);
outg.drawString(desc, (outImage.getWidth() - strWidth) / 2, outImage.getHeight() - textMargin);
outg.dispose();
// 二維碼輸出文件
File file = new File("/home/engr-z/qrcode.png");
ImageIO.write(outImage, "png", file);
CodeStyle是我封裝的類(lèi),用來(lái)設(shè)置二維碼樣式:
/**
* @author Engr-Z
* @since 2020/12/18
*/
@Data
public class CodeStyle {
/**
* 背景顏色,如:0xFFFFFFFF
*/
private int backgroundColor = 0xFFFFFFFF;
/**
* 碼顏色,如:0xFF000000
*/
private int codeColor = 0xFF000000;
/**
* 二維碼寬,px
*/
private int width;
/**
* 二維碼高,px
*/
private int height;
/**
* 邊框大小
*/
private int margin = 5;
}
以下是我執(zhí)行生成的二維碼:

讀取二維碼
File qrcodeFile = new File("D:/qrcode.png");
BufferedImage qrcodeImg = ImageIO.read(qrcodeFile);
MultiFormatReader formatReader = new MultiFormatReader();
//讀取指定的二維碼文件
BinaryBitmap binaryBitmap= new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(qrcodeImg)));
//定義二維碼參數(shù)
Map<DecodeHintType, Object> hints= new HashMap<>();
hints.put(DecodeHintType.CHARACTER_SET, "utf-8");
// 讀取
Result result = formatReader.decode(binaryBitmap, hints);
log.info("格式類(lèi)型:{}", result.getBarcodeFormat());
log.info("二維碼內(nèi)容:{}", result.getText());
鏈接
ZXing GitHub:https://github.com/zxing/zxing
以上就是Java 生成帶Logo和文字的二維碼的詳細(xì)內(nèi)容,更多關(guān)于Java 生成二維碼的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
java?freemarker實(shí)現(xiàn)動(dòng)態(tài)生成excel文件
這篇文章主要為大家詳細(xì)介紹了java如何通過(guò)freemarker實(shí)現(xiàn)動(dòng)態(tài)生成excel文件,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-12-12
springboot使用定時(shí)器@Scheduled不管用的解決
這篇文章主要介紹了springboot使用定時(shí)器@Scheduled不管用的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12
關(guān)于java自定義線程池的原理與實(shí)現(xiàn)
本文介紹了如何自定義線程池和阻塞隊(duì)列,包括阻塞隊(duì)列的實(shí)現(xiàn)方法,線程池的構(gòu)建以及拒絕策略的應(yīng)用,詳細(xì)闡述了線程池中任務(wù)的提交和執(zhí)行流程,以及如何處理任務(wù)超出隊(duì)列容量的情況2022-04-04
解決Spring中@Value注解取值為null問(wèn)題
近期應(yīng)用中因業(yè)務(wù)迭代需要接入 user 客戶(hù)端,接入后總是啟動(dòng)失敗,報(bào)注冊(cè) user bean 依賴(lài)的配置屬性為 null,所以接下來(lái)小編就和大家一起排查分析這個(gè)問(wèn)題,感興趣的小伙伴跟著小編一起來(lái)看看吧2023-08-08
java 實(shí)現(xiàn)數(shù)組擴(kuò)容與縮容案例
這篇文章主要介紹了java 實(shí)現(xiàn)數(shù)組擴(kuò)容與縮容案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-02-02
java面向?qū)ο缶幊讨匾拍罾^承和多態(tài)示例解析
這篇文章主要為大家介紹了java面向?qū)ο缶幊痰膬蓚€(gè)重要概念繼承和多態(tài)示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05
java序列化和serialVersionUID的使用方法實(shí)例
這篇文章主要介紹了java序列化和serialVersionUID的使用方法實(shí)例的相關(guān)資料,這里說(shuō)明很詳細(xì)的使用方法讓你徹底學(xué)會(huì),需要的朋友可以參考下2017-08-08

