首页 技术 正文
技术 2022年11月9日
0 收藏 584 点赞 4,853 浏览 2777 个字

QRCode jar下载地址:

生成:http://www.swetake.com/qrcode/index-e.html

读取:https://zh.osdn.net/projects/qrcode/

示例代码(引用自:http://www.ibloger.net/article/160.html):

package cn.utils;  import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException; import javax.imageio.ImageIO; import jp.sourceforge.qrcode.QRCodeDecoder;
import jp.sourceforge.qrcode.data.QRCodeImage;
import jp.sourceforge.qrcode.exception.DecodingFailedException; import com.swetake.util.Qrcode; public class QRCodeUtils {
/**
* 编码字符串内容到目标File对象中
*
* @param encodeddata 编码的内容
* @param destFile 生成file文件 1381090722 5029067275903
* @throws IOException
*/
public static void qrCodeEncode(String encodeddata, File destFile) throws IOException {
Qrcode qrcode = new Qrcode();
qrcode.setQrcodeErrorCorrect('M'); // 纠错级别(L 7%、M 15%、Q 25%、H 30%)和版本有关
qrcode.setQrcodeEncodeMode('B');
qrcode.setQrcodeVersion(7); // 设置Qrcode包的版本 byte[] d = encodeddata.getBytes("GBK"); // 字符集
BufferedImage bi = new BufferedImage(139, 139, BufferedImage.TYPE_INT_RGB);
// createGraphics // 创建图层
Graphics2D g = bi.createGraphics(); g.setBackground(Color.WHITE); // 设置背景颜色(白色)
g.clearRect(0, 0, 139, 139); // 矩形 X、Y、width、height
g.setColor(Color.BLACK); // 设置图像颜色(黑色) if (d.length > 0 && d.length < 123) {
boolean[][] b = qrcode.calQrcode(d);
for (int i = 0; i < b.length; i++) {
for (int j = 0; j < b.length; j++) {
if (b[j][i]) {
g.fillRect(j * 3 + 2, i * 3 + 2, 3, 3);
}
}
}
} // Image img = ImageIO.read(new File("D:/tt.png")); logo
// g.drawImage(img, 25, 55,60,50, null); g.dispose(); // 释放此图形的上下文以及它使用的所有系统资源。调用 dispose 之后,就不能再使用 Graphics 对象
bi.flush(); // 刷新此 Image 对象正在使用的所有可重构的资源 ImageIO.write(bi, "png", destFile);
System.out.println("Input Encoded data is:" + encodeddata);
} /**
* 解析二维码,返回解析内容
*
* @param imageFile
* @return
*/
public static String qrCodeDecode(File imageFile) {
String decodedData = null;
QRCodeDecoder decoder = new QRCodeDecoder();
BufferedImage image = null;
try {
image = ImageIO.read(imageFile);
} catch (IOException e) {
System.out.println("Error: " + e.getMessage());
} try {
decodedData = new String(decoder.decode(new J2SEImage(image)), "GBK");
System.out.println("Output Decoded Data is:" + decodedData);
} catch (DecodingFailedException dfe) {
System.out.println("Error: " + dfe.getMessage());
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return decodedData;
} public static void main(String[] args) {
String FilePath = "d:/qrcode.png";
File qrFile = new File(FilePath); // 二维码内容
String encodeddata = "Hello X-rapido";
try {
QRCodeUtils.qrCodeEncode(encodeddata, qrFile);
} catch (IOException e) {
e.printStackTrace();
} // 解码
String reText = QRCodeUtils.qrCodeDecode(qrFile);
System.out.println(reText);
}
} class J2SEImage implements QRCodeImage {
BufferedImage image; public J2SEImage(BufferedImage image) {
this.image = image;
} public int getWidth() {
return image.getWidth();
} public int getHeight() {
return image.getHeight();
} public int getPixel(int x, int y) {
return image.getRGB(x, y);
}
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,090
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,567
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,415
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,187
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,823
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,906