org.springframework.boot
spring-boot-starter-web
com.google.zxing
core
3.4.1
com.google.zxing
javase
3.4.1
org.projectlombok
lombok
2. 创建工具类
package com.chen.qrcode.util;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Path;
public class QrCodeUtil {
public static byte[] generateQrCodeByte(String text, int width, int height) throws WriterException, IOException {
QRCodeWriter qrCodeWriter = new QRCodeWriter();
BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height);
ByteArrayOutputStream pngOutputStream = new ByteArrayOutputStream();
MatrixToImageWriter.writeToStream(bitMatrix, "PNG", pngOutputStream);
return pngOutputStream.toByteArray();
}
public static void generateQrCodeImage(String text, int width, int height, String filePath) throws WriterException, IOException {
QRCodeWriter qrCodeWriter = new QRCodeWriter();
BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height);
Path path = FileSystems.getDefault().getPath(filePath);
MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path);
}
}
3. 创建测试控制层
package com.chen.qrcode.controller;
import com.chen.qrcode.util.QrCodeUtil;
import com.google.zxing.WriterException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Objects;
@RestController
@RequestMapping("/qr-code")
@Slf4j
public class QrCodeController {
public static String url = "https://www.baidu.com/";
@GetMapping("methodOne")
public ResponseEntity methodOne() {
byte[] qrCode = null;
try {
qrCode = QrCodeUtil.generateQrCodeByte(url, 350, 350);
} catch (Exception e) {
log.info("Exception:{}", e.getMessage());
}
// Header设置文件类型(对于ResponseEntity响应的方式,必须设置文件类型)
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.IMAGE_PNG);
return new ResponseEntity<>(qrCode, headers, HttpStatus.CREATED);
}
@GetMapping("methodTwo")
public void methodTwo(HttpServletResponse response) throws IOException {
byte[] qrCode = null;
try {
qrCode = QrCodeUtil.generateQrCodeByte(url, 350, 350);
} catch (Exception e) {
log.info("Exception:{}", e.getMessage());
}
// Header设置文件类型(ContentType不设置也没事)
response.setContentType(MediaType.IMAGE_PNG_VALUE);
response.getOutputStream().write(Objects.requireNonNull(qrCode));
}
@GetMapping("methodThree")
public void methodThree() throws IOException, WriterException {
//下载路径及文件名
String filePath = "D:\or-code.png";
QrCodeUtil.generateQrCodeImage(url, 350, 350, filePath);
}
}
4. 测试方法
-
methodOne
-
methodTwo
-
methodThree



