一怕敬、淺談二維碼
二維碼能有效的進(jìn)行信息的存儲和傳遞睁蕾,在我們每天的生活中都會用到二維碼。比如微信支付寶的掃碼支付族阅,掃一掃加好友篓跛,手機(jī)掃二維碼授權(quán)pc端登錄等等的廣泛應(yīng)用。那在我們開發(fā)中耘分,如何生成二維碼呢举塔?
二绑警、引入需要的依賴
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.3.0</version>
</dependency>
三求泰、生成帶logo的二維碼
public class QRCodeLogo {
private static final int QRCOLOR = 0xFF000000; // 默認(rèn)是黑色
private static final int BGWHITE = 0xFFFFFFFF; // 背景顏色
private static final int WIDTH = 400; // 二維碼寬
private static final int HEIGHT = 400; // 二維碼高
// 用于設(shè)置QR二維碼參數(shù)
private static Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>() {
private static final long serialVersionUID = 1L;
{
put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);// 設(shè)置QR二維碼的糾錯級別(H為最高級別)具體級別信息
put(EncodeHintType.CHARACTER_SET, "utf-8");// 設(shè)置編碼方式
}
};
// 生成帶logo的二維碼圖片
public static void drawLogoQRCode(File logoFile, File codeFile, String qrUrl, String note) {
try {
MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
// 參數(shù)順序分別為:編碼內(nèi)容,編碼類型计盒,生成圖片寬度渴频,生成圖片高度,設(shè)置參數(shù)
BitMatrix bm = multiFormatWriter.encode(qrUrl, BarcodeFormat.QR_CODE, WIDTH, HEIGHT, hints);
BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
// 開始利用二維碼數(shù)據(jù)創(chuàng)建Bitmap圖片北启,分別設(shè)為黑(0xFFFFFFFF)白(0xFF000000)兩色
for (int x = 0; x < WIDTH; x++) {
for (int y = 0; y < HEIGHT; y++) {
image.setRGB(x, y, bm.get(x, y) ? QRCOLOR : BGWHITE);
}
}
int width = image.getWidth();
int height = image.getHeight();
if (Objects.nonNull(logoFile) && logoFile.exists()) {
// 構(gòu)建繪圖對象
Graphics2D g = image.createGraphics();
// 讀取Logo圖片
BufferedImage logo = ImageIO.read(logoFile);
// 開始繪制logo圖片
g.drawImage(logo, width * 2 / 5, height * 2 / 5, width * 2 / 10, height * 2 / 10, null);
g.dispose();
logo.flush();
}
// 自定義文本描述
if (note!=null || !"".equals(note)) {
// 新的圖片卜朗,把帶logo的二維碼下面加上文字
BufferedImage outImage = new BufferedImage(400, 445, BufferedImage.TYPE_4BYTE_ABGR);
Graphics2D outg = outImage.createGraphics();
// 畫二維碼到新的面板
outg.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), null);
// 畫文字到新的面板
outg.setColor(Color.BLACK);
outg.setFont(new Font("楷體", Font.BOLD, 30)); // 字體拔第、字型、字號
int strWidth = outg.getFontMetrics().stringWidth(note);
if (strWidth > 399) {
// //長度過長就截取前面部分
// 長度過長就換行
String note1 = note.substring(0, note.length() / 2);
String note2 = note.substring(note.length() / 2, note.length());
int strWidth1 = outg.getFontMetrics().stringWidth(note1);
int strWidth2 = outg.getFontMetrics().stringWidth(note2);
outg.drawString(note1, 200 - strWidth1 / 2, height + (outImage.getHeight() - height) / 2 + 12);
BufferedImage outImage2 = new BufferedImage(400, 485, BufferedImage.TYPE_4BYTE_ABGR);
Graphics2D outg2 = outImage2.createGraphics();
outg2.drawImage(outImage, 0, 0, outImage.getWidth(), outImage.getHeight(), null);
outg2.setColor(Color.BLACK);
outg2.setFont(new Font("宋體", Font.BOLD, 30)); // 字體场钉、字型蚊俺、字號
outg2.drawString(note2, 200 - strWidth2 / 2,outImage.getHeight() + (outImage2.getHeight() - outImage.getHeight()) / 2 + 5);
outg2.dispose();
outImage2.flush();
outImage = outImage2;
} else {
outg.drawString(note, 200 - strWidth / 2, height + (outImage.getHeight() - height) / 2 + 12); // 畫文字
}
outg.dispose();
outImage.flush();
image = outImage;
}
image.flush();
ImageIO.write(image, "png", codeFile); // TODO
} catch (Exception e) {
e.printStackTrace();
}
}
}
四、編寫測試類
@Test
public void test1(){
//logo圖片保存的位置
File logoFile = new File("./data/bbb.jpg"); //logo
//生成的二維碼存放的位置
File QrCodeFile = new File("./data/aaa.jpg");
//二維碼攜帶的鏈接或者內(nèi)容
String url = "https://www.fangxinqian.cn/redirect.html";
//二維碼的標(biāo)題
String note = "";
QRCodeLogo.drawLogoQRCode(logoFile, QrCodeFile, url, note);
}
五逛万、效果展示
生成的二維碼圖片
image.png
logo圖片
image.png
大家可以把自己簡書的地址放在url里泳猬,把logo換成一個妹子,生成的二維碼就是我上面的效果宇植。然后把圖片發(fā)在朋友圈說給大家推薦一個妹子的微信得封,你的朋友掃完就進(jìn)入了你的簡書主頁,可以騙一波關(guān)注哈哈哈哈V赣簟Cι稀!