1. 程式人生 > 實用技巧 >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