生成二维码
Posted bevis-byf
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了生成二维码相关的知识,希望对你有一定的参考价值。
package cn.com.utils; 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; import java.io.File; import java.nio.file.Path; import java.util.HashMap; import java.util.UUID; public class QrCodeUtils { public File createQrCode(String content) throws Exception { final int width = 300; final int height = 300; final String format = "png"; //定义二维码的参数 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, hints); String folder = System.getProperty("java.io.tmpdir"); String fileName = UUID.randomUUID().toString()+".png"; Path file = new File(folder,fileName).toPath(); MatrixToImageWriter.writeToPath(bitMatrix, format, file); return file.toFile(); } }
<!-- 二维码相关 --> <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>
以上是关于生成二维码的主要内容,如果未能解决你的问题,请参考以下文章
leetcode_1292. Maximum Side Length of a Square with Sum Less than or Equal to Threshold_[二维前缀和](代码片段