Java實(shí)現(xiàn)二維碼QRCode的編碼和解碼與示例解析
Java實(shí)現(xiàn)二維碼QRCode的編碼和解碼
涉及到的一些主要類庫,方便大家下載:
編碼lib:Qrcode_swetake.jar (官網(wǎng)介紹-- http://www.swetake.com/qr/index-e.html)
解碼lib:qrcode.jar (官網(wǎng)介紹-- http://sourceforge.jp/projects/qrcode/)
【一】、編碼:
Java代碼QRCodeEncoderHandler.java
package michael.qrcode;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import com.swetake.util.Qrcode;
/**
* 二維碼生成器
* @blog http://sjsky.iteye.com
* @author Michael
*/
public class QRCodeEncoderHandler {
/**
* 生成二維碼(QRCode)圖片
* @param content
* @param imgPath
*/
public void encoderQRCode(String content, String imgPath) {
try {
Qrcode qrcodeHandler = new Qrcode();
qrcodeHandler.setQrcodeErrorCorrect('M');
qrcodeHandler.setQrcodeEncodeMode('B');
qrcodeHandler.setQrcodeVersion(7);
System.out.println(content);
byte[] contentBytes = content.getBytes("gb2312");
BufferedImage bufImg = new BufferedImage(140, 140,
BufferedImage.TYPE_INT_RGB);
Graphics2D gs = bufImg.createGraphics();
gs.setBackground(Color.WHITE);
gs.clearRect(0, 0, 140, 140);
// 設(shè)定圖像顏色> BLACK
gs.setColor(Color.BLACK);
// 設(shè)置偏移量 不設(shè)置可能導(dǎo)致解析出錯(cuò)
int pixoff = 2;
// 輸出內(nèi)容> 二維碼
if (contentBytes.length > 0 && contentBytes.length < 120) {
boolean[][] codeOut = qrcodeHandler.calQrcode(contentBytes);
for (int i = 0; i < codeOut.length; i++) {
for (int j = 0; j < codeOut.length; j++) {
if (codeOut[j][i]) {
gs.fillRect(j * 3 + pixoff, i * 3 + pixoff, 3, 3);
}
}
}
} else {
System.err.println("QRCode content bytes length = "
+ contentBytes.length + " not in [ 0,120 ]. ");
}
gs.dispose();
bufImg.flush();
File imgFile = new File(imgPath);
// 生成二維碼QRCode圖片
ImageIO.write(bufImg, "png", imgFile);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
String imgPath = "D:/test/twocode/Michael_QRCode.png";
String content = "Hello 大大、小小,welcome to QRCode!"
+ "\nMyblog [ http://sjsky.iteye.com ]"
+ "\nEMail [ sjsky007@gmail.com ]" + "\nTwitter [ @suncto ]";
QRCodeEncoderHandler handler = new QRCodeEncoderHandler();
handler.encoderQRCode(content, imgPath);
System.out.println("encoder QRcode success");
}
}
運(yùn)行后生成的二維碼圖片如下:

此時(shí)就可用手機(jī)的二維碼掃描軟件(本人用的:android 快拍二維碼 )來測試下,識(shí)別成功的截圖如下:

喜歡的朋友可以下載后試一試,做一些名片或者自己喜歡的東西。當(dāng)然Java也可以對二維碼圖片解碼,具體看下面關(guān)于解碼的內(nèi)容。
【二】、解碼:
Java代碼QRCodeDecoderHandler.java
package michael.qrcode;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import jp.sourceforge.qrcode.QRCodeDecoder;
import jp.sourceforge.qrcode.data.QRCodeImage;
import jp.sourceforge.qrcode.exception.DecodingFailedException;
/**
* @blog http://sjsky.iteye.com
* @author Michael
*/
public class QRCodeDecoderHandler {
/**
* 解碼二維碼
* @param imgPath
* @return String
*/
public String decoderQRCode(String imgPath) {
// QRCode 二維碼圖片的文件
File imageFile = new File(imgPath);
BufferedImage bufImg = null;
String decodedData = null;
try {
bufImg = ImageIO.read(imageFile);
QRCodeDecoder decoder = new QRCodeDecoder();
decodedData = new String(decoder.decode(new J2SEImage(bufImg)));
// try {
// System.out.println(new String(decodedData.getBytes("gb2312"),
// "gb2312"));
// } catch (Exception e) {
// // TODO: handle exception
// }
} catch (IOException e) {
System.out.println("Error: " + e.getMessage());
e.printStackTrace();
} catch (DecodingFailedException dfe) {
System.out.println("Error: " + dfe.getMessage());
dfe.printStackTrace();
}
return decodedData;
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
QRCodeDecoderHandler handler = new QRCodeDecoderHandler();
String imgPath = "d:/test/twocode/Michael_QRCode.png";
String decoderContent = handler.decoderQRCode(imgPath);
System.out.println("解析結(jié)果如下:");
System.out.println(decoderContent);
System.out.println("========decoder success!!!");
}
class J2SEImage implements QRCodeImage {
BufferedImage bufImg;
public J2SEImage(BufferedImage bufImg) {
this.bufImg = bufImg;
}
public int getWidth() {
return bufImg.getWidth();
}
public int getHeight() {
return bufImg.getHeight();
}
public int getPixel(int x, int y) {
return bufImg.getRGB(x, y);
}
}
}
運(yùn)行結(jié)果如下(解碼出的內(nèi)容和之前輸入的內(nèi)容一致 ):
解析結(jié)果如下:
Hello 大大、小小,welcome to QRCode!
Myblog [ http://sjsky.iteye.com ]
EMail [ sjsky007@gmail.com ]
Twitter [ @suncto ]
========decoder success!!!
以上就是對Java實(shí)現(xiàn)二維碼QRCode的編碼和解碼的資料整理,后續(xù)繼續(xù)補(bǔ)充相關(guān)資料,謝謝大家對本站的支持!
相關(guān)文章
SpringBoot2底層注解@ConfigurationProperties配置綁定
這篇文章主要介紹了SpringBoot2底層注解@ConfigurationProperties配置綁定,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05
微信小程序 springboot后臺(tái)如何獲取用戶的openid
這篇文章主要介紹了微信小程序 springboot后臺(tái)如何獲取用戶的openid,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09
java redis 實(shí)現(xiàn)簡單的用戶簽到功能
這篇文章主要介紹了java redis 實(shí)現(xiàn)簡單的用戶簽到功能,幫助大家更好的理解和使用Java,感興趣的朋友可以了解下2020-12-12
總結(jié)Java集合類操作優(yōu)化經(jīng)驗(yàn)
本文主要介紹的就是集合框架的使用經(jīng)驗(yàn),告訴大家如何高效、方便地管理對象,所有代碼基于JDK7,需要的朋友可以參考下2015-08-08
springboot(thymeleaf)中th:field和th:value的區(qū)別及說明
這篇文章主要介紹了springboot(thymeleaf)中th:field和th:value的區(qū)別及說明,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10
SpringBoot設(shè)置靜態(tài)資源訪問控制和封裝集成方案
這篇文章主要介紹了SpringBoot靜態(tài)資源訪問控制和封裝集成方案,關(guān)于springboot靜態(tài)資源訪問的問題,小編是通過自定義webconfig實(shí)現(xiàn)WebMvcConfigurer,重寫addResourceHandlers方法,具體完整代碼跟隨小編一起看看吧2021-08-08
集成apollo動(dòng)態(tài)日志取締logback-spring.xml配置
這篇文章主要為大家介紹了集成apollo動(dòng)態(tài)日志取締logback-spring.xml配置的過程內(nèi)容詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助2022-02-02
SpringBoot+shardingsphere實(shí)現(xiàn)按月分表功能教程
這篇文章主要介紹了SpringBoot+shardingsphere實(shí)現(xiàn)按月分表功能教程,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-04-04

