代码如下:
@Test //标签生成
public void test05() throws IOException, WriterException {
int width = Integer.parseInt("430"); // 图片宽
int height = Integer.parseInt("350");// 图片高
String titleStr = "WB_1123654987";//resourceAssets.getCode();//产品编码
String deviceName = "黑科技";//resourceAssets.getName();//产品名
String departName = "软件研发中心";//resourceAssets.getOfficeName();//使用部门
String deviceModel = "MdsdOA-II";//resourceAssets.getModel();//产品型号
String installPlace = "MdedOA-IIMdsOA-II";//resourceAssets.getRemark();//用途说明
String purchaseTime = "2021-08-06";//DateUtils.formatDate(resourceAssets.getBuyTime());//采购日期
String supplier = "安徽***有限公司";//companyService.getInfoById(resourceAssets.getMaintainer());
String deviceId= "D:\\mksdoffices";//二维码内容
String url = deviceId+"//assets//";
File f1 = new File(url);
if (!f1.exists())f1.mkdirs();
File f2 = new File((url+titleStr + ".jpg"));
if (f2.exists()) f2.delete();
String fileName = url + titleStr + ".jpg"; //资产标签存放路径
//先创建一个图片编辑器,生成一个二维码,二维码生成图片后再创建另一个图片编辑器,用来编写表格
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 = 100;
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);
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.drawLine(0, 50, 430, 50); // 从上到下第二个横线(设备编码下面横线)
g2.setStroke(new BasicStroke(0.0f)); // 边框不需要加粗
g2.drawLine(0, 100, 430, 100); // 从上到下第三个横线(设备名称下面横线)
g2.drawLine(0, 150, 430, 150); // 从上到下第四个横线(使用部门下面横线)
g2.drawLine(0, 200, 430, 200); // 从上到下第5个横线(安装位置下面横线)
g2.drawLine(0, 300, 430, 300); // 从上到下第6个横线(安装位置下面横线)
g2.drawLine(200, 100, 200, 300); // 从左到右第二个竖线
// 设置标题的字体,字号,大小
Font titleFont = new Font("宋体", Font.BOLD, 14);
g2.setFont(titleFont);
String markNameStr = titleStr;
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;// 感觉不居中,向左移动35个单位
g2.drawString("设备编码:"+markNameStr, titleWidthX, 30);
// 设备名称
g2.setFont(new Font("宋体", Font.BOLD, 14));
g2.drawString("设备名称:"+(deviceName==null?"":deviceName), titleWidthX, 80);
// 使用部门
g2.drawString("使用部门:"+(departName==null?"":departName), 5, 130);
// 规格型号
g2.drawString("规格型号:"+(deviceModel==null?"":deviceModel), 205, 130);
// 安装位置
g2.drawString("用途说明:"+(installPlace==null?"":installPlace), 5, 180);
// 购置日期
g2.drawString("购置日期:"+(purchaseTime==null?"":purchaseTime), 205, 180);
// 供应商
g2.drawString("维保商:"+(supplier==null?"":supplier), 5, 230);
// 画文字到新的面板
g2.drawImage(image, 270, 210,image.getWidth(), image.getHeight(), null);
// 安装位置
g2.drawString("用途说明:"+(installPlace==null?"":installPlace), 5, 330);
g2.dispose(); // 释放对象
ImageIO.write(bi, "JPEG", new FileOutputStream(fileName));// 保存图片 JPEG表示保存格式
}