当前位置:网站首页>Add a logo to the upper left corner of the picture, add time and address to the lower left corner, and wrap the line when the address reaches the specified length
Add a logo to the upper left corner of the picture, add time and address to the lower left corner, and wrap the line when the address reaches the specified length
2022-07-29 22:16:00 【~Memories】
效果
1.Add to photoslogo水印
(1)添加pom依赖
<dependency>
<groupId>net.coobird</groupId>
<artifactId>thumbnailator</artifactId>
<version>0.4.8</version>
</dependency>
/** * Add to photosLogo水印 * @param projectPath 项目路径 * @param finalImageWater Add the watermark background image storage address * @throws Exception */
private void addWaterLogoImage(String projectPath, String finalImageWater) throws Exception{
//读取原图,获取宽高
File file = new File(projectPath + "/WEB-INF/template/image.jpg");
BufferedImage image = ImageIO.read(file);
//Read the watermark image
BufferedImage waterImg = ImageIO.read(new File(projectPath + "/WEB-INF/template/雨虹logo.jpg"));
double bl = 0.18;//The watermark is the proportion of the original image width
//Calculate the new watermark width and height according to the scale
int waterWidth = (int) (image.getWidth() * bl);
int waterHeight = waterWidth * waterImg.getHeight() / waterImg.getWidth();
waterImg = Thumbnails.of(waterImg).size(waterWidth, waterHeight).keepAspectRatio(false).asBufferedImage();
//Use the new watermark to perform adding a watermark Positions.TOP_LEFT 将logo放到左上角
Thumbnails.of(file).watermark(Positions.TOP_LEFT, waterImg, 1f).scale(1).toFile(finalImageWater);
}
2.Add time to photos、Address watermark and implement line wrapping effect when the address is too long
/** * 添加时间、Address watermark * @param finalImageWater */
private void addWaterFontImage(String finalImageWater, String address) {
try {
String waterFont = "时 间:"+ DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss");
address = "地 址:"+address;
// 读取原图片信息
File srcImgFile = new File(finalImageWater);
Image srcImg = ImageIO.read(srcImgFile);
int srcImgWidth = srcImg.getWidth(null);
int srcImgHeight = srcImg.getHeight(null);
// 初始化画布,图片多大,How big is the canvas
BufferedImage bufImg = new BufferedImage(srcImgWidth, srcImgHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D g = bufImg.createGraphics();
//指定要绘制的图片
g.drawImage(srcImg, 0, 0, srcImgWidth, srcImgHeight, null);
double fontLength = srcImgWidth * 0.3;//Set the length of the text according to the proportion,That is, the text length
int fontSize = (int) (fontLength / 5 * 0.6);//文字大小,0.9为差值
//设置字体样式,加粗,大小
Font font = new Font("黑体", Font.BOLD, fontSize);
//设置文字颜色
g.setColor(Color.white);
g.setFont(font);
//The horizontal and vertical coordinates of the drawn text
/** * 控制换行 */
ArrayList<String> line_list = new ArrayList<>();
FontRenderContext frc = g.getFontRenderContext();
g.getFontRenderContext();
Rectangle2D stringBounds = font.getStringBounds(address, frc);
double fontWidth = stringBounds.getWidth();
if(fontWidth <= bufImg.getWidth()){
line_list.add(address);
}else {
int text_width = bufImg.getWidth()-60;// Output text width, Here, the canvas width is used as the text width test
double bs = fontWidth/text_width ;// The text length is a multiple of the text box length
int line_char_count = (int)Math.ceil(address.length() / bs) ;// Approximate number of words per line
int begin_index = 0 ;
while(begin_index <address.length()){
int end_index = begin_index + line_char_count ;
if(end_index>= address.length()){
end_index = address.length();
}
String line_str = address.substring(begin_index, end_index);
Rectangle2D tempStringBounds = font.getStringBounds(line_str, frc);
int tzcs = 0 ;// 调整次数
int tzzs = 1 ;// Adjust word count, When the string length of the temporary text is greater than the required text width, Decrease the word count of the temporary text each time, Then re-measure the text width
while(tempStringBounds.getWidth()> text_width){
line_str = line_str.substring(0, line_str.length() - tzzs );// 每次向前 tzzs characters are recalculated (待优化)
tempStringBounds = font.getStringBounds(line_str, frc);
tzcs++;
}
System.out.println("调整"+(end_index - begin_index -line_str.length())+"个数字, 调整了"+tzcs+"次, Current line width:"+tempStringBounds.getWidth());
line_list.add(line_str);
begin_index = begin_index + line_str.length() ;
}
}
int gao = 0;
if(srcImgHeight > srcImgWidth){
gao = 90;
}else{
gao = 100;
}
for(int i = 0;i<line_list.size();i++){
if(i == 0){
//Draw time watermark
g.drawString(waterFont, 20, (i+(srcImgHeight/gao))*80);
}
String line_str = line_list.get(i);
//Draws an address watermark
g.drawString(line_str, 20, (i+1+(srcImgHeight/gao))*80);//35 为每行的高度
}
g.dispose();
// 输出图片
Thumbnails.of(srcImgFile).watermark(Positions.BOTTOM_LEFT, bufImg, 1f).scale(1).toFile(finalImageWater);
}catch (Exception e){
e.printStackTrace();
}
}
边栏推荐
- 华为畅享50 Pro评测:HarmonyOS加持 更流畅更安全
- 【593. Valid Square】
- 容器网络硬核技术内幕 (22) 安全与自由兼顾
- WeChat Mini Program 31 Subcontracting Mechanism
- 小程序微信定位不准
- 第二好PyTorch新手课程;论文写作指南;使用µGo语言开发迷你编译器;超高效使用Transformer的扩展库;前沿论文 | ShowMeAI资讯日报
- Writing Elegant Kotlin Code: Talk About What I Think "Kotlinic"
- linux使用脚本安装redis
- 刘畊宏男孩女孩看过来!运动数据分析挖掘!(附全套代码和数据集)
- golang文件行号探索
猜你喜欢
笔记:fgets函数详解
Dry goods!Cooperative Balance in Federated Learning
高通WLAN框架学习(31)-- Power save
赶紧进来!!!带你认识C语言基本数据类型
5V升压充电8.4V芯片
Liu Genghong, boys and girls, come here!Sports data analysis and mining!(with a full set of code and data sets)
人社部公布“数据库运行管理员”成新职业,OceanBase参与制定职业标准
Verilog 加法器设计
The sword refers to Offer II 097. Number of subsequences
全自动化机器学习建模!效果吊打初级炼丹师!
随机推荐
容器网络硬核技术内幕 (22) 安全与自由兼顾
The demand for VR live broadcast marketing is increasing, and the data module is paving the way for us
微信小程序 31 分包机制
容器网络硬核技术内幕 (24) 知微知彰,知柔知刚 (上)
【点云】M3DeTR: Multi-representation, Multi-scale, Mutual-relation 3D Object Detection with Transformers
华为畅享50 Pro评测:HarmonyOS加持 更流畅更安全
In 2022, you still can't "low code"?Data science can also play with Low-Code!
Analysis of Crypto in Pi 2
Minimal jvm source code analysis
Fully automated machine learning modeling!The effect hangs the primary alchemist!
PointPillars 工程复现
容器网络硬核技术内幕 (26) 知微知彰,知柔知刚 (下)
第3章业务功能开发(线索关联市场活动,动态搜索)
[ACTF2020 Freshman Competition]Exec 1
ALBERT:A Lite BERT for Self-supervised Learning of Language Representations
全自动化机器学习建模!效果吊打初级炼丹师!
2022了你还不会『低代码』?数据科学也能玩转Low-Code啦!
在Ferora35中安装oracle-database-xe-21c
解释器模式
IDEA 快捷键