QRcode生成二维码、文件压缩打包工具类

本文介绍了一个Java工具类,用于生成、解析二维码并进行压缩打包。该工具类具备插入logo、多张二维码打包等高级功能,并附带单元测试确保功能正确性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前言

QR Code码,是由Denso公司于1994年9月研制的一种矩阵二维码符号,它具有一维条码及其它二维条码所具有的信息容量大、可靠性高、可表示汉字及图象多种文字信息、保密防伪性强等优点。(from baidu.com)

说明

该工具类提供二维码生成、logo图片插入、二维码解析、多张二维码打包压缩等方法。

生成二维码:

 private static BufferedImage createImage(String content, String imgPath,
                                             boolean needCompress, String productName) throws Exception {
        Hashtable<EncodeHintType, Object> hints = new Hashtable<>();
        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 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) ? 0xFF000000
                        : 0xFFFFFFFF);
            }
        }
        if (imgPath == null || "".equals(imgPath)) {
            return image;
        }
        // 插入图片
        QRCodeUtils.insertImage(image, imgPath, needCompress, productName);
        return image;
    }

解析二维码

    /**
     * 解析二维码
     *
     * @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));
        Result result;
        Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>();
        hints.put(DecodeHintType.CHARACTER_SET, CHARSET);
        result = new MultiFormatReader().decode(bitmap, hints);
        String resultStr = result.getText();
        return resultStr;
    }

二维码压缩打包:


    /***
     * 二维码打包
     **/
    @Test
    public void zipFiles() throws Exception {
        List<File> fileList = new ArrayList<>();
        String text = "java大神!";  //这里设置自定义网站url
        //    String logoPath = "C:\\Users\\Pictures\\lingxi.png";
        String destPath = "C:\\Users\\Pictures\\erweima";
        QRCodeUtils.mkOrClearDir(destPath);
        File file111 = QRCodeUtils.encode("图片名称111", text, null, destPath, true, "巡检路线1-站点1");
        File file222 = QRCodeUtils.encode("图片名称2222", text, null, destPath, true, "巡检路线1-站点2");
        System.out.println(file111.getPath());
        System.out.println(file222.getPath());
        fileList.add(file111);
        fileList.add(file222);
        String url = destPath + "二维码.zip";
        File zipFile = new File(url);
        // 调用压缩方法
        ZipMultiFileUtil.zipFiles(fileList.stream().toArray(File[]::new), zipFile);
        //将项目名称的文件夹 压缩为zip
        boolean flag = ZipMultiFileUtil.fileToZip(destPath, destPath, "二维码.zip");
        System.out.println("二维码压缩结果:" + flag);
        System.out.println("二维码压缩包地址:" + url);
    }

单元测试:

/***
     * 二维码生成
     **/
    @Test
    public void QRCreateImage() throws Exception {
        String text = "javaer!";  //这里设置自定义网站url
        //  String logoPath = "C:\\Users\\Pictures\\lingxi.png";
        String destPath = "C:\\Users\\Pictures\\";
        File file111 = QRCodeUtils.encode("图片名称111", text, null, destPath, true, "巡检路线");
        System.out.println(file111.getPath());
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值