赞
踩
1.maven依赖
- <!-- https://mvnrepository.com/artifact/com.google.zxing/core -->
- <dependency>
- <groupId>com.google.zxing</groupId>
- <artifactId>core</artifactId>
- <version>3.3.0</version>
- </dependency>
-
- <!-- https://mvnrepository.com/artifact/com.google.zxing/javase -->
- <dependency>
- <groupId>com.google.zxing</groupId>
- <artifactId>javase</artifactId>
- <version>3.3.0</version>
- </dependency>
2.生成二维码
- public static void main(String[] args) throws WriterException, IOException {
- int width = 300;
- int height = 300;
- String format = "png";
- String contents = "http://www.baidu.com" ;
-
- //定义二维码参数
- Map<EncodeHintType, Object> hints = new HashMap<>();
- hints.put(EncodeHintType.CHARACTER_SET,"UTF-8");
- hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
- hints.put(EncodeHintType.MARGIN,2);
-
- BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, BarcodeFormat.QR_CODE,width,height,hints);
- Path file = new File("D:/imooc.png").toPath();
- MatrixToImageWriter.writeToPath(bitMatrix,format,file);
- }
3.解析二维码
- public static void main(String[] args) throws IOException, NotFoundException {
- MultiFormatReader multiFormatReader = new MultiFormatReader();
-
- File file = new File("E:/alipay1.png");
- BufferedImage bufferedImage = ImageIO.read(file);
- BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(bufferedImage)));
- Map hints = new HashMap<>();
- hints.put(EncodeHintType.CHARACTER_SET,"UTF-8");
- Result result = multiFormatReader.decode(binaryBitmap,hints);
- System.out.println(result.toString());
- System.out.println(result.getBarcodeFormat());
- System.out.println(result.getText());
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。