java实现二维码的生成和解析

时间:2020-10-21
本文章向大家介绍java实现二维码的生成和解析,主要包括java实现二维码的生成和解析使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

二维码现在已经在我们的生活中大量使用,如手机支付,扫一扫登录,扫一扫加好友等。

生成二维码

添加maven依赖

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

使用工具生成二维码

public class Client {

  public static void main(String[] args) throws Exception {
    String content = "中国";
    Map<EncodeHintType, Object> hints = new HashMap<>();
    hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
    hints.put(EncodeHintType.MARGIN, 0);
    int onColor = 0xFF000000;     //前景色
    int offColor = 0xBFB0A8;    //背景色
    MatrixToImageConfig config = new MatrixToImageConfig(onColor, offColor);
    BitMatrix bitMatrix = new MultiFormatWriter()
        .encode(content, BarcodeFormat.QR_CODE, 300, 300, hints);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    MatrixToImageWriter.writeToStream(bitMatrix, "png", baos, config);
    System.out.println(Base64.getEncoder().encodeToString(baos.toByteArray()));
  }

}

生成的二维码如下

zxing包提供了多种码的创建,二维码,条形码等。

解析二维码

public class Client {

  public static void main(String[] args) throws Exception {
    InputStream is = new FileInputStream("D:/a.png");
    BufferedImage image = ImageIO.read(is);
    LuminanceSource source = new BufferedImageLuminanceSource(image);
    BinaryBitmap bb = new BinaryBitmap(new HybridBinarizer(source));
    HashMap<DecodeHintType, String> hints = new HashMap<>();
    hints.put(DecodeHintType.CHARACTER_SET, "utf-8");
    Result result = new MultiFormatReader().decode(bb, hints);
    System.out.println("二维码格式类型:" + result.getBarcodeFormat());
    System.out.println("二维码文本内容:" + result.getText());
  }

}

解析出的文本为:中国,为生成二维码时填入的内容。

生成条形码

public class Client {

  public static void main(String[] args) throws Exception {
    String content = "123456789";
    Map<EncodeHintType, Object> hints = new HashMap<>();
    hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
    hints.put(EncodeHintType.MARGIN, 0);
    int onColor = 0xFF000000;     //前景色
    int offColor = 0xBFB0A8;    //背景色
    MatrixToImageConfig config = new MatrixToImageConfig(onColor, offColor);
    BitMatrix bitMatrix = new MultiFormatWriter()
        .encode(content, BarcodeFormat.CODE_128, 300, 100, hints);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    MatrixToImageWriter.writeToStream(bitMatrix, "png", baos, config);
    System.out.println(Base64.getEncoder().encodeToString(baos.toByteArray()));
  }

}

BarcodeFormat.CODE_128 表示条形码
生成的条形码如下

解析条形码

public class Client {

  public static void main(String[] args) throws Exception {
    InputStream is = new FileInputStream("D:/b.png");
    BufferedImage image = ImageIO.read(is);
    LuminanceSource source = new BufferedImageLuminanceSource(image);
    BinaryBitmap bb = new BinaryBitmap(new HybridBinarizer(source));
    HashMap<DecodeHintType, String> hints = new HashMap<>();
    hints.put(DecodeHintType.CHARACTER_SET, "utf-8");
    Result result = new MultiFormatReader().decode(bb, hints);
    System.out.println("二维码格式类型:" + result.getBarcodeFormat());
    System.out.println("二维码文本内容:" + result.getText());
  }
}

解析出的内容为:123456789

原文地址:https://www.cnblogs.com/strongmore/p/13855155.html