java添加水印等比缩放
/**
* 图片天加文字水印(默认缩小scale)
* 备注:
* Positions.BOTTOM_RIGHT 表示水印位置
*
* @param filePath 原图路径
* @param newFilePath 处理后新图片路径
* @param markText 水印文字
* @param scale 比例(0.1- 1.0)
* @param transparency 水印透明度
* @param type 1 按比例 2按宽高
* @param widthSize 缩放宽
* @param heightSize 缩放高
*/
public static boolean waterMarkWithText(String filePath, String newFilePath,
String markText, float scale, float transparency,
int widthSize, int heightSize, int type) {
try {
//先进行图片缩放
if (type == 1) {
imgScale(scale, filePath,newFilePath);
} else {
imgSize(widthSize,heightSize, filePath,newFilePath);
}
//获取缩放图分辨率
File file = new File(newFilePath);
BufferedImage imgBi = null;
try {
imgBi = ImageIO.read(file);
} catch (Exception e) {
e.printStackTrace();
}
//图片原始宽度
int width = imgBi.getWidth();
//图片原始高度
int height = imgBi.getHeight();
//计算右下角水印高宽
int waterWidth = new Double(width *0.5).intValue();
int waterHeight = new Double(height).intValue();
ImgHandle im = new ImgHandle();
String randomNum = String.valueOf(System.currentTimeMillis());
BufferedImage bi = im.apply(imgBi, waterWidth, waterHeight, markText, 1, randomNum);
BufferedImage bi2 = im.apply(imgBi, waterWidth, waterHeight, randomNum, 1, markText);
Watermark watermark = new Watermark(Positions.BOTTOM_LEFT,
bi, transparency);
Watermark watermark2 = new Watermark(Positions.BOTTOM_RIGHT,
bi2, transparency); Thumbnails.of(newFilePath).scale(1).
watermark(watermark).toFile(newFilePath);
watermark2(watermark2, newFilePath);
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
/**
* 生成的水印
*
* @param img
* @return
*/
public BufferedImage apply(BufferedImage img, int waterWidth, int waterHeight, String markText, float scale,
String markText2) {
int width = img.getWidth();
int height = img.getHeight(); BufferedImage imgWithWatermark = new BufferedImage(waterWidth, waterHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D g = imgWithWatermark.createGraphics(); //设置透明 start
imgWithWatermark = g.getDeviceConfiguration().createCompatibleImage(waterWidth, waterHeight, Transparency.TRANSLUCENT);
g = imgWithWatermark.createGraphics();
g.setColor(new Color(159, 160, 160));
//设置透明 end
int[] sizes = new int[]{60, 30, 20, 16, 14, 9, 8, 6, 4};
int contentLength = 0;
Font font = null;
for (int i = 0; i < 8; i++) {
//设置字体及大小
font = new Font("Helvetica Regular", Font.BOLD, sizes[i]);
g.setFont(font);
g.drawRect(0, 0, 0, 0);
contentLength = getWatermarkLength(markText + markText2, g);
if (contentLength < width) {
//找到最合适的字体
break;
}
}
//设置水印的坐标
FontDesignMetrics metrics = FontDesignMetrics.getMetrics(g.getFont());
int fontHeight = metrics.getHeight();
int y = waterHeight - fontHeight;
char[] data = markText.toCharArray();
if (markText.contains("https")) {
g.drawChars(data, 0, data.length, 4, y);
} else {
//这里的x1值需要根据你缩放后图片大小进行计算;
int x1 = 0;
if (width > 300) {
x1 = 270;
} else {
x1 = 55;
}
g.drawChars(data, 0, data.length, x1, y);
}
return imgWithWatermark;
}
//添加水印
public static boolean watermark2(Watermark watermark2, String filePath) {
try {
Thumbnails.of(filePath).scale(1).
watermark(watermark2).toFile(filePath);
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
/**
* 计算水印文字宽度
*
* @param waterMarkContent
* @param g
* @return
*/
public static int getWatermarkLength(String waterMarkContent, Graphics2D g) {
return g.getFontMetrics(g.getFont()).charsWidth(waterMarkContent.toCharArray(), 0, waterMarkContent.length());
}
/**
* 比例缩放
*
* @param scale
* @param filePath
* @param newFilePath
* @return
*/
public static boolean imgScale(float scale, String filePath, String newFilePath) {
try {
Thumbnails.of(filePath).scale(scale).toFile(newFilePath);
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
} /**
* 大小缩放
* @param width
* @param height
* @param filePath
* @param newFilePath
* @return
*/
public static boolean imgSize(int width,int height, String filePath, String newFilePath) {
try {
Thumbnails.of(filePath).size(width,height).toFile(newFilePath);
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
public static void main(String[] args) {
String filePath = "C:\\Users\\admin\\Desktop\\1000.jpg";
String newFilePath = "C:\\Users\\admin\\Desktop\\800x800_2.jpg";
boolean sc2 = waterMarkWithText(filePath, newFilePath, "https://licd.beijing2022.cn", 1f, 1.0f, 1000, 1000, 1);
}


等比缩放之后水印位置保持不变- -
java添加水印等比缩放的更多相关文章
- java图片高质量缩放类
import java.awt.Color;import java.awt.Graphics;import java.awt.Image;import java.awt.image.BufferedI ...
- java处理图片--图片的缩放,旋转和马赛克化
这是我自己结合网上的一些资料封装的java图片处理类,支持图片的缩放,旋转,马赛克化.(转载请注明出处:http://blog.csdn.net/u012116457) 不多说,上代码: packag ...
- java基础---->Java中图片的缩放
缩略图代表网页上或计算机中图片经压缩方式处理后的小图 ,其中通常会包含指向完整大小的图片的超链接.缩略图用于在 Web 浏览器中更加迅速地装入图形或图片较多的网页.今天,我们就开始java中图像的缩略 ...
- java.awt.Graphics2D 图片缩放
关键字:java image thumbnail google 粗略demo: import java.awt.Graphics2D; import java.awt.GraphicsConfig ...
- android图片缩放平移
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android=" ...
- Nginx 独立图片服务器的搭建
为什么需要独立图片服务器? 如果你留心的话,可以发现,现在主流的网站都是有单独的图片服务器的,例如,人人网的为rrimg,淘宝的为taobaocdn,下面还有很多的二级域名. 独立的图片服务器有诸多好 ...
- javaCV开发详解之4:转流器实现(也可作为本地收流器、推流器,新增添加图片及文字水印,视频图像帧保存),实现rtsp/rtmp/本地文件转发到rtmp流媒体服务器(基于javaCV-FFMPEG)
javaCV系列文章: javacv开发详解之1:调用本机摄像头视频 javaCV开发详解之2:推流器实现,推本地摄像头视频到流媒体服务器以及摄像头录制视频功能实现(基于javaCV-FFMPEG.j ...
- Android WebKit 内核
一.WebKit简介 WebKit是一个开源的浏览器网页排版引擎,包含WebCore排版引擎和JSCore引擎.WebCore和JSCore引擎来自于KDE项目的KHTML和KJS开源项目.Andro ...
- Spark案例分析
一.需求:计算网页访问量前三名 import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext} /* ...
随机推荐
- liunx 安装redis 4.0
liunx 上安装redis 4.0.1 第一步:将 redis-4.0.1.tar.gz 压缩问上传至/home目录下 第二步: 解压文件 tar -zxvf redis-4.0.1.tar.g ...
- ant 安装 网址
1 http://www.testautomationguru.com/jmeter-continuous-performance-testing-part1/ 2 http://www.testau ...
- git reset --hard xxxxxxx
关于git reset --hard xxxxxxx命令之Git版本回退 今晚代码写着写着就头脑有点发懵,手指也不听使唤了竟然让我敲出了 git reset --hard 命令,然后的然后就是之前所有 ...
- ssh 登录报错 packet_write_wait: Connection to x.x.x.x port 22: Broken pipe
问题 更新个人博客文章时遇到:Error: packet_write_wait: Connection to 192.30.253.113 port 22: Broken pipe packet_wr ...
- 'mysql' 不是内部或外部命令,也不是可运行的程序或批处理文件
今天安装完MYSQL8.0的版本,根据课本的提示,在CMD里运行,出现了'mysql' 不是内部或外部命令,也不是可运行的程序或批处理文件.在网上搜了一下,他的解决方法是这样的: 1.设置一下环境变量 ...
- Oracle 行转列pivot 、列转行unpivot 的Sql语句总结
这个比较简单,用||或concat函数可以实现 select concat(id,username) str from app_user select id||username str from ap ...
- Confluence 6 用户宏示例 - Formatted Panel
下面的用演示了如果还写一个用户宏,并在这个宏中创建一个格式化的面板,并且指定颜色.将会创建下面的面板: (Title) 注意:这个面板的标题为空,如果你没有给这个面板标题参数的话. Macro n ...
- SpringBoot捕获全局异常
1.创建GloableExceptionAop类捕获全局异常 package com.cppdy.exception; import org.springframework.web.bind.anno ...
- html 之表单,div标签等。。。。。。。
一.表单 功能:表单用于向服务器传输数据,从而实现用户与Web服务器的交互 表单能够包含input系列标签,比如文本字段.复选框.单选框.提交按钮等等. 表单还可以包含textarea.select. ...
- Python基础之继承与派生
一.什么是继承: 继承是一种创建新的类的方式,新建的类可以继承一个或过个父类,原始类成为基类或超类,新建的类则称为派生类 或子类. 其中,继承又分为:单继承和多继承. class parent_cla ...