JAVA生成二维码链接(扫描二维码后进行指定链接跳转)

您所在的位置:网站首页 二维码怎么制作成链接 JAVA生成二维码链接(扫描二维码后进行指定链接跳转)

JAVA生成二维码链接(扫描二维码后进行指定链接跳转)

2024-03-23 15:22| 来源: 网络整理| 查看: 265

1、需要用到的maven依赖 com.google.zxing core 3.3.0 com.google.zxing javase 3.3.0

目前项目大多都是maven项目,我们只需要把上面的依赖放入项目的pom.xml文件中就能自动导入jar包到项目,前提是自己的maven环境都已经搭建好了,相信大家已经搭建好了,这里就不累述了。

2、代码 package com.example.demo.qrcode; import java.io.File; import java.nio.file.Path; import java.util.HashMap; import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.MultiFormatWriter; import com.google.zxing.client.j2se.MatrixToImageWriter; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; /** * Description: * @author xuyangwei * @date 2019年9月13日 */ public class Zxing { public static void main(String[] args) { Zxing zxing = new Zxing(); // 传参:二维码内容和生成路径 if (zxing.orCode("https://blog.csdn.net/q15102780705/article/details/100060137", "D:\\1.jpg")) { System.out.println("ok,成功"); } else { System.out.println("no,失败"); } } private boolean orCode(String content, String path) { /* * 图片的宽度和高度 */ int width = 300; int height = 300; // 图片的格式 String format = "png"; // 定义二维码的参数 HashMap hints = new HashMap(); // 定义字符集编码格式 hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); // 纠错的等级 L > M > Q > H 纠错的能力越高可存储的越少,一般使用M hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M); // 设置图片边距 hints.put(EncodeHintType.MARGIN, 2); try { // 最终生成 参数列表 (1.内容 2.格式 3.宽度 4.高度 5.二维码参数) BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints); // 写入到本地 Path file = new File(path).toPath(); MatrixToImageWriter.writeToPath(bitMatrix, format, file); return true; } catch (Exception e) { e.printStackTrace(); return false; } } }

使用了mian方法测试了,成功生成了二维码图片。

3、效果

扫描二维码后进行跳转链接



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3