当前位置:   article > 正文

使用zxing生成和读取QRCode二维码_zixing 用法

zixing 用法

1.maven依赖

  1. <!-- https://mvnrepository.com/artifact/com.google.zxing/core -->
  2. <dependency>
  3. <groupId>com.google.zxing</groupId>
  4. <artifactId>core</artifactId>
  5. <version>3.3.0</version>
  6. </dependency>
  7. <!-- https://mvnrepository.com/artifact/com.google.zxing/javase -->
  8. <dependency>
  9. <groupId>com.google.zxing</groupId>
  10. <artifactId>javase</artifactId>
  11. <version>3.3.0</version>
  12. </dependency>
2.生成二维码

  1. public static void main(String[] args) throws WriterException, IOException {
  2. int width = 300;
  3. int height = 300;
  4. String format = "png";
  5. String contents = "http://www.baidu.com" ;
  6. //定义二维码参数
  7. Map<EncodeHintType, Object> hints = new HashMap<>();
  8. hints.put(EncodeHintType.CHARACTER_SET,"UTF-8");
  9. hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
  10. hints.put(EncodeHintType.MARGIN,2);
  11. BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, BarcodeFormat.QR_CODE,width,height,hints);
  12. Path file = new File("D:/imooc.png").toPath();
  13. MatrixToImageWriter.writeToPath(bitMatrix,format,file);
  14. }
3.解析二维码

  1. public static void main(String[] args) throws IOException, NotFoundException {
  2. MultiFormatReader multiFormatReader = new MultiFormatReader();
  3. File file = new File("E:/alipay1.png");
  4. BufferedImage bufferedImage = ImageIO.read(file);
  5. BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(bufferedImage)));
  6. Map hints = new HashMap<>();
  7. hints.put(EncodeHintType.CHARACTER_SET,"UTF-8");
  8. Result result = multiFormatReader.decode(binaryBitmap,hints);
  9. System.out.println(result.toString());
  10. System.out.println(result.getBarcodeFormat());
  11. System.out.println(result.getText());
  12. }




本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/715815
推荐阅读
相关标签
  

闽ICP备14008679号