<!DOCTYPE html><htmllang="en"><head><metacharset="UTF-8"/><title></title></head><body><!--The thymeleaf syntax is used here,you need to write your own path--><imgsrc="/qr-img?code_url=${code_url}"style="width:300px;height:300px;"/></body></html>
import com.google.zxing.common.BitMatrix;import javax.imageio.ImageIO;import java.awt.image.BufferedImage;import java.io.File;import java.io.IOException;import java.io.OutputStream;/**
* 二維碼的生成需要藉助MatrixToImageWriter類,該類是由Google提供的,可以將該類直接拷貝到源碼中使用
* Created by sam on 16/4/12.
*/publicclassMatrixToImageWriter{privatestaticfinalint BLACK =0xFF000000;privatestaticfinalint WHITE =0xFFFFFFFF;privateMatrixToImageWriter(){}publicstatic BufferedImage toBufferedImage(BitMatrix matrix){int width = matrix.getWidth();int height = matrix.getHeight();
BufferedImage image =newBufferedImage(width, height, BufferedImage.TYPE_INT_RGB);for(int x =0; x < width; x++){for(int y =0; y < height; y++){
image.setRGB(x, y, matrix.get(x, y)? BLACK : WHITE);}}return image;}publicstaticvoidwriteToFile(BitMatrix matrix, String format, File file)throws IOException {
BufferedImage image =toBufferedImage(matrix);if(!ImageIO.write(image, format, file)){thrownewIOException("Could not write an image of format "+ format +" to "+ file);}}publicstaticvoidwriteToStream(BitMatrix matrix, String format, OutputStream stream)throws IOException {
BufferedImage image =toBufferedImage(matrix);if(!ImageIO.write(image, format, stream)){thrownewIOException("Could not write an image of format "+ format);}}}