文章目录
前言
扫码跳转地址,解析二维码,连接wifi。
一、加入依赖
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>android-core</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>android-integration</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.3.0</version>
</dependency>
二、二维码制作
//图片的宽度和高度 int width = 300; int height = 300; //图片的格式 String format = "png"; //内容,此处放你想要放的内容(例如网页链接),若是网址链接,要加上 "http://" 才能被微信识别, //地址:"http://baidu.com" //扫码连接wife需要特定的字符串,自行搜索 String content = "这里填你想输入的信息"; //定义二维码的参数 HashMap hints = new HashMap(); //编码 hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); //容错等级,与二维码存储的大小有关,一般选择M hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M); //边距 hints.put(EncodeHintType.MARGIN, 2); //生成二维码 try { BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints); //存放二维码路径 Path file = new File("E:/img.png").toPath(); MatrixToImageWriter.writeToPath(bitMatrix, format, file); } catch (Exception e) { e.printStackTrace(); }
三、二维码解析
try { MultiFormatReader multiFormatReader = new MultiFormatReader(); //二维码的存放位置 File file = new File("E:/img.png"); //创建一个图片缓冲区 BufferedImage image = ImageIO.read(file); //对图片进行解析 BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image))); //定义二维码的参数 HashMap hints = new HashMap(); //编码 hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); //得到解析后的result Result result = multiFormatReader.decode(binaryBitmap, hints); System.out.println("解析结果:" + result.toString()); System.out.println("二维码格式类型·:" + result.getBarcodeFormat()); } catch (NotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }
把代码拷贝到mian方法里面直接跑就行。
总结
以上就是今天要讲的内容,本文仅仅简单介绍了二维码的使用。