JAVA生成二维码链接

网友投稿 565 2022-11-04

本站部分文章、图片属于网络上可搜索到的公开信息,均用于学习和交流用途,不能代表睿象云的观点、立场或意见。我们接受网民的监督,如发现任何违法内容或侵犯了您的权益,请第一时间联系小编邮箱jiasou666@gmail.com 处理。

JAVA生成二维码链接

pom.xml

com.google.zxing core 3.3.3 com.google.zxing javase 3.3.3

2.Java类

package zxing;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 xueqimiao * @date 2019年12月25日 */public class Zxing { public static void main(String[] args) { Zxing zxing = new Zxing(); // 传参:二维码内容和生成路径 if (zxing.orCode("https://baidu.com", "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; } }}

3.运行结果

上一篇:软件测试培训之优化测试和成本的7种方法
下一篇:软件测试培训之Python 和Java 哪个语言更适合做自动化测试?
相关文章

 发表评论

暂时没有评论,来抢沙发吧~