zxing生成二维码

MatrixToImageWriter:
package common;

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;


public 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);
        }
    }
}
View Code
zxingQrCodeUtil:
package common;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import sdk.LogUtil;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;


public class zxingQrCodeUtil {
    /**
     * 生成web版本二维码(Stream)
     *
     * @param contents  要生成二维码的内容
     * @param format    二维码图片格式
     * @param response  response对象
     * @param width 二维码宽度
     * @param height    二维码高度
     * @throws IOException
     */
    public static void getQrCodeToStream(String contents, String format, HttpServletResponse response, int width, int height) throws IOException {
        if (contents != null && !"".equals(contents)) {
            ServletOutputStream stream = null;
            try {
                stream = response.getOutputStream();
//                QRCodeWriter writer = new QRCodeWriter();
//                BitMatrix bitMatrix = writer.encode(contents, BarcodeFormat.QR_CODE, width, height);
                MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
                Map<EncodeHintType, String> hints = new HashMap<>();
                hints.put(EncodeHintType.CHARACTER_SET, DemoBase.encoding);
                BitMatrix bitMatrix = multiFormatWriter.encode(contents, BarcodeFormat.QR_CODE, width, height, hints);
                MatrixToImageWriter.writeToStream(bitMatrix, format, stream);
            } catch (WriterException e) {
                e.printStackTrace();
                LogUtil.writeErrorLog("生成二维码失败!");
            } finally {
                if (stream != null) {
                    stream.flush();
                    stream.close();
                }
            }
        }
    }

    /**
     * 生成web版本二维码(img)
     * @param contents 要生成二维码的内容
     * @param format    二维码图片格式
     * @param file  二维码图片地址
     * @param width 二维码宽度
     * @param height    二维码高度
     * @throws IOException
     */
    public static void getQrCodeToFile(String contents, String format, File file, int width, int height) throws IOException {
        if (contents != null && !"".equals(contents)) {
            try {
                MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
                Map<EncodeHintType, String> hints = new HashMap<>();
                hints.put(EncodeHintType.CHARACTER_SET, DemoBase.encoding);
                BitMatrix bitMatrix = multiFormatWriter.encode(contents, BarcodeFormat.QR_CODE, width, height, hints);
                MatrixToImageWriter.writeToFile(bitMatrix, format, file);
            } catch (WriterException e) {
                e.printStackTrace();
                LogUtil.writeErrorLog("生成二维码失败!");
            }
        }
    }
}
View Code
// 获取二维码
qrCode = rspData.get("qrCode");
String realPath = req.getServletContext().getRealPath("/");
realPath = realPath.substring(0, realPath.indexOf("out"));
File file = new File(realPath + "web/img/2013.jpg");//图片输出路径
zxingQrCodeUtil.getQrCodeToFile(qrCode, "jpg", file, 200, 200);
// 显示二维码
String contextPath = req.getContextPath();
String url = req.getRequestURL().toString();
String path = url.substring(0, (url.indexOf(contextPath) + contextPath.length()));
String imgPath = path + "/img/2013.jpg";
resp.getWriter().write("<p>"+qrCode+"</p><div><img src='"+imgPath+"' alt="" ></div>");