public static void main(String[] args) {
// String[] text = {"测试设备编号;路灯;花源成美路辅路;江苏测试管理有限公司;028-60822000;105.872615383,10.466578703"};
//
// String imgpath = "D:\\IdeaProjects\\Ticket-Manage-Service\\ticket-manage-service\\QrCode";
//
// for (int i = 0; i < text.length; i++) {
//
// String[] s = text[i].split(";");
//
// String[] lines = {"设备编号:" + s[0], "设备类型:" + s[1], "道路名称:" + s[2], "管护单位:" + s[3], "报修电话:" + s[4]};
// String[] line = {s[0], s[1], s[2], s[3], s[4]};
//
// createq(lines, line, imgpath + s[0] + ".jpeg");
// }
File path = new File(System.getProperty("user.dir"));
//2024-09-19-8排-2号桌-6号位.png
File qrCodeFile = new File(path.getAbsolutePath(), "QrCode");
String filePath = qrCodeFile.getAbsolutePath().concat("/")
.concat("test")
.concat(".png");
QrCodeUtil.generateQrCodeWithText("123456", filePath, "2024-09-19", "8排-2号桌-6号位");
}
static void createq(String[] lines, String[] line, String imgpath) {
String qrCodeText = line[0];
//二维码大小
int size = 400;
//画布大小
int combinedWidth = size + 800;
int combinedHeight = size + 400;
//标题高度
int topsize = 160;
//设置标题y坐标
int yTitle = 110;
//设置画布内边距
int padding = 5;
//设置标题内容
String title = "xx市镇";
//设置图片dpi
int dpi = 300;
Color blue = new Color(75, 125, 178);
//设置二维码参数
Map hints = new HashMap();
//设置UTF-8, 防止中文乱码
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
// 设置二维码的容错性
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
//留白 默认4
hints.put(EncodeHintType.MARGIN, 1);
// 码版本,取值为 1 到 40 ,根据二维码内容决定最低版本
hints.put(EncodeHintType.QR_VERSION, 3);
//创建码位图
BitMatrix bitMatrix = null;
try {
bitMatrix = new QRCodeWriter().encode(qrCodeText, BarcodeFormat.QR_CODE, size, size, hints);
} catch (WriterException e) {
e.printStackTrace();
}
//将生成码位图转换成BufferedImage
BufferedImage qrImage = MatrixToImageWriter.toBufferedImage(bitMatrix);
//创建空白BufferedImage
BufferedImage combined = new BufferedImage(combinedWidth, combinedHeight, BufferedImage.TYPE_INT_RGB);
//从BufferedImage对象中获取画布(Graphics2D)对象
Graphics2D graphics = combined.createGraphics();
//设置渲染提示为启用文本抗锯齿
graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
//默认开启绘图抗锯齿
//graphics.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL,RenderingHints.VALUE_STROKE_DEFAULT);
//设置空白画布整体背景颜色
GraphicsUtils.drawBackground(graphics, 0, 0, combinedWidth, combinedHeight, Color.WHITE);
//绘制表格画线
//绘制线在内边距内,不影响其它内容,-1操作
int linepadding = padding - 1;
//绘制矩形
GraphicsUtils.drawRectangle(graphics, linepadding, linepadding, combinedWidth - linepadding * 2, combinedHeight - linepadding * 2, Color.BLACK);
//绘制内三横线
GraphicsUtils.drawLine(graphics, linepadding, padding + topsize + 1, combinedWidth - linepadding, padding + topsize + 1, Color.BLACK);
GraphicsUtils.drawLine(graphics, linepadding, padding + topsize + size + 2, combinedWidth - linepadding, padding + topsize + size + 2, Color.BLACK);
GraphicsUtils.drawLine(graphics, linepadding, combinedHeight - (combinedHeight - padding * 2 - topsize - size + 2) / 2 - padding, combinedWidth - linepadding, combinedHeight - (combinedHeight - padding * 2 - topsize - size + 2) / 2 - padding, Color.BLACK);
//绘制二维码竖线及右边2横线
GraphicsUtils.drawLine(graphics, padding + size + 1, padding + topsize + 1, padding + size + 1, padding + topsize + size + 1, Color.BLACK);
GraphicsUtils.drawLine(graphics, padding + size + 1, padding + topsize + 1 + size / 3, combinedWidth - linepadding, padding + topsize + 1 + size / 3, Color.BLACK);
GraphicsUtils.drawLine(graphics, padding + size + 1, padding + topsize + 1 + size / 3 * 2, combinedWidth - linepadding, padding + topsize + 1 + size / 3 * 2, Color.BLACK);
//该方法用于绘制头部绘制矩形并填充指定颜色
GraphicsUtils.drawFillRect(graphics, padding, padding, combinedWidth - (padding * 2), topsize, blue);
//二维码插入画布
GraphicsUtils.drawImage(graphics, qrImage, padding, topsize + padding + 2);
//该方法用于绘制头部绘制矩形并填充指定颜色
GraphicsUtils.drawBackground(graphics, padding, padding, combinedWidth - (padding * 2), topsize, blue);
//绘制头部字体
Font titleFont = new Font("宋体", Font.BOLD, 70);
GraphicsUtils.drawFont(graphics, titleFont, Color.white);
FontMetrics fontMetrics = graphics.getFontMetrics();
int fontWidth = fontMetrics.stringWidth(title);
int xTitle = ((combinedWidth - fontWidth) / 2);
GraphicsUtils.drawText(graphics, title, xTitle, yTitle + padding, titleFont, Color.white);
//绘制非头部字体
//设置字体及颜色
Font downFont = new Font("宋体", Font.BOLD, 50);
GraphicsUtils.drawFont(graphics, downFont, Color.BLACK);
fontMetrics = graphics.getFontMetrics();
int fontHeight = fontMetrics.getHeight();
for (int i = 0; i < lines.length; i++) {
if (i < 3) {
graphics.drawString(lines[i], size + padding + 5, topsize + padding + (size / 3) * i + (size / 3 + fontHeight) / 2);
} else if (i == 3) {
int test = combinedHeight - (combinedHeight - padding * 2 - topsize - size) / 4 * 3 - padding + fontHeight / 2;
graphics.drawString(lines[i], 5 + padding, test);
} else if (i == 4) {
int test = combinedHeight - (combinedHeight - padding * 2 - topsize - size) / 4 - padding + fontHeight / 2;
graphics.drawString(lines[i], 5 + padding, test);
}
}
graphics.dispose();
try {
ImageIO.write(combined, "jpeg", new File(imgpath));
} catch (IOException e) {
e.printStackTrace();
}
}
}
class GraphicsUtils {
// 创建一个空白的 BufferedImage 对象
public static BufferedImage createEmptyImage(int width, int height) {
return new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
}
// 在图像上设置背景颜色
public static void drawBackground(Graphics2D g2d, int x, int y, int width, int height, Color color) {
g2d.setBackground(color);
g2d.clearRect(x, y, width, height);
}
// 在图像上绘制文本
public static void drawText(Graphics2D g2d, String text, int x, int y, Font font, Color color) {
g2d.setColor(color);
g2d.setFont(font);
g2d.drawString(text, x, y);
}
// 在图像上绘制矩形
public static void drawRectangle(Graphics2D g2d, int x, int y, int width, int height, Color color) {
g2d.setColor(color);
g2d.drawRect(x, y, width, height);
}
// 在图像上绘制椭圆
public static void drawEllipse(Graphics2D g2d, int x, int y, int width, int height, Color color) {
g2d.setColor(color);
g2d.drawOval(x, y, width, height);
}
// 在图像上绘制线段
public static void drawLine(Graphics2D g2d, int x1, int y1, int x2, int y2, Color color) {
g2d.setColor(color);
g2d.drawLine(x1, y1, x2, y2);
}
// 在图像上绘制图片
public static void drawImage(Graphics2D g2d, BufferedImage imageToDraw, int x, int y) {
g2d.drawImage(imageToDraw, x, y, null);
}
// 在图像指定区域绘制颜色
public static void drawFillRect(Graphics2D g2d, int x, int y, int width, int height, Color color) {
g2d.setColor(color);
g2d.fillRect(x, y, width, height);
}
// 在图像指定区域绘制颜色
public static void drawFont(Graphics2D g2d, Font font, Color color) {
g2d.setFont(font);
g2d.setColor(color);
}
}
标签:int,画图,padding,Color,g2d,graphics,size
From: https://www.cnblogs.com/Arborblog/p/18374061