27.用zxing生成二维码
先在idea里面导入jar包
链接:https://pan.baidu.com/s/1UdorNg8o0A4FsElghqkJyw
提取码:c4bt
复制这段内容后打开百度网盘手机App,操作更方便哦
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.*;
/**
*使用zxing生成二维码
*/
public class CreateQRCode {
public static void main(String[] arge) throws WriterException, IOException {
int width = 300;//定义图片宽度
int height = 300;//定义图片高度
String format ="png";//定义图片格式
String content ="https://blog.csdn.net/weixin_43392489";//定义二维码内容
//定义二维码参数
HashMap hints = new HashMap();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");//设置编码
//设置容错等级,等级越高,容量越小
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
hints.put(EncodeHintType.MARGIN,2);//设置边距
//生成矩阵
BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height);
//设置路径
Path file = new File("D:/Users/ma.png").toPath();
MatrixToImageWriter.writeToPath(bitMatrix, format, file);//输出图像
}
}
复制上面代码
运行即可生成
最后在d盘user下可以找到
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 使用C#创建一个MCP客户端
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 按钮权限的设计及实现