<dependency> <groupId>com.google.zxing</groupId> <artifactId>core</artifactId> <version>3.3.0</version> </dependency> <!-- https://mvnrepository.com/artifact/com.google.zxing/javase --> <dependency> <groupId>com.google.zxing</groupId> <artifactId>javase</artifactId> <version>3.3.0</version> </dependency>
public static void generateQrCode(OutputStream out,String content, int width, int height, String format) throws WriterException, IOException { //format="png" Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>(); // 設置QR二維碼的糾錯級別(H爲最高級別)具體級別信息 hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); // 內容所使用編碼 hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); // 設置圖片的邊距 hints.put(EncodeHintType.MARGIN,1); // 生成矩陣 BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints); // 輸出圖像 MatrixToImageWriter.writeToStream(bitMatrix,format,out); }
public static Result decodeQrCode(InputStream inputStream) throws IOException, NotFoundException { BufferedImage image = ImageIO.read(inputStream); LuminanceSource source = new BufferedImageLuminanceSource(image); Binarizer binarizer = new HybridBinarizer(source); BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer); Map<DecodeHintType, Object> hints = new HashMap<DecodeHintType, Object>(); hints.put(DecodeHintType.CHARACTER_SET, "UTF-8"); Result result = new MultiFormatReader().decode(binaryBitmap, hints);// 對圖像進行解碼 return result; }
二維碼的生成細節和原理html
QR碼的信息量和版本java
缺了一塊的二維碼,爲何也能掃描出來?shell
二維碼的基礎原理是什麼?maven