一.导包
下载地址:
https://mvnrepository.com/artifact/com.google.zxing/core
二.源代码
public class QRcodeConstructor {
public static void main(String[] args) throws WriterException, IOException {
//用java绘制一个拜年二维码
//导包
MultiFormatWriter formatWriter = new MultiFormatWriter();
//五个参数 内容 类型 宽度 高度 其他信息
String content ="写上你的内容!";
BarcodeFormat type = BarcodeFormat.QR_CODE;
int width = 600;
int height=600;
Map map=new HashMap<>();
map.put(EncodeHintType.CHARACTER_SET,"UTF-8");
map.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
map.put(EncodeHintType.MARGIN,2);
//BitMatrix(虚拟二维码对象)
BitMatrix matrix = formatWriter.encode(content, type, width, height, map);
int black= Color.black.getRGB();
int white= Color.white.getRGB();
BufferedImage image=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
for (int x = 0; x <width; x++) {
for (int y = 0; y <height; y++) {
image.setRGB(x,y,matrix.get(x,y)?black:white);
}
}
File file=new File("C://Users//86177//Desktop//test//qrcode.jpg");
ImageIO.write(image,"jpg",file);
}
}