zxing实现生成二维码与解析

生成二维码的开源项目有SwetakeQRCode、BarCode4j、Zxing......

前端有JQuery-qrcode,同样能实现生成二维码。下面我用的是Zxing


pom.xml的配置

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.zhaomei.test</groupId>
  <artifactId>qrcode</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>
  <name>qrcode</name>
  <url>http://maven.apache.org</url>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
      <dependency>  
            <groupId>com.google.zxing</groupId>  
            <artifactId>core</artifactId>  
            <version>3.0.0</version>  
        </dependency>  
        <dependency>  
            <groupId>com.google.zxing</groupId>  
            <artifactId>javase</artifactId>  
            <version>3.0.0</version>  
        </dependency>  
  </dependencies> 
</project>

java代码生成二维码(content需要什么内容就写哪个内容跳转)

package com.zhaomei.test.qrcode;
import java.io.File;
import java.nio.file.Path;
import java.util.HashMap;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
public class QRCodeTest {
//生成二维码
public static void main(String[] args) throws Exception {
int width =300 ;
int height =300 ;
String format ="png";
String content="http://www.jthinking.com";
//定义二维码的参数
HashMap hints =new HashMap();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
hints.put(EncodeHintType.MARGIN,2);
try {
BitMatrix  bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height,hints);
   Path file =new File("D:/img.png").toPath();
   MatrixToImageWriter.writeToPath(bitMatrix, format, file);
} catch (WriterException e) {
e.printStackTrace();
}
}
}

java代码解析二维码

package com.zhaomei.test.qrcode;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.HashMap;
import javax.imageio.ImageIO;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;
public class ReadQRCode {
   public static void main(String[] args) throws Exception {
MultiFormatReader  formatReader =new MultiFormatReader();
File file =new File("D:/img.png");
BufferedImage  image =ImageIO.read(file);
//生成二维码参数
HashMap hints =new HashMap();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
BinaryBitmap  binaryBitmap =new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image)));
    Result result= formatReader.decode(binaryBitmap,hints);
    System.out.println("解析结果是"+result.toString());
    System.out.println("二维码文本内容"+result.getText());
    System.out.println("二维码格式内容"+result.getBarcodeFormat());
   }
}
解析二维码打印数据

解析结果是http://www.jthinking.com
二维码文本内容http://www.jthinking.com
二维码格式内容QR_CODE

生成的二维码图:



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值