java二维码生成代码

2019-1-4

片段 1片段 2


code

import com.google.zxing.common.BitMatrix;

import java.awt.image.BufferedImage;
import java.awt.Graphics2D;
import java.awt.Image;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Hashtable;

import javax.imageio.ImageIO;

import org.springframework.core.io.ClassPathResource;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;

/**
 * 二维码生成工具
 * @Auth
 */
public final class MatrixToImageWriter {

    private static final int BLACK = 0xFF000000;
    private static final int WHITE = 0xFFFFFFFF;
    private static final int MARGIN = 1; //边框

    private static final String FORMAT = "png";

    private MatrixToImageWriter() {
    }

    public static void createRqCode(String textOrUrl, int width, int height, OutputStream toStream)
            throws WriterException, IOException {

        Hashtable<EncodeHintType, Object> hints = new Hashtable<>();
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); // 内容所使用字符集编码
        hints.put(EncodeHintType.MARGIN, new Integer(MARGIN));

        BitMatrix bitMatrix = new MultiFormatWriter().encode(textOrUrl, BarcodeFormat.QR_CODE, width, height, hints);

        BufferedImage image = toBufferedImage(bitMatrix);
        //应用LOGO
        //applyLogo(image);
        writeToStream(image, FORMAT, toStream);

    }

    private static void applyLogo(BufferedImage image) throws IOException {

        Graphics2D gs = image.createGraphics();

        ClassPathResource resource = new ClassPathResource("logo.png");//logo图片

        // 载入logo
        Image img = ImageIO.read(resource.getFile());

        int left = image.getWidth() / 2 - img.getWidth(null) / 2;
        int top = image.getHeight() / 2 - img.getHeight(null) / 2;

        gs.drawImage(img, left, top, null);
        gs.dispose();
        img.flush();

    }

    private 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(BufferedImage image, String format, File file) throws IOException {

        if (!ImageIO.write(image, format, file)) {
            throw new IOException("Could not write an image of format " + format + " to " + file);
        }
    }

    public static void writeToStream(BufferedImage image, String format, OutputStream stream) throws IOException {
        if (!ImageIO.write(image, format, stream)) {
            throw new IOException("Could not write an image of format " + format);
        }
    }
}


依赖包

<dependency>
    <groupId>com.google.zxing</groupId>
    <artifactId>core</artifactId>
    <version>3.3.0</version>
</dependency>

下载地址

百度网盘
密码: