- 导入依赖
<!-- zxing生成二维码 --> <dependency> <groupId>com.google.zxing</groupId> <artifactId>core</artifactId> <version>3.3.3</version> </dependency> <dependency> <groupId>com.google.zxing</groupId> <artifactId>javase</artifactId> <version>3.3.3</version> </dependency>
- 方法
package org.manage.util; 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.QRCodeWriter; import javax.imageio.ImageIO; import java.awt.*; import java.awt.image.BufferedImage; import java.io.*; import java.nio.file.FileSystems; import java.nio.file.Path; import java.util.Base64; import java.util.HashMap; import java.util.Map; public class QRCodeGeneratorUtils { // 生成二维码图片保存在本地 public static void generateQRCodeImage(String url, int width, int height, String filePath) throws WriterException { // 配置参数,设置字符集为UTF-8 Map<EncodeHintType, Object> hints = new HashMap<>(); hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); // 创建二维码的内容 BitMatrix bitMatrix = new MultiFormatWriter().encode(url, BarcodeFormat.QR_CODE, width, height, hints); // 生成图片的路径 Path path = FileSystems.getDefault().getPath(filePath); try { // 将二维码写入图片 MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path); } catch (IOException e) { // 处理可能出现的IO异常 System.err.println("Failed to generate QR Code: " + e.getMessage()); } } //返回二维码 public static byte[] generateQRCodeImageBinarySystem(String url, int width, int height) throws WriterException, IOException { Map<EncodeHintType, Object> hints = new HashMap<>(); hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); BitMatrix bitMatrix = new MultiFormatWriter().encode(url, BarcodeFormat.QR_CODE, width, height, hints); BufferedImage image = MatrixToImageWriter.toBufferedImage(bitMatrix); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ImageIO.write(image, "PNG", baos); return baos.toByteArray(); } /** * * 生成中间带logo的二维码 * @param url 二维码跳转路径 * @param qrCodeWidth 二维码宽度 * @param qrCodeHeight 二维码高度 * @return base64 */ public static byte[] generateQRCodeWithLogo(String url, int qrCodeWidth, int qrCodeHeight) { try { Map<EncodeHintType, Object> hints = new HashMap<>(); hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); BitMatrix bitMatrix = new QRCodeWriter().encode(url, BarcodeFormat.QR_CODE, qrCodeWidth, qrCodeHeight, hints); BufferedImage qrCodeImage = new BufferedImage(qrCodeWidth, qrCodeHeight, BufferedImage.TYPE_INT_ARGB); qrCodeImage.createGraphics(); Graphics2D graphics = (Graphics2D) qrCodeImage.getGraphics(); graphics.setColor(new Color(255, 255, 255, 250)); // 最后一个参数用于调整二维码透明度,参数范围0~255 graphics.fillRect(0, 0, qrCodeWidth, qrCodeHeight); graphics.setColor(Color.BLACK); for (int i = 0; i < qrCodeWidth; i++) { for (int j = 0; j < qrCodeHeight; j++) { if (bitMatrix.get(i, j)) { graphics.fillRect(i, j, 1, 1); } } } //项目打包部署后可能会报错找不到logo图片路径 //因此不建议使用File imageFile = new File("total-admin/src/main/resources/images/appLogo.png"); ClassLoader classLoader = QRCodeGeneratorUtils.class.getClassLoader(); InputStream imageFile = classLoader.getResourceAsStream("images/appLogo.png"); Image logoImage = ImageIO.read(imageFile); int logoWidth = logoImage.getWidth(null); int logoHeight = logoImage.getHeight(null); int x = (qrCodeWidth - logoWidth) / 2; int y = (qrCodeHeight - logoHeight) / 2; graphics.drawImage(logoImage, x, y, logoWidth, logoHeight, null); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ImageIO.write(qrCodeImage, "png", baos); return baos.toByteArray(); } catch (WriterException | IOException e) { e.printStackTrace(); } return null; } }
- 测试
// 主方法用于测试 public static void main(String[] args) throws IOException, WriterException { // 调用生成二维码带logo的方法 String qrCodeData = "https://www.baidu.com"; byte[] qrCodeWithLogo = generateQRCodeWithLogo(qrCodeData,400,400); String encodedString = Base64.getEncoder().encodeToString(qrCodeWithLogo); System.out.println(encodedString); }
Java使用zxing生成二维码(本地保存/base64)、生成二维码中心带logo的
于 2024-01-14 01:18:36 首次发布