通過java生成讀取二維碼詳解
前言
在開始之前,我們需要先引入二維碼生成和讀取的jar包,這里我用的是qrcodejar
新建一個Java工程,在工程中添加一個lib目錄,把兩個Jar包放到lib目錄下,不要忘了add as libiary


在build.gradle中添加配置
compile fileTree(dir:'lib',include:['*.jar'])
(這里我用的是gradle)
準備工作結(jié)束,馬上開始
生成二維碼
public class QRcodeTest {
public static void main(String[] args) throws Exception{
Qrcode qrcode = new Qrcode();
qrcode.setQrcodeErrorCorrect('M');//糾錯等級(分為L、M、H三個等級)
qrcode.setQrcodeEncodeMode('B');//N代表數(shù)字,A代表a-Z,B代表其它字符
qrcode.setQrcodeVersion(10);//版本
//生成二維碼中要存儲的信息
String qrData ="http://www.baidu.com";
//設(shè)置一下二維碼的像素
int width = 67+12*(10-1);
int height = 67+12*(10-1);
BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
//繪圖
Graphics2D gs = bufferedImage.createGraphics();
gs.setBackground(Color.WHITE);
gs.setColor(Color.BLACK);
gs.clearRect(0, 0, width, height);//清除下畫板內(nèi)容
//設(shè)置下偏移量,如果不加偏移量,有時會導(dǎo)致出錯。
int pixoff = 2;
byte[] d = qrData.getBytes("gb2312");
if(d.length > 0 && d.length <120){
boolean[][] s = qrcode.calQrcode(d);
for(int i=0;i<s.length;i++){
for(int j=0;j<s.length;j++){
if(s[j][i]){
gs.fillRect(j*3+pixoff, i*3+pixoff, 3, 3);
}
}
}
}
gs.dispose();
bufferedImage.flush();
//設(shè)置要存儲的目錄(這里存儲在本地)
ImageIO.write(bufferedImage, "png", new File("E:/code/qrcodebd.png"));
}
}
這樣就可以生成一張二維碼圖片,掃描二維碼,會跳轉(zhuǎn)到百度首頁。
二維碼圖片
public class MyQrCodeImage implements QRCodeImage {
BufferedImage bufferedImage;
public MyQrCodeImage(BufferedImage bufferedImage) {
this.bufferedImage = bufferedImage;
}
public int getHeight() {
return bufferedImage.getHeight();
}
public int getPixel(int arg0, int arg1) {
return bufferedImage.getRGB(arg0, arg1);
}
public int getWidth() {
return bufferedImage.getWidth();
}
}
讀取二維碼
public class ReadQrCode {
public static void main(String[] args) throws IOException {
File file = new File("E:/code/qrcodebd.png");
BufferedImage bufferedImage = ImageIO.read(file);
Qrcode qrcode = new Qrcode();
QRCodeDecoder codeDecoder = new QRCodeDecoder();
String result = new String(codeDecoder.decode(new MyQrCodeImage(bufferedImage)),"gb2312");
System.out.println(result);
}
}
讀取二維碼的內(nèi)容為http://www.baidu.com
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot實現(xiàn)連接nacos并支持多環(huán)境部署
這篇文章主要介紹了SpringBoot實現(xiàn)連接nacos并支持多環(huán)境部署方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-06-06
SpringMVC中Controller類數(shù)據(jù)響應(yīng)的方法
這篇文章主要介紹了SpringMVC中的數(shù)據(jù)響應(yīng)的問題,主要來了解 Controller 類如何進行數(shù)據(jù)響應(yīng)的,本文給大家介紹的非常詳細,需要的朋友可以參考下2021-07-07
Java 在Word中創(chuàng)建郵件合并模板并合并文本和圖片的操作方法
通過Java程序展示如何來實現(xiàn)創(chuàng)建模板,并通過郵件合并功能來合并文本數(shù)據(jù)和圖片數(shù)據(jù)的方法,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2021-07-07
Java接口返回省市區(qū)樹形結(jié)構(gòu)的實現(xiàn)
本文主要介紹了Java接口返回省市區(qū)樹形結(jié)構(gòu)的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01
Mybatis-Plus進階分頁與樂觀鎖插件及通用枚舉和多數(shù)據(jù)源詳解
這篇文章主要介紹了Mybatis-Plus的分頁插件與樂觀鎖插件還有通用枚舉和多數(shù)據(jù)源的相關(guān)介紹,文中代碼附有詳細的注釋,感興趣的朋友來看看吧2022-03-03

