- 添加依赖
<dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.8.6</version> </dependency> <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>
- 測試
@GetMapping("/hello") public ResponseEntity<byte[]> hello() throws Exception { String info = "nan nan,You're so beautiful!"; byte[] qrcode = getQRCodeImage(info, 360, 360);//内容,宽,高 final HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.IMAGE_PNG); return new ResponseEntity<byte[]> (qrcode,headers, HttpStatus.OK); } public static byte[] getQRCodeImage(String text, int width, int height) throws Exception { QRCodeWriter qrCodeWriter = new QRCodeWriter(); Map<Object, Object> map = new HashMap<>(); map.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L); map.put(EncodeHintType.MARGIN, 1); BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height); ByteArrayOutputStream pngOutputStream = new ByteArrayOutputStream(); MatrixToImageWriter.writeToStream(bitMatrix, "PNG", pngOutputStream); byte[] pngData = pngOutputStream.toByteArray(); return pngData; }
posted @
2020-11-06 15:39
smile_暮雪
阅读(
125)
评论()
收藏
举报