Java使用zxing生成解读QRcode二维码

来源:互联网 发布:pdf expert mac破解版 编辑:程序博客网 时间:2024/06/05 06:40

1.maven的pom配置jar包,如果不实用maven请手动下载jar包

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

2.生成二维码图片

public static void main( String[] args ) {     //生成图片的一些基本参数     int width = 300;     int height = 300;     String format = "png";     //这里是内容     String content = "看着你们加班,我不加班,我好开心!";     //定义二维码参数     Map<EncodeHintType, Object> params = new HashMap<EncodeHintType, Object>();     params.put(EncodeHintType.CHARACTER_SET, "utf-8");     params.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);     params.put(EncodeHintType.MARGIN, 2);     //生成二维码     try {          BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, params);          Path file = new File("D:/image/img.png").toPath();          MatrixToImageWriter.writeToPath(bitMatrix, format, file);     } catch (Exception e) {          // TODO Auto-generated catch block          e.printStackTrace();     }}

3.读取二维码中信息

public static void main(String[] args) {     MultiFormatReader multiFormatReader = new MultiFormatReader();     //读取图片     File file = new File("D:/image/img.png");     try {          BufferedImage image = ImageIO.read(file);          BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image)));          Result result = multiFormatReader.decode(binaryBitmap);          //这里是返回的信息          System.out.println(result.toString());          System.out.println(result.getBarcodeFormat());          System.out.println(result.getText());     } catch (Exception e) {          // TODO Auto-generated catch block          e.printStackTrace();     }}
原创粉丝点击