标签:uid mon bit character public final artifact dom image
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>
标签:uid mon bit character public final artifact dom image
原文地址:https://www.cnblogs.com/bevis-byf/p/11720691.html