云记
首页
常用软件
操作系统
技术分享
东云生态
  • 技术网站
  • 其他
关于我们
首页
常用软件
操作系统
技术分享
东云生态
  • 技术网站
  • 其他
关于我们
  • 前端技术

    • JavaScript基础知识
    • 其他
    • 正则表达式
    • Favicon
  • 后端技术

    • 数据结构
    • 开发规范
    • 路径匹配规则
    • Java字符串
    • 二维码的生成与读取
    • 雪花算法
    • SpringBoot注解
    • SpringBoot自定义banner
    • SpringBoot日志
    • Util、POJO、domain、entity、model、dao、view、mapper、service、controller的作用和区别分析
    • SpringSecurity
  • 数据库

    • MySQL
    • Oracle
  • 面试

    • Java面试

解决方案:

pom引入

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

<dependency>
  <groupId>com.google.zxing</groupId>
  <artifactId>javase</artifactId>
  <version>3.4.1</version>
</dependency>
  1. 生成二维码
    @Test
    void test() {
        int width = 300;
        int hight = 300;
        String format = "png";
        String content = "测试Demo";
        // 定义二维码参数
        HashMap<EncodeHintType, Object> map = new HashMap<>();
        map.put(EncodeHintType.CHARACTER_SET, "utf-8");
        map.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
        map.put(EncodeHintType.MARGIN, 2);
        try {
            BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, hight, map);
            Path file = new File("D:\\image.png").toPath();
            MatrixToImageWriter.writeToPath(bitMatrix, format, file);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
  1. 读取二维码
@Test
    void test() {
        MultiFormatReader multiFormatReader = new MultiFormatReader();
        File file = new File("D:\\image.png");
        try {
            BufferedImage image = ImageIO.read(file);
            LuminanceSource luminanceSource = new BufferedImageLuminanceSource(image);
            Binarizer binarizer = new HybridBinarizer(luminanceSource);
            BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);
            // 定义二维码参数
            Map<DecodeHintType, Object> hints = new HashMap<>();
            hints.put(DecodeHintType.CHARACTER_SET, "utf-8");
            Result result = multiFormatReader.decode(binaryBitmap, hints);
            System.out.println(result.toString());
            System.out.println(result.getBarcodeFormat());
            System.out.println(result.getText());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
最后更新时间:
贡献者: xiaozhe
上一篇
Java字符串
下一篇
雪花算法