欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

通過java生成讀取二維碼詳解

 更新時間:2019年05月29日 11:22:14   作者:Cold不叫寇德  
這篇文章主要介紹了java二維碼生成讀取詳解,二維碼再生活在無處不在,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,下面和小編一起來學(xué)習(xí)一下吧

前言

在開始之前,我們需要先引入二維碼生成和讀取的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)文章

最新評論