阅读背景:

java Qrcode生成二维码并输出到前端_red_blue2的博客_java 生成二维码并输出流

来源:互联网 

java生成二维码输出到并前端

直接以图片形式返回给前端

public class QrCodeUtil {
    private static final int BLACK = -16777216;
    private static final int WHITE = -1;

    public QrCodeUtil() {
    }

    public static BitMatrix createQrCode(String path) {
        try {
            Map<EncodeHintType, String> hints = new HashMap();
            hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
            BitMatrix bitMatrix = (new MultiFormatWriter()).encode(path, BarcodeFormat.QR_CODE, 400, 400, hints);
            return bitMatrix;
        } catch (Exception var3) {
            var3.printStackTrace();
            return null;
        }
    }

    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);
        }
    }

    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);
        }
    }

    private static BufferedImage toBufferedImage(BitMatrix matrix) {
        int width = matrix.getWidth();
        int height = matrix.getHeight();
        BufferedImage image = new BufferedImage(width, height, 1);

        for(int x = 0; x < width; ++x) {
            for(int y = 0; y < height; ++y) {
                image.setRGB(x, y, matrix.get(x, y) ? -16777216 : -1);
            }
        }

        return image;
    }
public 



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: