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


在build.gradle中添加配置
compile fileTree(dir:'lib',include:['*.jar'])
(這里我用的是gradle)
準(zhǔn)備工作結(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整合Spring Security構(gòu)建安全的Web應(yīng)用
pring Security是一個強大的身份驗證和訪問控制框架,本文主要介紹了SpringBoot整合Spring Security構(gòu)建安全的Web應(yīng)用,具有一定的參考價值,感興趣的可以了解一下2024-01-01
關(guān)于springboot 配置文件中屬性變量引用方式@@解析
這篇文章主要介紹了關(guān)于springboot 配置文件中屬性變量引用方式@@解析,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04
Spring?Cloud?oauth2?認(rèn)證服務(wù)搭建過程示例
這篇文章主要為大家介紹了Spring?Cloud?oauth2?認(rèn)證服務(wù)搭建過程示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06
SpringBoot使用thymeleaf實現(xiàn)一個前端表格方法詳解
Thymeleaf是一個現(xiàn)代的服務(wù)器端 Java 模板引擎,適用于 Web 和獨立環(huán)境。Thymeleaf 的主要目標(biāo)是為您的開發(fā)工作流程帶來優(yōu)雅的自然模板,本文就來用它實現(xiàn)一個前端表格,感興趣的可以了解一下2022-10-10
Spring?代理?Bean?獲取不到原始?Bean?對象注解解決方法
這篇文章主要介紹了Spring?代理?Bean?獲取不到原始?Bean?對象注解解決方法,文章圍繞主題相關(guān)資料展開詳細(xì)介紹,需要的小伙伴可以參考一下2022-04-04
SpringBoot2之PUT請求接收不了參數(shù)的解決方案
這篇文章主要介紹了SpringBoot2之PUT請求接收不了參數(shù)的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07

