利用google的zxing快速生成QRCode
1.导入jar包,如果是非maven工程就去mvnrepository.com搜索zxing,下载本jar包即可
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.3.0</version>
</dependency>
2.写上下面代码即可,可创建一个main方法来测试,下面标识出来了对应的包
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import java.io.File;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
public class GenerateQRCode {
public static void main(String[] args) {
int width = 300;
int height = 300;
String format = "png";
String content = "https://www.cn.net";
//定义二维码参数
Map<Object,Object> map = new HashMap<>();
map.put(EncodeHintType.CHARACTER_SET,"utf-8");
map.put(EncodeHintType.ERROR_CORRECTION,ErrorCorrectionLevel.M);
map.put(EncodeHintType.MARGIN,2);
try {
BitMatrix encode = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height);
Path file = new File("C:/code/img.png").toPath();
MatrixToImageWriter.writeToPath(encode,format,file);
} catch (Exception e) {
e.printStackTrace();
}
}
}
运行main方法即可!
注意:此处的路径需要存在,没有做路径不存在去新增路径的方法。
标签:google,JAVA,java,zxing,QRCode,import,EncodeHintType,com From: https://blog.51cto.com/u_15932265/5993564