java怎么生成qrCode
发表于:2025-12-02 作者:千家信息网编辑
千家信息网最后更新 2025年12月02日,本篇内容主要讲解"java怎么生成qrCode",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"java怎么生成qrCode"吧!参数类:import com
千家信息网最后更新 2025年12月02日java怎么生成qrCode
本篇内容主要讲解"java怎么生成qrCode",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"java怎么生成qrCode"吧!
参数类:
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;/** * */public class QRCodeParams { private String txt; // 二维码内容 private String qrCodeUrl; // 二维码网络路径 private String logoPath; // logo图片 private Integer width = 300; // 二维码宽度 private Integer height = 300; // 二维码高度 private Integer onColor = 0xFF000000; // 前景色 private Integer offColor = 0xFFFFFFFF; // 背景色 private Integer margin = 1; // 白边大小,取值范围0~4 private ErrorCorrectionLevel level = ErrorCorrectionLevel.L; // 二维码容错率 public String getTxt() { return txt; } public void setTxt(String txt) { this.txt = txt; } public Integer getWidth() { return width; } public void setWidth(Integer width) { this.width = width; } public Integer getHeight() { return height; } public void setHeight(Integer height) { this.height = height; } public String getQrCodeUrl() { return qrCodeUrl; } public void setQrCodeUrl(String qrCodeUrl) { this.qrCodeUrl = qrCodeUrl; } public String getLogoPath() { return logoPath; } public void setLogoPath(String logoPath) { this.logoPath = logoPath; } public Integer getOnColor() { return onColor; } public void setOnColor(Integer onColor) { this.onColor = onColor; } public Integer getOffColor() { return offColor; } public void setOffColor(Integer offColor) { this.offColor = offColor; } public ErrorCorrectionLevel getLevel() { return level; } public void setLevel(ErrorCorrectionLevel level) { this.level = level; } public Integer getMargin() { return margin; } public void setMargin(Integer margin) { this.margin = margin; }}图标配置类:
import java.awt.BasicStroke;import java.awt.Color;import java.awt.Graphics2D;import java.awt.geom.RoundRectangle2D;import java.awt.image.BufferedImage;import java.io.IOException;import java.net.URL;import java.net.URLConnection;import javax.imageio.ImageIO;/** */public class QRLogoConfig { /** * 设置 logo * * @param matrixImage * 源二维码图片 * @return 返回带有logo的二维码图片 * @throws IOException * @author Administrator sangwenhao */ public BufferedImage LogoMatrix(BufferedImage matrixImage, String logoPath) throws IOException { /** * 读取二维码图片,并构建绘图对象 */ Graphics2D g2 = matrixImage.createGraphics(); int matrixWidth = matrixImage.getWidth(); int matrixHeigh = matrixImage.getHeight(); int num = logoPath.indexOf('/', 8); String u = logoPath.substring(0, num); URL url = new URL(logoPath); URLConnection connection = url.openConnection(); connection.setDoOutput(true); connection.setRequestProperty("referer", u); /** * 读取Logo图片 */ BufferedImage logo = ImageIO.read(connection.getInputStream()); // 开始绘制图片 g2.drawImage(logo, matrixWidth / 5 * 2, matrixHeigh / 5 * 2, matrixWidth / 5, matrixHeigh / 5, null);// 绘制 BasicStroke stroke = new BasicStroke(5, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND); g2.setStroke(stroke);// 设置笔画对象 // 指定弧度的圆角矩形 RoundRectangle2D.Float round = new RoundRectangle2D.Float( matrixWidth / 5 * 2, matrixHeigh / 5 * 2, matrixWidth / 5, matrixHeigh / 5, 20, 20); g2.setColor(Color.white); g2.draw(round);// 绘制圆弧矩形 // 设置logo 有一道灰色边框 BasicStroke stroke2 = new BasicStroke(1, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND); g2.setStroke(stroke2);// 设置笔画对象 RoundRectangle2D.Float round2 = new RoundRectangle2D.Float( matrixWidth / 5 * 2 + 2, matrixHeigh / 5 * 2 + 2, matrixWidth / 5 - 4, matrixHeigh / 5 - 4, 20, 20); g2.setColor(new Color(128, 128, 128)); g2.draw(round2);// 绘制圆弧矩形 g2.dispose(); matrixImage.flush(); return matrixImage; }}异常类:
/** * */public class QRParamsException extends Exception { private static final long serialVersionUID = 8837582301762730656L; public QRParamsException() { } // 用来创建无参数对象 public QRParamsException(String message) { // 用来创建指定参数对象 super(message); // 调用超类构造器 }}工具处理类:
import java.awt.Color;import java.awt.Graphics2D;import java.awt.Image;import java.awt.image.BufferedImage;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.IOException;import java.util.Hashtable;import javax.imageio.ImageIO;import org.apache.commons.lang3.StringUtils;import com.google.zxing.BarcodeFormat;import com.google.zxing.EncodeHintType;import com.google.zxing.MultiFormatWriter;import com.google.zxing.common.BitMatrix;import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;/** * */public class QRCodeUtil { private static int width = 500; // 二维码宽度 private static int height = 500; // 二维码高度 private static int onColor = 0xFF000000; // 前景色 private static int offColor = 0xFFFFFFFF; // 背景色 private static int margin = 1; // 白边大小,取值范围0~4 private static ErrorCorrectionLevel level = ErrorCorrectionLevel.L; // 二维码容错率 /** * 生成二维码 * * @param params * QRCodeParams属性:txt、fileName、filePath不得为空; * @throws QRParamsException */ public static ByteArrayOutputStream generateQRImage(QRCodeParams params) throws QRParamsException { if (params == null || params.getTxt() == null) { throw new QRParamsException("参数错误"); } try { initData(params); String txt = params.getTxt(); if (StringUtils.isNoneBlank(params.getLogoPath())) { return generateQRImage(txt, params.getLogoPath()); } else { return generateQRImage(txt); } } catch (Exception e) { e.printStackTrace(); } return null; } /** * 生成二维码 * * @param txt * //二维码内容 */ public static ByteArrayOutputStream generateQRImage(String txt) { Hashtable hints = new Hashtable(); // 指定纠错等级 hints.put(EncodeHintType.ERROR_CORRECTION, level); // 指定编码格式 hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); hints.put(EncodeHintType.MARGIN, margin); try { BitMatrix bitMatrix = new MultiFormatWriter().encode(txt, BarcodeFormat.QR_CODE, width, height, hints); BufferedImage image = toBufferedImage(bitMatrix); ByteArrayOutputStream out = new ByteArrayOutputStream(); try { ImageIO.write(image, "PNG", out); return out; } catch (IOException e) { e.printStackTrace(); } } catch (Exception e) { e.printStackTrace(); } return null; } /** * 生成带logo的二维码图片 * * @param txt * //二维码内容 * @param logoPath * //logo绝对物理路径 * @throws Exception */ public static ByteArrayOutputStream generateQRImage(String txt, String logoPath) throws Exception { Hashtable hints = new Hashtable(); // 指定纠错等级,纠错级别(L 7%、M 15%、Q 25%、H 30%) hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); hints.put(EncodeHintType.ERROR_CORRECTION, level); hints.put(EncodeHintType.MARGIN, margin); BitMatrix bitMatrix = new MultiFormatWriter().encode(txt, BarcodeFormat.QR_CODE, width, height, hints); return writeToFile(bitMatrix, logoPath); } /** * * @param matrix * 二维码矩阵相关 * @param format * 二维码图片格式 * @param file * 二维码图片文件 * @param logoPath * logo路径 * @throws IOException */ public static void writeToFile(BitMatrix matrix, String format, File file, String logoPath) throws IOException { BufferedImage image = toBufferedImage(matrix); Graphics2D gs = image.createGraphics(); int ratioWidth = image.getWidth() * 2 / 10; int ratioHeight = image.getHeight() * 2 / 10; // 载入logo Image img = ImageIO.read(new File(logoPath)); int logoWidth = img.getWidth(null) > ratioWidth ? ratioWidth : img .getWidth(null); int logoHeight = img.getHeight(null) > ratioHeight ? ratioHeight : img .getHeight(null); int x = (image.getWidth() - logoWidth) / 2; int y = (image.getHeight() - logoHeight) / 2; gs.drawImage(img, x, y, logoWidth, logoHeight, null); gs.setColor(Color.black); gs.setBackground(Color.WHITE); gs.dispose(); img.flush(); if (!ImageIO.write(image, format, file)) { throw new IOException("Could not write an image of format " + format + " to " + file); } } public static ByteArrayOutputStream writeToFile(BitMatrix matrix, String logoPath) throws IOException { BufferedImage image = toBufferedImage(matrix); // 设置logo图标 QRLogoConfig logoConfig = new QRLogoConfig(); image = logoConfig.LogoMatrix(image, logoPath); ByteArrayOutputStream out = new ByteArrayOutputStream(); try { ImageIO.write(image, "PNG", out); return out; } catch (IOException e) { e.printStackTrace(); } return null; } public static BufferedImage toBufferedImage(BitMatrix matrix) { int width = matrix.getWidth(); int height = matrix.getHeight(); BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { image.setRGB(x, y, matrix.get(x, y) ? onColor : offColor); } } return image; } public static BitMatrix deleteWhite(BitMatrix matrix) { int[] rec = matrix.getEnclosingRectangle(); int resWidth = rec[2] + 1; int resHeight = rec[3] + 1; BitMatrix resMatrix = new BitMatrix(resWidth, resHeight); resMatrix.clear(); for (int i = 0; i < resWidth; i++) { for (int j = 0; j < resHeight; j++) { if (matrix.get(i + rec[0], j + rec[1])) resMatrix.set(i, j); } } return resMatrix; } private static void initData(QRCodeParams params) { if (params.getWidth() != null) { width = params.getWidth(); } if (params.getHeight() != null) { height = params.getHeight(); } if (params.getOnColor() != null) { onColor = params.getOnColor(); } if (params.getOffColor() != null) { offColor = params.getOffColor(); } if (params.getLevel() != null) { level = params.getLevel(); } }} 调用:
QRCodeParams qrParams = new QRCodeParams();// url = "http://192.168.1.2:8086/business"; qrParams.setTxt(url); qrParams.setWidth(300); qrParams.setHeight(300); ByteArrayOutputStream out = QRCodeUtil.generateQRImage(qrParams); qrCode = this.uploadService.upload(out,IdUtils.id()+".png");
到此,相信大家对"java怎么生成qrCode"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
二维
二维码
图片
生成
内容
对象
参数
矩形
路径
图标
圆弧
大小
宽度
景色
格式
笔画
等级
背景
范围
高度
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
安仁电脑软件开发工资多少
软件开发测试爱情短句
数据库日期减
安徽省网络安全学院
网络安全分为两大方面
中指的数据库账号
安全产品平台软件开发
互联网时间同步服务器
三年级网络安全海报简单
邢台软件开发
用友nc配置数据库连接
国家网络安全法治宣传标语
图片json数据库
wow美服数据库
高可用技术数据库分表
涉赌的软件开发主管被罚款
金融安全服务器
呈贡区智能化软件开发咨询报价
敏捷软件开发 计划驱动
广东企业管理软件开发教程
dw文件上传到服务器
网络安全的辩论串词
codesoft软件开发商
计算机网络技术的企业分析
计算机网络技术学什么中专
京东方软件开发在北京么
医疗器械网络安全研究资料
小学生网络安全实践活动
深圳微商软件开发设计
计算机技术数据库