java生成标签图片
参考网址:
https://blog.csdn.net/Maomaoqifeng/article/details/107860894/
代码仓库地址
https://gitee.com/shao_ming314/study_demo/tree/master/java%E7%94%9F%E6%88%90%E6%A0%87%E7%AD%BE%E5%9B%BE%E7%89%87/ImageAndQrcode
引入依赖
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.3.3</version>
</dependency>
测试
package com.shaoming;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Hashtable;
public class ImageAndQrCodeTest {
public static void main(String[] args) throws IOException, WriterException {
test();
}
public static void test() throws FileNotFoundException, IOException, WriterException {
/**
* 图片的宽高
*/
int width = 430; // 图片宽
int height = 250;// 图片高
// 医院
String hospitalName = "xxxx医院";
//
String assetName = "xxx";
// 设备型号
String assetModelNum = "xxxx";
// 出厂编号
String assetFactoryNum = "xxxxx";
// 设备编号
String assetCode = "xxxx-xxx";
// 使用科室
String useDept = "测试科室";
/**
* 扫描二维码的信息
*/
// 爱德亚设备编号
String deviceId="ixxxx-xxxx";//生成二维码的产品id
String fileName ="D:/test/mytest.png";//二维码存放路径
//先创建一个图片编辑器,生成一个二维码,二维码生成图片后再创建另一个图片编辑器,用来编写表格
Hashtable<EncodeHintType, ErrorCorrectionLevel> hintMap = new Hashtable<EncodeHintType, ErrorCorrectionLevel>();
hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L); // 矫错级别
QRCodeWriter qrCodeWriter = new QRCodeWriter();
//创建比特矩阵(位矩阵)的QR码编码的字符串
deviceId=new String(deviceId.getBytes("UTF-8"),"ISO-8859-1");
int qrCodeSize = 126;
BitMatrix byteMatrix = qrCodeWriter.encode(deviceId, BarcodeFormat.QR_CODE, qrCodeSize, qrCodeSize, hintMap);
// 使BufferedImage勾画QRCode (matrixWidth 是行二维码像素点)
int matrixWidth = byteMatrix.getWidth();
// BufferedImage image = new BufferedImage(matrixWidth-20, matrixWidth-20, BufferedImage.TYPE_INT_RGB);
BufferedImage image = new BufferedImage(matrixWidth-20, matrixWidth-20, BufferedImage.TYPE_INT_RGB);
image.createGraphics();
Graphics2D graphics = (Graphics2D) image.getGraphics();
graphics.setColor(Color.WHITE);
graphics.fillRect(0, 0, matrixWidth, matrixWidth);
// 使用比特矩阵画并保存图像
graphics.setColor(Color.BLACK);
for (int i = 0; i < matrixWidth; i++){
for (int j = 0; j < matrixWidth; j++){
if (byteMatrix.get(i, j)){
graphics.fillRect(i-10, j-10,1, 1);
}
}
}
image.flush();
// 得到图片缓冲区
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);// INT精确度达到一定,RGB三原色,高度70,宽度150
bi.createGraphics();
// 得到它的绘制环境(这张图片的笔)
Graphics2D g2 = (Graphics2D) bi.getGraphics();
g2.setColor(Color.WHITE); // 设置背景颜色
g2.fillRect(0, 0, width, height);// 填充整张图片(其实就是设置背景颜色)
g2.setColor(Color.black);// 设置字体颜色
g2.setStroke(new BasicStroke(2.0f)); // 边框加粗
g2.drawRect(1, 1, width - 2, height - 2); // 画边框就是黑边框
g2.setStroke(new BasicStroke(2.0f)); // 边框不需要加粗
g2.drawLine(0, 100, 430, 100); // 从上到下第2个横线(设备名称下面横线)
g2.drawLine(0, 130, 430, 130); // 从上到下第3个横线(使用部门下面横线)
g2.drawLine(0, 160, 320, 160); // 从上到下第4个横线(安装位置下面横线)
g2.drawLine(0, 190, 320, 190); // 从上到下第5个横线(安装位置下面横线)
g2.drawLine(0, 220, 320, 220); // 从上到下第6个横线(安装位置下面横线)
g2.drawLine(90, 100, 90, 300); // 从左到右第二个竖线
g2.drawLine(320, 130, 320, 300); // 从左到右第三个竖线
// 设置标题的字体,字号,大小
Font titleFont = new Font("黑体", Font.BOLD, 30);
g2.setFont(titleFont);
String markNameStr = hospitalName;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); // 抗锯齿
// 计算文字长度,计算居中的X点坐标
FontMetrics fm = g2.getFontMetrics(titleFont);
int titleWidth = fm.stringWidth(markNameStr);
int titleWidthX = (width - titleWidth) / 2 ;// 感觉不居中,向左移动35个单位
g2.drawString("xxxx医院", titleWidthX, 40);
g2.setFont(new Font("黑体", Font.BOLD, 11));
g2.drawString("XXXX COUNTY PEOPLE'S HOSPITAL", titleWidthX, 55);
g2.setFont(new Font("宋体", Font.BOLD, 16));
g2.drawString("设备名称:", 5, 120);
g2.drawString(assetName, 95, 120);
g2.drawString("设备型号:", 5, 150);
g2.drawString(assetModelNum, 95, 150);
g2.drawString("出厂编号:", 5, 180);
g2.drawString(assetFactoryNum, 95, 180);
g2.drawString("设备编号:", 5, 210);
g2.drawString(assetCode, 95, 210);
g2.drawString("使用科室:", 5, 240);
g2.drawString(useDept, 95, 240);
g2.drawImage(image, 322, 134,image.getWidth(), image.getHeight(), null);
g2.dispose(); // 释放对象
ImageIO.write(bi, "JPEG", new FileOutputStream(fileName));// 保存图片 JPEG表示保存格式
}
}