使用zxing生成二维码

下载zxing

点击zxing下载

具体代码
public class QRCodeMaker {

    private static final int PIC_WIDTH = 400;
    private static final int PIC_HEIGHT = 400;

    private static final int BLUE = 0xFF3ED547;
    private static final int WHITE = 0xFFFFFFFF;
    private static final int BLACK = 0xFF000000;
    private static final int RED = 0xFFFF0000;

    private static final String IMG_FORMATE = "jpg";

    private static final int LOGO_BORDER_WIDTH = 2;//LOGO边框宽度
    private static final Color LOGO_BORDER_COLOR = Color.WHITE;//LOGO边框颜色

    // TODO:logo file path
    private static final String LOGO_PATH = "D:" + File.separator + "logo.png";

    /**
     * 生成二维码图片
     * @param info  二维码信息
     * @param out  目的图片地址
     */
    public static void MakeQRCode(String info, String out) throws WriterException, IOException {
        File outFile = new File(out);

        if (!outFile.getParentFile().exists()) {
            outFile.getParentFile().mkdirs();
        }

        BufferedImage image = toBufferedImg(getBitMatrix(info));

        ImageIO.write(image, IMG_FORMATE, outFile);

    }

    /**
     * 生成带logo的二维码图片
     * @param info      图片信息
     * @param out       输出图片地址
     * @param logoPath  logo图片地址
     * @throws WriterException
     * @throws IOException
     */
    public static void MakeQrCodeWithLogo(String info, String out, String logoPath) throws WriterException, IOException {
        File outFile = new File(out);

        if (!outFile.getParentFile().exists()) {
            outFile.getParentFile().mkdirs();
        }

        BufferedImage image = toBufferedImg(getBitMatrix(info));

        addLogo2QrCode(image, new File(logoPath));

        ImageIO.write(image, IMG_FORMATE, outFile);
    }

    /**
     * get bitMatrix from content
     * @param conten    qr_code info
     * @return
     * @throws WriterException
     */
    private static BitMatrix getBitMatrix(String conten) throws WriterException {
        MultiFormatWriter writer = new MultiFormatWriter();

        Map hint = new HashMap();
        hint.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        hint.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        return writer.encode(conten, BarcodeFormat.QR_CODE, PIC_WIDTH, PIC_HEIGHT, hint);
    }

    private static BufferedImage toBufferedImg(BitMatrix bitMatrix) {
        int width = bitMatrix.getWidth();
        int height = bitMatrix.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, bitMatrix.get(x, y) ? BLUE : WHITE);
            }
        }

        return image;
    }

    private static void addLogo2QrCode(BufferedImage qrImage, File logoFile) throws IOException {

        if (qrImage == null) {
            throw new RuntimeException("qrpic is null");
        }

        if (!logoFile.exists()) {
            throw new FileNotFoundException("can not found logo file at " + logoFile.getAbsolutePath());
        }

        BufferedImage logo = ImageIO.read(logoFile);

        int logoWidth = logo.getWidth();
        int logoHeight = logo.getHeight();

        // get logo position
        int x = (qrImage.getWidth() - logoWidth) / 2;
        int y = (qrImage.getHeight() - logoHeight) / 2;

        // draw logo on qr picture
        Graphics2D graphics2D = qrImage.createGraphics();

        graphics2D.drawImage(logo, x, y, logoWidth, logoHeight, null);
        graphics2D.drawRoundRect(x, y, logoWidth, logoHeight, 15, 15);

        graphics2D.setStroke(new BasicStroke(LOGO_BORDER_WIDTH));
        graphics2D.setColor(LOGO_BORDER_COLOR);
        graphics2D.drawRect(x, y, logoWidth, logoHeight);

        graphics2D.dispose();
    }

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值