利用ZXing工具生成二维码以及解析二维码

首先,我们需要下载Google的ZXing库。
下载地址:https://github.com/zxing/zxing

根据内容创建二维码

	/**
     * TODO 根据给定的内容生成二维码
     *
     * @param content        二维码内容
     * @param logoImagePath  logo图标的路径
     * @param needCompressed 是否需要压缩logo
     * @return 生成的二维码
     * @throws IOException
     * @throws WriterException
     */
	private static BufferedImage createImage(String content, String logoImagePath, boolean needCompressed) 
            throws IOException, WriterException {
        HashMap hints = new HashMap<EncodeHintType, Object>();
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);  // 纠错等级
        hints.put(EncodeHintType.CHARACTER_SET, CHARSET);
        hints.put(EncodeHintType.MARGIN, 1);  // 二维码两边空白区域大小
        BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, QRCODE_SIZE, QRCODE_SIZE, hints);
        int height = bitMatrix.getHeight();
        int width = bitMatrix.getWidth();
        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) ? 0xFF000000 : 0xFFFFFFFF);

        if (logoImagePath == null || "".equals(logoImagePath))
            return image;

        // 如果有logo,则插入logo图片
        QRCode.InsertImage(image, logoImagePath, needCompressed);
        return image;
    }

如果有logo则将logo插入到二维码中


```java
	/**
     * TODO 插入logo图片
     * @param sourceImage 原图片
     * @param logoImagePath logo图片所在的路径
     * @param needCompressed 是否需要压缩
     * @throws IOException
     */
    private static void InsertImage(BufferedImage sourceImage, String logoImagePath, boolean needCompressed) throws IOException {
        File file = new File(logoImagePath);
        if (!file.exists()){
            System.out.println("logo文件不存在!\n");
            return;
        }

        Image src = ImageIO.read(file);
        int width = src.getWidth(null);
        int height = src.getHeight(null);

        // 压缩二维码图片
        if (needCompressed) {
            if (width > LOGO_WIDTH)
                width = LOGO_WIDTH;
            if (height > LOGO_HEIGHT)
                height = LOGO_HEIGHT;

            Image image = src.getScaledInstance(width, height, Image.SCALE_SMOOTH);
            BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            Graphics g = tag.getGraphics();
            g.drawImage(image, 0, 0, null);
            g.dispose();  // 释放占有的资源
            src = image;
            // 直观的理解:Graphics2D 就相当于画笔,而BufferedImage 就是画笔绘制的结果。
        }

        // 插入logo
        Graphics2D graph = sourceImage.createGraphics();
        int x = (QRCODE_SIZE - width) / 2;
        int y = (QRCODE_SIZE - height) / 2;
        graph.drawImage(src, x, y, width, height, null);
        Shape shape = new RoundRectangle2D.Float(x, y, width, width, 6, 6);
        graph.setStroke(new BasicStroke(3f));
        graph.draw(shape);
        graph.dispose();
    }

解码过程

	/**
     * TODO 解析二维码内容
     * @param file 二维码
     * @return 二维码包含的信息
     * @throws Exception
     */
    public static String decode(File file) throws Exception {
        BufferedImage image;
        image = ImageIO.read(file);
        if (image == null) {
            return null;
        }
        BufferedImageLuminanceSource source = new BufferedImageLuminanceSource(image);
        BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
        HashMap hints = new HashMap<DecodeHintType, Object>();
        hints.put(DecodeHintType.CHARACTER_SET, CHARSET);
        Result result = new MultiFormatReader().decode(bitmap, hints);
        String resultStr = result.getText();
        return resultStr;
    }

源码下载地址:https://github.com/Liyzy/ZXing-QRCode
开发环境:idea 2018.2

转载:原文链接包含生成原理:https://blog.csdn.net/weixin_36191602/article/details/82466148

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值