代码如下:
package com.ckf.code.code;
import com.alibaba.fastjson.JSONObject;
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 org.junit.Test;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
/**
* @author 安详的苦丁茶
* @date 2020/7/30 22:32
*/
public class Qrcode {
@Test
public void generateQrcode() throws WriterException, IOException {
//生成二维码
JSONObject jsonObject = new JSONObject();
//给jsonObject对象中存数据
jsonObject.put("company", );
jsonObject.put("Name", "ccc");
jsonObject.put("author", "ckf");
jsonObject.put("address", "GuangDongZhanJiang");
String content = jsonObject.toString();
//定义图片的大小
int width = 300;
int height = 300;
//创建一个map集合
Map<EncodeHintType, Object> hint = new HashMap<EncodeHintType, Object>();
//定义二维码存储路径以及命名
String filePath="F://";
String fileName="QRCode.jpg";
//创建一个矩阵对象
BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hint);
//创建一个路径对象
Path path= FileSystems.getDefault().getPath(filePath,fileName);
//将矩阵对象生成一个图片(二维码)
MatrixToImageWriter.writeToPath(bitMatrix,"jpg",path);
System.out.println("生成二维码");
}
}