当前位置: 首页 > news >正文

springboot集成thymeleaf实战

引言

笔者最近接到一个打印标签的需求,由于之前没有做过类似的功能,所以这也是一次学习探索的机会了,打印的效果图如下:
在这里插入图片描述
这个最终的打印是放在58mm*58mm的小标签纸上,条形码就是下面的35165165qweqweqe序列号生成的,也是图片形式。序列号应该放在条形码的正下方居中位置的,但是由于笔者前端技术有点拉跨,碰到样式啥的就头疼,这也是尽力后的效果了。下面看集成过程吧。

一、引入pom相关依赖包

笔者的环境是JDK17,pom相关版本如下,具体用什么版本不固定,不报错就行。

 <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency><dependency><groupId>com.google.zxing</groupId><artifactId>core</artifactId><version>3.4.1</version></dependency><dependency><groupId>com.google.zxing</groupId><artifactId>javase</artifactId><version>3.4.1</version></dependency><dependency><groupId>ognl</groupId><artifactId>ognl</artifactId><version>3.4.3</version></dependency><!-- Flying Saucer --><dependency><groupId>org.xhtmlrenderer</groupId><artifactId>flying-saucer-pdf</artifactId><version>9.1.20</version></dependency><!--itext--><dependency><groupId>com.lowagie</groupId><artifactId>itext</artifactId><version>2.1.7</version></dependency><dependency><groupId>org.apache.commons</groupId><artifactId>commons-lang3</artifactId><version>3.12.0</version></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><scope>provided</scope></dependency>

二、条形码工具类

