Java +zxing二维码生成测试

二维码生成

maven库

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

QrCodeGenerator代码


public class QrCodeGenerator {
    /**
     * 生成二维码,
     *
     * @param text     二维码存储信息
     * @param width    图片宽度
     * @param height   图片高度
     * @param filePath 生成图片路径
     * @throws WriterException
     * @throws IOException
     */
    public static void generateQRCodeImage(String text, int width, int height, String filePath) throws WriterException, IOException {
        QRCodeWriter qrCodeWriter = new QRCodeWriter();
        BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height);
        Path path = FileSystems.getDefault().getPath(filePath);
        MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path);
        MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path);

    }


}

测试代码

public static void main(String[] args) {
private static final String QR_CODE_IMAGE_PATH = "C:\\Users\\Desktop\\QrCode\\QRCode.png";
try {
     QrCodeGenerator.generateQRCodeImage("二维码存储信息", 350, 350, QR_CODE_IMAGE_PATH);
  } catch (WriterException e) {
      System.out.println("WriterException :: " + e.getMessage());
  } catch (IOException e) {
      System.out.println("OException :: " + e.getMessage());
  }
}

测试结果
在这里插入图片描述

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页