二维码

通过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!!!");

}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值