现在用二维码传递消息是如此的流行和快捷,二维码中 可存储的信息量比较大,容易识别,内容丰富,可以储存文本,链接,名片等等。并且现在支付宝微信等的支付都直接可以用扫描二维码进行支付,利用特定的扫码软件,能够解析二维码中的内容。在我的项目中,用到了需要存储一个二维码的链接,让用户直接扫码以后就可以下单的需求。经过查询,可以用Google的qrcodegencore.jar的类库直接生成二维码。附件中是实现生成二维码的jar包
接下来用两个步骤来实现此功能需求


1、生成二维码

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;
import org.apache.commons.lang3.StringUtils;

import javax.p_w_picpathio.ImageIO;
import java.awt.p_w_picpath.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Hashtable;

public class QRGenUtils {

    private static final int black = 0xFF000000;
    private static final int   = 0xFFFFFFFF;

    public static BufferedImage toBufferedImage(BitMatrix matrix) {
        int width = matrix.getWidth();
        int height = matrix.getHeight();
        BufferedImage p_w_picpath = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                p_w_picpath.setRGB(x, y, matrix.get(x, y) ? black : white);
            }
        }
        return p_w_picpath;
    }


    public static void writeToFile(BitMatrix matrix, String format, File file)
            throws IOException {
        BufferedImage p_w_picpath = toBufferedImage(matrix);
        ImageIO.write(p_w_picpath, format, file);
    }

    public static void createQRImage(String content, int width, int height, String path, String fileName) throws Exception {
        MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
        Hashtable hints = new Hashtable();
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        BitMatrix bitMatrix = multiFormatWriter.encode(content, BarcodeFormat.QR_CODE, width, height, hints);
        if (StringUtils.isNotBlank(path)) {
            if (!path.endsWith("/")) {
                path = path + "/";
            }
        } else {
            path = "";
        }
        String suffix = "jpg";
        if (fileName.indexOf(".") <= -1) {
            fileName = fileName + "." + suffix;
        } else {
            suffix = fileName.split("[.]")[1];
        }
        fileName = path + fileName;
        File file = new File(fileName);
        writeToFile(bitMatrix, suffix, file);
    }


    public static BufferedImage createQRImageBuffer(String content, int width, int height) throws  Exception{
        MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
        Hashtable hints = new Hashtable();
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        BitMatrix bitMatrix = multiFormatWriter.encode(content, BarcodeFormat.QR_CODE, width, height, hints);
        BufferedImage p_w_picpath = toBufferedImage(bitMatrix);
        return p_w_picpath;
    }
}


2、把生成BufferedImage数据流进行Base64编码

BufferedImage qrImageBuffer = QRGenUtils.createQRImageBuffer(content, 200, 200);
ByteArrayOutputStream os=new ByteArrayOutputStream();
ImageIO.write(qrImageBuffer, "png", os);
Base64 base64 = new Base64();
String base64Img = new String(base64.encode(os.toByteArray()));


然后把编码后图片在前端页面直接取值即可

<img id="cashier_page_p_w_picpath" src='data:img/jpg;base64,${base64Img }' style="display: none"/>