今天我们就来学习一下zxing的用法,这里只涉及到java se的部分。

如果在java se环境中使用zxing,需要在maven的pom.xml中添加依赖:

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

一、根据内容生成二维码

public void encodeNoReturn(String content) {
    try {
        Path path = new File("file/huhx.png").toPath();
        BitMatrix matrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, 300, 300);
        MatrixToImageWriter.writeToPath(matrix, "png", path);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

 

二、根据二维码读取内容

public static void main(String[] args) {
    File file = new File("file/huhx.jpg");
    BufferedImage bufferedImage = null;
    try {
        bufferedImage = ImageIO.read(file);
    } catch (IOException e) {
        e.printStackTrace();
    }
    LuminanceSource source = new BufferedImageLuminanceSource(bufferedImage);
    BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
    Hashtable<DecodeHintType, String> hints = new Hashtable<DecodeHintType, String>();
    hints.put(DecodeHintType.CHARACTER_SET, "GBK");
    Result result = null;
    try {
        result = new MultiFormatReader().decode(bitmap, hints);
    } catch (NotFoundException e) {
        e.printStackTrace();
    }
    String url = result.getText();
    System.out.println(url);
}

 

友情链接

 

相关文章:

  • 2022-12-23
  • 2021-11-24
  • 2022-01-18
  • 2021-10-14
  • 2021-12-01
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-08-01
  • 2021-11-23
  • 2021-08-21
  • 2022-12-23
  • 2021-05-27
  • 2021-09-19
相关资源
相似解决方案