package com.qgp.study;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
/**
* @Description:
* @Author: qianguopeng
* @createTime: 2023年08月18日 13:41:14
* @version: 版本:1.0
*/
public class QRCodeWithLink {
public static void main(String[] args) throws UnsupportedEncodingException {
String link = "https://blog.csdn.net/TPQTX?type=blog"; // 要生成二维码的链接
String encodedLink = URLEncoder.encode(link, "UTF-8");
String weChatLink = "weixin://dl/scan?qr_code=" + encodedLink; // 微信跳转链接格式
int size = 300; // 二维码尺寸
String filePath = "D:\\code\\study-demo\\QR-Code-demo\\wechat_qrcode.png"; // 保存的文件路径
try {
QRCodeWriter qrCodeWriter = new QRCodeWriter();
// 设置二维码参数
EncodeHintType hintType = EncodeHintType.ERROR_CORRECTION;
ErrorCorrectionLevel level = ErrorCorrectionLevel.H;
BitMatrix bitMatrix = qrCodeWriter.encode(weChatLink, BarcodeFormat.QR_CODE, size, size);
BufferedImage qrImage = new BufferedImage(size, size, BufferedImage.TYPE_INT_RGB);
int qrColor = 0xFF000000; // 黑色
int backgroundColor = 0xFFFFFFFF; // 白色
// 渲染二维码
for (int x = 0; x < size; x++) {
for (int y = 0; y < size; y++) {
qrImage.setRGB(x, y, bitMatrix.get(x, y) ? qrColor : backgroundColor);
}
}
// 保存二维码图像
File outputFile = new File(filePath);
ImageIO.write(qrImage, "png", outputFile);
System.out.println("WeChat QR Code generated and saved.");
// 在默认图像查看器中显示二维码
displayQRCodeImage(outputFile);
} catch (Exception e) {
e.printStackTrace();
}
}
// 在默认图像查看器中显示图像
private static void displayQRCodeImage(File imageFile) throws IOException {
if (imageFile.exists()) {
String osName = System.getProperty("os.name").toLowerCase();
Runtime rt = Runtime.getRuntime();
try {
if (osName.contains("win")) {
rt.exec("rundll32 url.dll,FileProtocolHandler " + imageFile.getAbsolutePath());
} else if (osName.contains("mac")) {
rt.exec("open " + imageFile.getAbsolutePath());
} else if (osName.contains("nix") || osName.contains("nux")) {
rt.exec("xdg-open " + imageFile.getAbsolutePath());
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
创意编码艺术:用Java绘就你的二维码艺术王国(附源代码)!
共29个文件
png:15个
java:6个
class:5个
0 下载量 120 浏览量
2023-08-18
14:04:18
上传
评论
收藏 90KB RAR 举报
温馨提示
本章节源代码
资源推荐
资源详情
资源评论
收起资源包目录
QR-Code-demo.rar (29个子文件)
QR-Code-demo
自定义二维码.png 2KB
picture.jpg 31KB
URL加密跳转.png 2KB
文本二维码.png 1KB
QR-Code-demo
pom.xml 1KB
src
test
java
org
example
AppTest.java 677B
main
java
com
qgp
study
QRCodeWithLink.java 3KB
QRGenerateHasLogoDemo.java 3KB
QRGenerateDemo.java 2KB
CustomQRCodeGenerator.java 3KB
BatchQRCodeGenerator.java 3KB
target
classes
com
qgp
study
CustomQRCodeGenerator.class 3KB
QRGenerateHasLogoDemo.class 4KB
QRCodeWithLink.class 4KB
BatchQRCodeGenerator.class 3KB
QRGenerateDemo.class 3KB
generated-sources
annotations
.gitignore 490B
qrcode_10.png 1KB
qrcode_3.png 1KB
qrcode_7.png 1KB
wechat_qrcode.png 2KB
qrcode_4.png 1KB
CSDN-天秤钱同学.png 30KB
qrcode_6.png 1KB
qrcode_5.png 1KB
qrcode_8.png 1KB
qrcode_1.png 1KB
qrcode_2.png 1KB
qrcode_9.png 1KB
共 29 条
- 1
资源评论
Money鹏同学
- 粉丝: 101
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功