首先导入包(这里介绍maven方式)

<!-- https://mvnrepository.com/artifact/com.google.zxing/core -->
<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>

接着写一个工具类 MatrixToImageWriter.java

package com.heitian.ssm.util;
import com.google.zxing.common.BitMatrix;
import javax.imageio.ImageIO;
import java.io.File;
import java.io.OutputStream;
import java.io.IOException;
import java.awt.image.BufferedImage;
/** 二维码工具类
 * Created by benhailong on 2017/9/14.
 */
public final class MatrixToImageWriter {
    private static final int BLACK = 0xFF000000;
    private static final int WHITE = 0xFFFFFFFF;
    private MatrixToImageWriter() {}
    public static BufferedImage toBufferedImage(BitMatrix matrix) {
        int width = matrix.getWidth();
        int height = matrix.getHeight();
        BufferedImage image = new BufferedImage(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;
    }
    public static void writeToFile(BitMatrix matrix, String format, File file)
            throws IOException {
        BufferedImage image = toBufferedImage(matrix);
        if (!ImageIO.write(image, format, file)) {
            throw new IOException("Could not write an image of format " + format + " to " + file);
        }
    }
    public static void writeToStream(BitMatrix matrix, String format, OutputStream stream)
            throws IOException {
        BufferedImage image = toBufferedImage(matrix);
        if (!ImageIO.write(image, format, stream)) {
            throw new IOException("Could not write an image of format " + format);
        }
    }
}

最后写一个公共方法给以调用 GetZxing.java

package com.heitian.ssm.util;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
/**
 * Created by benhailong on 2017/9/14.
 */
public class GetZxing {
    public String getZxingImgUrl(String content,HttpServletRequest request){
        String imgUrl =null;
        try {
            String path = "/Users/benhailong/IdeaProjects/lixian/target/web-ssm/uploadfile/code";
            MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
            Map hints = new HashMap();
            //内容所使用编码
            hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
            //设置二维码的容错等级L级:约可纠错7%的数据码字;M级:15;Q级:25% ;H级:30;
            hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
            hints.put(EncodeHintType.MARGIN,1);//边距
            BitMatrix bitMatrix = multiFormatWriter.encode(content, BarcodeFormat.QR_CODE, 300, 300, hints);
            //生成二维码
            String s = UUID.randomUUID().toString().replaceAll("-", "");
            File outputFile = new File(path,s+".jpg");
            MatrixToImageWriter.writeToFile(bitMatrix, "jpg", outputFile);
            imgUrl = "http://localhost:8080/uploadfile/code/"+s+".jpg";
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return imgUrl;
        }
    }
}

调用 controller 层

GetZxing getZxing = new GetZxing();
String url = getZxing.getZxingImgUrl("111",request);
System.out.println("url="+url);

注意:参数 content 是需要给二维码的星系,必传,多个参数请采用 json 传参数,request 必传

返回值:url是返回的二维码路径