思路使用谷歌提供的com.google.zxing,工具類,對內容進行二維碼生成。java
1.添加maven依賴spring
<dependency> <groupId>com.google.zxing</groupId> <artifactId>core</artifactId> <version>3.3.0</version> </dependency> <dependency> <groupId>com.google.zxing</groupId> <artifactId>javase</artifactId> <version>3.3.0</version> </dependency>
2.編寫MyQRCodeUtil.java緩存
package com.mmz.spring; import java.awt.image.BufferedImage; import java.io.File; import java.nio.file.Path; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util.Random; import javax.imageio.ImageIO; import org.springframework.stereotype.Component; import com.google.zxing.BarcodeFormat; import com.google.zxing.BinaryBitmap; import com.google.zxing.EncodeHintType; import com.google.zxing.MultiFormatReader; import com.google.zxing.MultiFormatWriter; import com.google.zxing.Result; import com.google.zxing.client.j2se.BufferedImageLuminanceSource; import com.google.zxing.client.j2se.MatrixToImageWriter; import com.google.zxing.common.BitMatrix; import com.google.zxing.common.HybridBinarizer; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; /** * 二維碼生成工具類 */ @Component public class MyQRCodeUtil { /** * 調用示例MyQRCodeUtil.createQRCodeByContent("個人小夥伴", "d://"); * @param content 生成的內容 * @param compressionUrl 本地臨時路徑,例如 d:// * @return 返回臨時文件路徑 */ public static String createQRCodeByContent(String content,String compressionUrl){ final int width = 500; final int height = 600; final String format = "png"; int flag = new Random().nextInt(9999); if (flag < 1000) { flag += 1000; } //定義二維碼的參數 HashMap hints = new HashMap(); hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M); hints.put(EncodeHintType.MARGIN, 2); String url =""; //生成二維碼 try { BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints); // 先寫到本地緩存,設置一個隨機文件名 String tempFileName = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + String.valueOf(flag); Path tempFile = new File(compressionUrl + tempFileName + ".png").toPath(); //Path file = new File("F:/img.png").toPath(); MatrixToImageWriter.writeToPath(bitMatrix, format, tempFile); url = compressionUrl + tempFileName + ".png"; } catch (Exception e) { } return url; } public static Result parseQrCode(String path) throws Exception { MultiFormatReader formatReader = new MultiFormatReader(); File file = new File(path); BufferedImage image = ImageIO.read(file); BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image))); //定義二維碼的參數 HashMap map = new HashMap(); map.put(EncodeHintType.CHARACTER_SET, "utf-8"); Result result = formatReader.decode(binaryBitmap, map); System.out.println("二維碼解析結果:" + result.toString()); System.out.println("二維碼的格式:" + result.getBarcodeFormat()); System.out.println("二維碼的文本內容:" + result.getText()); return result; } }
3.編寫測試類 QrTest.javadom
package com.mmz.spring; import com.google.zxing.Result; public class QrTest { public static void main(String[] args) { MyQRCodeUtil.createQRCodeByContent("個人小夥伴", "d://"); try { Result parseQrCode = MyQRCodeUtil.parseQrCode("d://201906240952007298.png"); System.out.println(parseQrCode); } catch (Exception e) { e.printStackTrace(); } } }
//最後發現,若是生成的內容過多,解析方法會報錯com.google.zxing.NotFoundExceptionmaven