package com.hulei.thymeleafproject;import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.oned.Code128Writer;
import org.apache.commons.lang3.StringUtils;import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;/*** @author hulei* @Date 2024/7/26 14:15* @Description: 条形码工具类**/
public class BarCodeUtils {/*** 默认图片宽度*/private static final int DEFAULT_PICTURE_WIDTH = 400;/*** 默认图片高度*/private static final int DEFAULT_PICTURE_HEIGHT = 200;/*** 默认条形码宽度*/private static final int DEFAULT_BAR_CODE_WIDTH = 300;/*** 默认条形码高度*/private static final int DEFAULT_BAR_CODE_HEIGHT = 30;/*** 默认字体大小*/private static final int DEFAULT_FONT_SIZE = 15;/*** 图片格式*/private static final String FORMAT = "png";/*** 字符集*/private static final String CHARSET = "utf-8";/*** 设置 条形码参数*/private static final Map<EncodeHintType, Object> hints = new HashMap<>();static {hints.put(EncodeHintType.CHARACTER_SET, "utf-8");}/*** 获取条形码图片** @param codeValue 条形码内容* @return 条形码图片*/public static BufferedImage getBarCodeImage(String codeValue) {return getBarCodeImage(codeValue, DEFAULT_BAR_CODE_WIDTH, DEFAULT_BAR_CODE_HEIGHT);}/*** 获取条形码图片** @param codeValue 条形码内容* @param width     宽度* @param height    高度* @return 条形码图片*/public static BufferedImage getBarCodeImage(String codeValue, int width, int height) {// CODE_128是最常用的条形码格式return getBarCodeImage(codeValue, width, height, BarcodeFormat.CODE_128);}/*** 获取条形码图片** @param codeValue     条形码内容* @param width         宽度* @param height        高度* @param barcodeFormat 条形码编码格式* @return 条形码图片*/public static BufferedImage getBarCodeImage(String codeValue, int width, int height, BarcodeFormat barcodeFormat) {Code128Writer writer = switch (barcodeFormat) {case CODE_128 ->// 最常见的条形码,但是不支持中文new Code128Writer();case PDF_417 ->// 支持中文的条形码格式new Code128Writer();// 如果使用到其他格式,可以在这里添加default -> new Code128Writer();};// 编码内容, 编码类型, 宽度, 高度, 设置参数BitMatrix bitMatrix;bitMatrix = writer.encode(codeValue, barcodeFormat, width, height, hints);return MatrixToImageWriter.toBufferedImage(bitMatrix);}/*** 获取条形码** @param codeValue 条形码内容* @param bottomStr 底部文字*/public static BufferedImage getBarCodeWithWords(String codeValue, String bottomStr) {return getBarCodeWithWords(codeValue, bottomStr, "", "", "");}/*** 获取条形码* @param codeValue   条形码内容* @param bottomStr   底部文字* @param topLeftStr  左上角文字* @param topRightStr 右上角文字*/public static BufferedImage getBarCodeWithWords(String codeValue,String bottomStr,String bottomStr2,String topLeftStr,String topRightStr) {return getCodeWithWords(getBarCodeImage(codeValue),bottomStr,bottomStr2,topLeftStr,topRightStr,DEFAULT_PICTURE_WIDTH,DEFAULT_PICTURE_HEIGHT,0,-20,0,0,0,0,DEFAULT_FONT_SIZE);}/*** 获取条形码** @param codeImage       条形码图片* @param firstBottomStr  底部文字首行* @param secondBottomStr 底部文字次行* @param topLeftStr      左上角文字* @param topRightStr     右上角文字* @param pictureWidth    图片宽度* @param pictureHeight   图片高度* @param codeOffsetX     条形码宽度* @param codeOffsetY     条形码高度* @param topLeftOffsetX  左上角文字X轴偏移量* @param topLeftOffsetY  左上角文字Y轴偏移量* @param topRightOffsetX 右上角文字X轴偏移量* @param topRightOffsetY 右上角文字Y轴偏移量* @param fontSize        字体大小* @return 条形码图片*/public static BufferedImage getCodeWithWords(BufferedImage codeImage,String firstBottomStr,String secondBottomStr,String topLeftStr,String topRightStr,int pictureWidth,int pictureHeight,int codeOffsetX,int codeOffsetY,int topLeftOffsetX,int topLeftOffsetY,int topRightOffsetX,int topRightOffsetY,int fontSize) {BufferedImage picImage = new BufferedImage(pictureWidth, pictureHeight, BufferedImage.TYPE_INT_RGB);Graphics2D g2d = picImage.createGraphics();// 抗锯齿setGraphics2D(g2d);// 设置白色setColorWhite(g2d, picImage.getWidth(), picImage.getHeight());// 条形码默认居中显示int codeStartX = (pictureWidth - codeImage.getWidth()) / 2 + codeOffsetX;int codeStartY = (pictureHeight - codeImage.getHeight()) / 2 + codeOffsetY;// 画条形码到新的面板g2d.drawImage(codeImage, codeStartX, codeStartY, codeImage.getWidth(), codeImage.getHeight(), null);// 画文字到新的面板g2d.setColor(Color.BLACK);// 字体、字型、字号g2d.setFont(new Font("微软雅黑", Font.PLAIN, fontSize));// 文字与条形码之间的间隔int wordAndCodeSpacing1 = 0;if (StringUtils.isNotEmpty(firstBottomStr)) {// 文字长度int strWidth = g2d.getFontMetrics().stringWidth(firstBottomStr);// 文字X轴开始坐标,这里是居中int strStartX = codeStartX + (codeImage.getWidth() - strWidth) / 2;// 文字Y轴开始坐标int strStartY = codeStartY + codeImage.getHeight() + fontSize + wordAndCodeSpacing1;// 画文字g2d.drawString(firstBottomStr, strStartX, strStartY);}// 文字与条形码之间的间隔int wordAndCodeSpacing2 = 30;if (StringUtils.isNotEmpty(secondBottomStr)) {// 文字长度int strWidth = g2d.getFontMetrics().stringWidth(secondBottomStr);// 文字X轴开始坐标,这里是居中int strStartX = codeStartX + (codeImage.getWidth() - strWidth) / 2;// 文字Y轴开始坐标int strStartY = codeStartY + codeImage.getHeight() + fontSize + wordAndCodeSpacing2;// 画文字g2d.drawString(secondBottomStr, strStartX, strStartY);}if (StringUtils.isNotEmpty(topLeftStr)) {// 文字长度int strWidth = g2d.getFontMetrics().stringWidth(topLeftStr);// 文字X轴开始坐标int strStartX = codeStartX + topLeftOffsetX;// 文字Y轴开始坐标int strStartY = codeStartY + topLeftOffsetY - wordAndCodeSpacing1;// 画文字g2d.drawString(topLeftStr, strStartX, strStartY);}if (StringUtils.isNotEmpty(topRightStr)) {// 文字长度int strWidth = g2d.getFontMetrics().stringWidth(topRightStr);// 文字X轴开始坐标,这里是居中int strStartX = codeStartX + codeImage.getWidth() - strWidth + topRightOffsetX;// 文字Y轴开始坐标int strStartY = codeStartY + topRightOffsetY - wordAndCodeSpacing1;// 画文字g2d.drawString(topRightStr, strStartX, strStartY);}g2d.dispose();picImage.flush();return picImage;}/*** 设置 Graphics2D 属性  (抗锯齿)** @param g2d Graphics2D提供对几何形状、坐标转换、颜色管理和文本布局更为复杂的控制*/private static void setGraphics2D(Graphics2D g2d) {g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_DEFAULT);Stroke s = new BasicStroke(1, BasicStroke.CAP_ROUND, BasicStroke.JOIN_MITER);g2d.setStroke(s);}/*** 设置背景为白色** @param g2d Graphics2D提供对几何形状、坐标转换、颜色管理和文本布局更为复杂的控制*/private static void setColorWhite(Graphics2D g2d, int width, int height) {g2d.setColor(Color.WHITE);//填充整个屏幕g2d.fillRect(0, 0, width, height);//设置笔刷g2d.setColor(Color.BLACK);}/*** 将 BufferedImage 转为 base64*/public static String bufferedImage2Base64(BufferedImage image) throws IOException {// 输出流ByteArrayOutputStream stream = new ByteArrayOutputStream();ImageIO.write(image, FORMAT, stream);java.util.Base64.Encoder encoder = java.util.Base64.getEncoder();String imgBase64 = new String(encoder.encode(stream.toByteArray()), CHARSET);imgBase64 = "data:image/" + FORMAT + ";base64," + imgBase64;return imgBase64;}}

这个工具类中,默认生成的条形码图片格式是png,当然可以自己修改格式。

三、thymeleaf画模板

这个就是打印模板了,thymeleaf和freemarker一样都是模板引擎,freemarker模板语法更简单些。如果需要简单的变量替换和循环,FreeMarker可能是更好的选择。如果需要更丰富的模板功能和动态内容处理,Thymeleaf可能更适合。笔者这里选择的是thymeleaf。

<!DOCTYPE html>
<html lang="zh-CN">
<head><title>维修库商品打印标签模板</title><meta charset="UTF-8"></meta><style>        body, html {margin: 0;padding: 0;width: 70mm;height: 70mm;font-family: 'SimSun', sans-serif; /* 防止生成的PDF中文不显示 */}h1 {text-align: center;font-size: 12px;line-height: 1.5;}p {font-size: 12px;margin: 3px 0;}.device-code {display: flex; /* 使用Flexbox布局 */align-items: center; /* 垂直居中对齐 */}.sn-container {display: inline-flex; /* 内联Flexbox容器 */align-items: center; /* 垂直居中对齐 */margin-left: 2px; /* 与“设备码:”之间的间距 */}.sn-image {width: auto; /* 图片宽度自适应 */}.sn-text {margin-top: 5px; /* 文本与图片之间的间距 */text-align: center; /* 文字居中 */}img {vertical-align: middle;display: inline-block;}</style>
</head>
<body>
<div><h1><img th:src="${zlbcImage}" alt="Image" style="height:30px;"></img>智链泊车</h1><p th:text="${createTime != null ? '入库日期:'+ createTime : '入库日期:未知'}"></p><p th:text="${materialName != null ? '名&nbsp;&nbsp;&nbsp;&nbsp;称:'+ materialName : '名称:未知'}"></p><p th:text="${supplierName != null ? '客&nbsp;&nbsp;&nbsp;&nbsp;户:'+ supplierName : '客户:未知'}"></p><p class="device-code">&nbsp;&nbsp;码:<span class="sn-container"><img class="sn-image" th:src="${sequencesNumberImage}" alt="Image"/><div class="sn-text" th:text="${sequencesNumber}">${sequencesNumber}</div></span></p>
</div>
</body>
</html>

这个模板里面的变量赋值时比较简单的,主要是有两个图片的变量zlbcImagesequencesNumberImage,一个是智慧停车前面的原型小图标,一个就是条形码是,在赋值时是需要把图片读成BufferedImage,再把BufferedImage使用base64编码一下。

另外一个重要的点是:font-family: ‘SimSun’, sans-serif;
这个属性必须加上,否则后面把html转成PDF时,中文会不显示。

四、字体准备simsun.ttc

这个字体是因为,我要把html转成一个PDF,中间转换需要一些字体,并且支持中文,网上搜索了下,选择了simsun.ttc这个字体,同时我在html上也指定了这个字体。网上下载这个字体资源库后,放在如下位置,以便程序中加载使用。
在这里插入图片描述

五、测试代码

package com.hulei.thymeleafproject;import com.lowagie.text.pdf.BaseFont;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import org.xhtmlrenderer.pdf.ITextFontResolver;
import org.xhtmlrenderer.pdf.ITextRenderer;import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.List;/*** @author hulei* @date 2024/7/27 9:26*/@RestController
public class TestController {@Resourceprivate TemplateEngine templateEngineBySelf;@PostMapping("/printSNLabel")public void test(@RequestBody List<PrintSNLabelReqDTO> list) {list.forEach(loop -> {Map<String, Object> map = new HashMap<>();map.put("createTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));map.put("materialName", loop.getMaterialName());map.put("supplierName", loop.getSupplierName());//设备码图片二进制字节流BufferedImage sequencesNumberImage = BarCodeUtils.getBarCodeImage(loop.getSequencesNumber(), 100, 50);this.storeImage(sequencesNumberImage, "E:/111.png");try {String base64Image = BarCodeUtils.bufferedImage2Base64(sequencesNumberImage);System.out.println("base64Image: " + base64Image);map.put("sequencesNumberImage", base64Image);} catch (IOException e) {throw new RuntimeException(e);}map.put("sequencesNumber", loop.getSequencesNumber());try {ClassLoader classLoader = Thread.currentThread().getContextClassLoader();String symbolImagePath = "images/zlbcImage.png";InputStream inputStream = classLoader.getResourceAsStream(symbolImagePath);assert inputStream != null;BufferedImage zlbcImageBufferedImage = ImageIO.read(inputStream);this.storeImage(zlbcImageBufferedImage, "E:/222.png");String zlbcImage = BarCodeUtils.bufferedImage2Base64(zlbcImageBufferedImage);System.out.println("zlbcImage: " + zlbcImage);map.put("zlbcImage", zlbcImage);} catch (IOException e) {throw new RuntimeException(e);}try {generateSNPicture(map);} catch (IOException e) {throw new RuntimeException(e);}});}private void generateSNPicture(Map<String,Object> map) throws IOException {// 填充模板数据Context context = new Context();context.setVariable("createTime", map.get("createTime"));context.setVariable("materialName", map.get("materialName"));context.setVariable("supplierName", map.get("supplierName"));context.setVariable("sequencesNumberImage", map.get("sequencesNumberImage"));context.setVariable("sequencesNumber", map.get("sequencesNumber"));context.setVariable("zlbcImage", map.get("zlbcImage"));String htmlContent = templateEngineBySelf.process("printTemplate", context);System.out.println(htmlContent);htmlToPdf(htmlContent);}private void htmlToPdf(String htmlContent){try {//创建PDf文件ITextRenderer renderer = new ITextRenderer();//获取使用的字体数据(由于对中文字体显示可能会不支持,所以需要主动添加字体数据设置。)ITextFontResolver fontResolver = renderer.getFontResolver();fontResolver.addFont("templates/fonts/simsun.ttc",BaseFont.IDENTITY_H, BaseFont.EMBEDDED);//设置文件名称String sDate = new SimpleDateFormat("yyyyMMdd").format(new Date());String sTime = new SimpleDateFormat("HHmmssSSS").format(new Date());// 生成临时文件Path tempPdfPath = Files.createTempFile("temp_pdf_"+sDate+sTime, ".pdf");String pdfFilePath = tempPdfPath.toAbsolutePath().toString();// 将html生成文档renderer.setDocumentFromString(htmlContent);renderer.layout();OutputStream os = new FileOutputStream(pdfFilePath);// 将文档写入到输出流中renderer.createPDF(os);// 关闭流os.close();//把临时生成的文件转移到E盘,这里可以根据个人需求选在把临时文件上传到文件服务器System.out.println("pdfFilePath: "+pdfFilePath);File tempPdfFile = tempPdfPath.toFile();System.out.println("tempPdfFileName: "+tempPdfFile.getName());// 复制文件到E盘try {Path targetPath = Paths.get("E:", tempPdfFile.getName()); // 目标路径Files.copy(tempPdfPath, targetPath);System.out.println("文件已复制到 E 盘");} catch (Exception e) {System.err.println("复制文件时发生错误: " + e.getMessage());}//删除临时生成的本地PDF文件Files.delete(tempPdfPath);} catch (Exception e) {System.out.println("生成pdf文件失败");throw new RuntimeException(e);}}private void storeImage(BufferedImage image, String filePath){try {// 指定输出文件路径和格式File outputFile = new File(filePath);// 使用 ImageIO.write 方法将图片写入磁盘boolean isWritten = ImageIO.write(image, "png", outputFile);if (isWritten) {System.out.println("图片已成功保存到磁盘.");} else {System.out.println("图片保存失败.");}} catch (IOException e) {System.err.println("保存图片时发生错误: " + e.getMessage());}}}

这里为了展示代码,没有分层了,全都放在了controller层。主要分为三块:加载html模板,变量赋值,html转pdf

转成pdf后的效果如下:

在这里插入图片描述

Apifox测试工具,测试数据如下,注意json是数组形式,因为后端controller接收的是List
在这里插入图片描述

整个代码我已上传到gitee:gitee仓库地址

相关文章:

springboot集成thymeleaf实战

引言 笔者最近接到一个打印标签的需求&#xff0c;由于之前没有做过类似的功能&#xff0c;所以这也是一次学习探索的机会了&#xff0c;打印的效果图如下&#xff1a; 这个最终的打印是放在58mm*58mm的小标签纸上&#xff0c;条形码就是下面的35165165qweqweqe序列号生成的&…...

SpringBoot+Vue+kkFileView实现文档管理(文档上传、下载、在线预览)

场景 SpringBootVueOpenOffice实现文档管理(文档上传、下载、在线预览)&#xff1a; SpringBootVueOpenOffice实现文档管理(文档上传、下载、在线预览)_霸道流氓气质的博客-CSDN博客_vue openoffice 上面在使用OpenOffice实现doc、excel、ppt等文档的管理和预览。 除此之外…...

从代码层面熟悉UniAD,开始学习了解端到端整体架构

0. 简介 最近端到端已经是越来越火了&#xff0c;以UniAD为代表的很多工作不断地在不断刷新端到端的指标&#xff0c;比如最近SparseDrive又重新刷新了所有任务的指标。在端到端火热起来之前&#xff0c;成熟的模块化自动驾驶系统被分解为不同的独立任务&#xff0c;例如感知、…...

微信小程序-选中文本时选中checkbox

1.使用labe嵌套住checkbox标签 <label class"label-box"> <checkbox >匿名提交</checkbox> </label>2.使checkbox和label组件在同一行 .label-box{display: flex;align-items: center; }效果图 此时选中文本匿名提交&#xff0c;checkbox…...

[玄机]流量特征分析-蚁剑流量分析

题目网址【玄机】&#xff1a;https://xj.edisec.net/ AntSword&#xff08;蚁剑&#xff09;是一款开源的网络安全工具&#xff0c;常用于网络渗透测试和攻击。它可以远程连接并控制被攻击计算机&#xff0c;执行命令、上传下载文件等操作。 蚁剑与网站进行数据交互的过程中&a…...

2-51 基于matlab的IFP_FCM(Improved fuzzy partitions-FCM)

基于matlab的IFP_FCM&#xff08;Improved fuzzy partitions-FCM&#xff09;&#xff0c;改进型FCM(模糊C均值)聚类算法,解决了FCM算法对初始值设定较为敏感、训练速度慢、在迭代时容易陷入局部极小的问题。并附带了Box和Jenkins煤气炉数据模型辨识实例。程序已调通&#xff0…...

Java人力资源招聘社会校招类型招聘小程序

✨&#x1f4bc;【职场新风尚&#xff01;解锁人力资源招聘新神器&#xff1a;社会校招类型招聘小程序】✨ &#x1f393;【校招新体验&#xff0c;一键触达梦想企业】&#x1f393; 还在为错过校园宣讲会而懊恼&#xff1f;别怕&#xff0c;社会校招类型招聘小程序来救场&am…...

oracle表、表空间使用空间

文章目录 一、Oracle查询表空间占用情况二、Oracle查询表占用的空间三、Oracle查询表空间使用情况四、Oracle查询每张表占用空间五、表空间大小 TOC 一、Oracle查询表空间占用情况 oracle日常工作中查看表占用空间大小是数据库管理中的基本操作&#xff1a; SELECT a.tablesp…...

IDEA管理远程仓库Git

1、模拟项目 新建一个文件夹&#xff0c;用来这次演示 用IDEA来打开文件夹 2、创建仓库 在IDEA中给该文件夹创建本地仓库和远程仓库 在菜单栏找到VCS选择Share project on Gitee 在弹窗中输入描述信息 接下来会出现以下弹窗 点击ADD后&#xff0c;在gitee上会创建远程仓库 …...

【数据结构】Java实现二叉搜索树

二叉搜索树的基本性质 二叉搜索树&#xff08;Binary Search Tree, BST&#xff09;是一种特殊的二叉树&#xff0c;它具有以下特征&#xff1a; 1. 节点结构&#xff1a;每个节点包含一个键&#xff08;key&#xff09;和值&#xff08;value&#xff09;&#xff0c;以及指…...

钉钉小程序如何通过setdate重置对象

在钉钉小程序中&#xff0c;通过setData方法来重置对象&#xff08;即更新对象中的数据&#xff09;是一个常见的操作。然而&#xff0c;需要注意的是&#xff0c;钉钉小程序&#xff08;或任何小程序平台&#xff09;的setData方法在处理对象更新时有一些特定的规则和最佳实践…...

DjangoRF-10-过滤-django-filter

1、安装pip install django-filter https://pypi.org/ 搜索django-filter基础用法 2、进行配置 3、进行内容调试。 4、如果碰到没有关联的字段。interfaces和projects没有直接关联字段&#xff0c;但是interface和module有关联&#xff0c;而且module和projects关联&#x…...

Android SurfaceFlinger——GraphicBuffer的生成(三十二)

通过前面的学习我们知道,在 SurfaceFlinger 中使用的生产者/消费者模型,Surface 做为生产者一方存在如下两个比较重要的函数: dequeueBuffer:获取一个缓冲区(GraphicBuffer),也就是 GraphicBuffer 生成。queueBuffer :把缓冲区(GraphicBuffer)放入缓冲队列中。 …...

<数据集>棉花识别数据集<目标检测>

数据集格式&#xff1a;VOCYOLO格式 图片数量&#xff1a;13765张 标注数量(xml文件个数)&#xff1a;13765 标注数量(txt文件个数)&#xff1a;13765 标注类别数&#xff1a;4 标注类别名称&#xff1a;[Partially opened, Fully opened boll, Defected boll, Flower] 序…...

[240730] OpenAI 推出基于规则的奖励机制 (RBR) 提升模型安全性 | 英特尔承认其13、14代 CPU 存在问题

目录 OpenAI 推出基于规则的奖励机制&#xff08;RBR&#xff09;提升模型安全性英特尔承认其 13、14代 CPU 存在问题 OpenAI 推出基于规则的奖励机制&#xff08;RBR&#xff09;提升模型安全性 为了解决传统强化学习中依赖人工反馈的低效问题&#xff0c;OpenAI 开发了基于规…...

【JavaScript】展开运算符详解

文章目录 一、展开运算符的基本用法1. 展开数组2. 展开对象 二、展开运算符的实际应用1. 合并数组2. 数组的浅拷贝3. 合并对象4. 对象的浅拷贝5. 更新对象属性 三、展开运算符的高级用法1. 在函数参数中使用2. 嵌套数组的展开3. 深拷贝对象4. 动态属性名 四、注意事项和最佳实践…...

麒麟V10系统统一认证子系统国际化

在适配麒麟V10系统统一认证子系统国际化过程中&#xff0c; 遇到了很多的问题&#xff0c;关键是麒麟官方的文档对这部分也是粗略带过&#xff0c;遇到的问题有: &#xff08;1&#xff09;xgettext无法提取C源文件中目标待翻译的字符串。 &#xff08;2&#xff09;使用msgf…...

C语言进阶 13. 文件

C语言进阶 13. 文件 文章目录 C语言进阶 13. 文件13.1. 格式化输入输出13.2. 文件输入输出13.3. 二进制文件13.4. 按位运算13.5. 移位运算13.6. 位运算例子13.7. 位段 13.1. 格式化输入输出 格式化输入输出: printf %[flags][width][.prec][hlL]type scanf %[flags]type %[fl…...

LinuxCentos中ELK日志分析系统的部署(详细教程8K字)附图片

&#x1f3e1;作者主页&#xff1a;点击&#xff01; &#x1f427;Linux基础知识(初学)&#xff1a;点击&#xff01; &#x1f427;Linux高级管理防护和群集专栏&#xff1a;点击&#xff01; &#x1f510;Linux中firewalld防火墙&#xff1a;点击&#xff01; ⏰️创作…...

Vscode ssh Could not establish connection to

错误表现 上午还能正常用vs code连接服务器看代码&#xff0c;中午吃个饭关闭vscode再重新打开输入密码后就提示 Could not establish connection to xxxx 然后我用终端敲ssh的命令连接&#xff0c;结果是能正常连接。 解决方法 踩坑1 网上直接搜Could not establish con…...

SciencePlots——绘制论文中的图片

文章目录 安装一、风格二、1 资源 安装 # 安装最新版 pip install githttps://github.com/garrettj403/SciencePlots.git# 安装稳定版 pip install SciencePlots一、风格 简单好用的深度学习论文绘图专用工具包–Science Plot 二、 1 资源 论文绘图神器来了&#xff1a;一行…...

CocosCreator 之 JavaScript/TypeScript和Java的相互交互

引擎版本&#xff1a; 3.8.1 语言&#xff1a; JavaScript/TypeScript、C、Java 环境&#xff1a;Window 参考&#xff1a;Java原生反射机制 您好&#xff0c;我是鹤九日&#xff01; 回顾 在上篇文章中&#xff1a;CocosCreator Android项目接入UnityAds 广告SDK。 我们简单讲…...

04-初识css

一、css样式引入 1.1.内部样式 <div style"width: 100px;"></div>1.2.外部样式 1.2.1.外部样式1 <style>.aa {width: 100px;} </style> <div class"aa"></div>1.2.2.外部样式2 <!-- rel内表面引入的是style样…...

相机Camera日志分析之三十一:高通Camx HAL十种流程基础分析关键字汇总(后续持续更新中)

【关注我,后续持续新增专题博文,谢谢!!!】 上一篇我们讲了:有对最普通的场景进行各个日志注释讲解,但相机场景太多,日志差异也巨大。后面将展示各种场景下的日志。 通过notepad++打开场景下的日志,通过下列分类关键字搜索,即可清晰的分析不同场景的相机运行流程差异…...

【C语言练习】080. 使用C语言实现简单的数据库操作

080. 使用C语言实现简单的数据库操作 080. 使用C语言实现简单的数据库操作使用原生APIODBC接口第三方库ORM框架文件模拟1. 安装SQLite2. 示例代码:使用SQLite创建数据库、表和插入数据3. 编译和运行4. 示例运行输出:5. 注意事项6. 总结080. 使用C语言实现简单的数据库操作 在…...

Maven 概述、安装、配置、仓库、私服详解

目录 1、Maven 概述 1.1 Maven 的定义 1.2 Maven 解决的问题 1.3 Maven 的核心特性与优势 2、Maven 安装 2.1 下载 Maven 2.2 安装配置 Maven 2.3 测试安装 2.4 修改 Maven 本地仓库的默认路径 3、Maven 配置 3.1 配置本地仓库 3.2 配置 JDK 3.3 IDEA 配置本地 Ma…...

让回归模型不再被异常值“带跑偏“,MSE和Cauchy损失函数在噪声数据环境下的实战对比

在机器学习的回归分析中&#xff0c;损失函数的选择对模型性能具有决定性影响。均方误差&#xff08;MSE&#xff09;作为经典的损失函数&#xff0c;在处理干净数据时表现优异&#xff0c;但在面对包含异常值的噪声数据时&#xff0c;其对大误差的二次惩罚机制往往导致模型参数…...

C/C++ 中附加包含目录、附加库目录与附加依赖项详解

在 C/C 编程的编译和链接过程中&#xff0c;附加包含目录、附加库目录和附加依赖项是三个至关重要的设置&#xff0c;它们相互配合&#xff0c;确保程序能够正确引用外部资源并顺利构建。虽然在学习过程中&#xff0c;这些概念容易让人混淆&#xff0c;但深入理解它们的作用和联…...

MyBatis中关于缓存的理解

MyBatis缓存 MyBatis系统当中默认定义两级缓存&#xff1a;一级缓存、二级缓存 默认情况下&#xff0c;只有一级缓存开启&#xff08;sqlSession级别的缓存&#xff09;二级缓存需要手动开启配置&#xff0c;需要局域namespace级别的缓存 一级缓存&#xff08;本地缓存&#…...

全面解析数据库:从基础概念到前沿应用​

在数字化时代&#xff0c;数据已成为企业和社会发展的核心资产&#xff0c;而数据库作为存储、管理和处理数据的关键工具&#xff0c;在各个领域发挥着举足轻重的作用。从电商平台的商品信息管理&#xff0c;到社交网络的用户数据存储&#xff0c;再到金融行业的交易记录处理&a…...