通过Qrcode生成二维码:
需导入Qrcode.jar包
生成二维码:
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;
public class QRCodeEncoderHandler {
public void encoderQRCode(String content, String imgPath) throws Exception {
// 处理异常
try {
// 实例化对象
Qrcode qrcodeHandler = new Qrcode();
// 设置
qrcodeHandler.setQrcodeErrorCorrect('M');
qrcodeHandler.setQrcodeEncodeMode('B');
qrcodeHandler.setQrcodeVersion(7);
// 二维码内容转换
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);
// 设定图像颜色> BLACK
gs.setColor(Color.BLACK);
// 设置偏移量 不设置可能导致解析出错
int pixoff = 2;
// 输出内容> 二维码
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 {
throw new Exception("QRCode content bytes length = " + contentBytes.length + " not in [ 0,120 ]. ");
}
gs.dispose();
bufImg.flush();
// 二维码文件对象
File imgFile = new File(imgPath);
// 判断是否存在
if(!imgFile.exists()){
// 不存在,先进行创建
imgFile.mkdirs();
}
// 生成二维码QRCode图片
ImageIO.write(bufImg, "png", imgFile);
} catch (Exception e) {
throw new Exception("Error:" + e.getMessage());
}
}
}
解析二维码:
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import jp.sourceforge.qrcode.QRCodeDecoder;
import jp.sourceforge.qrcode.data.QRCodeImage;
public class QRCodeDecoderHandle {
public String decoderQRCode(String imgPath) throws Exception {
// 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)));
} catch (Exception e) {
// 抛出异常
throw new Exception("Error: " + e.getMessage());
}
// 返回
return decodedData;
}
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);
}
}
}
public class DemoTest {
public static void main(String[] args) throws Exception {
String imgPath = "D:/test/twocode/Wang_QRCode.png";
String content ="\nMyHome [ http://user.qzone.qq.com/417282638/infocenter ]"
+ "\nEMail [ hnwsjx@126.com ]";
QRCodeEncoderHandler handler = new QRCodeEncoderHandler();
// handler.encoderQRCode(content, imgPath);
System.out.println("encoder QRcode success");
QRCodeDecoderHandle handle = new QRCodeDecoderHandle();
String imgPaths = "d:/test/twocode/Wang_QRCode.png";
String decoderContent = handle.decoderQRCode(imgPaths);
System.out.println("解析结果如下:");
System.out.println(decoderContent);
System.out.println("========decoder success!!!");
}
}
需导入Qrcode.jar包
生成二维码:
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;
public class QRCodeEncoderHandler {
public void encoderQRCode(String content, String imgPath) throws Exception {
// 处理异常
try {
// 实例化对象
Qrcode qrcodeHandler = new Qrcode();
// 设置
qrcodeHandler.setQrcodeErrorCorrect('M');
qrcodeHandler.setQrcodeEncodeMode('B');
qrcodeHandler.setQrcodeVersion(7);
// 二维码内容转换
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);
// 设定图像颜色> BLACK
gs.setColor(Color.BLACK);
// 设置偏移量 不设置可能导致解析出错
int pixoff = 2;
// 输出内容> 二维码
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 {
throw new Exception("QRCode content bytes length = " + contentBytes.length + " not in [ 0,120 ]. ");
}
gs.dispose();
bufImg.flush();
// 二维码文件对象
File imgFile = new File(imgPath);
// 判断是否存在
if(!imgFile.exists()){
// 不存在,先进行创建
imgFile.mkdirs();
}
// 生成二维码QRCode图片
ImageIO.write(bufImg, "png", imgFile);
} catch (Exception e) {
throw new Exception("Error:" + e.getMessage());
}
}
}
解析二维码:
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import jp.sourceforge.qrcode.QRCodeDecoder;
import jp.sourceforge.qrcode.data.QRCodeImage;
public class QRCodeDecoderHandle {
public String decoderQRCode(String imgPath) throws Exception {
// 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)));
} catch (Exception e) {
// 抛出异常
throw new Exception("Error: " + e.getMessage());
}
// 返回
return decodedData;
}
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);
}
}
}
public class DemoTest {
public static void main(String[] args) throws Exception {
String imgPath = "D:/test/twocode/Wang_QRCode.png";
String content ="\nMyHome [ http://user.qzone.qq.com/417282638/infocenter ]"
+ "\nEMail [ hnwsjx@126.com ]";
QRCodeEncoderHandler handler = new QRCodeEncoderHandler();
// handler.encoderQRCode(content, imgPath);
System.out.println("encoder QRcode success");
QRCodeDecoderHandle handle = new QRCodeDecoderHandle();
String imgPaths = "d:/test/twocode/Wang_QRCode.png";
String decoderContent = handle.decoderQRCode(imgPaths);
System.out.println("解析结果如下:");
System.out.println(decoderContent);
System.out.println("========decoder success!!!");
}
}