依赖:
com.google.zxing
方法参数:
EncodeHintType枚举类 传递给writers的一系列参数(并没有列举全面)
ERROR_CORRECTION 指定二维码容错等级
ErrorCorrectionLevel(M,L,H,Q四个等级由低到高)
CHARACTER_SET 编码格式
MARGIN 指定生成条形码时的空白(像素为单位)
DATA_MATRIX_SHAPE 矩形形状
PDF417_COMPACTION 指定为PDF417压缩格式
PDF417_DIMENSIONS 指定PDF147最大和最小行数和列数
QR_VERSION 指定qr版本
生成方法:
MultiFormatWriter().encode(内容,BarcodeFormat.QR_CODE,width,height,map)
内容:
就是你想把什么变成二维码
BarcodeFormat二维码的格式(一个枚举)
width:宽
height:高
map:存的就是EncodeHintType的参数
Image类
抽象类 其实现类为BufferedImage,一个带缓存区的图像类。
主要作用:将一幅图片加载到内存中
//创建一个不带透明色的对象
BufferedImage bimage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
代码:
public class QrCodeGenerate {
public static void main(String[] args) {
int width = 300;
int height = 300;
//制定图片格式
String format="png";
//内容
String content = "我是内容";
//定义二维码的参数
HashMap map = new HashMap();
map.put(EncodeHintType.CHARACTER_SET, "utf-8");//编码
map.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
map.put(EncodeHintType.MARGIN, 2);
//生成二维码
try {
BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height,map);
Path file = new File("D:\\image\\1.jpg").toPath();//二维码存放路径
MatrixToImageWriter.writeToPath(bitMatrix, format, file);
} catch (Exception e) {
e.printStackTrace();
}
}
}