测试水印工具类,优化修改文件扩展名代码
This commit is contained in:
@@ -1,44 +1,60 @@
|
||||
package com.micro.fast.common.util;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
/**
|
||||
* 文件操作工具类
|
||||
* create by lishouyu 2017/08/27
|
||||
* email 18332763730@163.com
|
||||
*
|
||||
* last edit lishouyu 2017/08/27
|
||||
* last edit lishouyu 2017/09/07
|
||||
* email 18332763730@163.com
|
||||
* 添加递归修改文件夹下所有特定文件的扩展名的方法
|
||||
*/
|
||||
public class FileUtil {
|
||||
/**
|
||||
* 修改文件夹下的所有符合条件的文件的扩展名,递归文件夹下所有文件
|
||||
* @param dir 文件夹下的路径
|
||||
* @param oldExtensionName 旧的文件扩展名
|
||||
* @param newExtensionName 新的文件扩展名
|
||||
*/
|
||||
public static void ModifyFileExtensionNameInDir(File dir,String oldExtensionName,String newExtensionName){
|
||||
//判断输入的文件是否是文件夹
|
||||
boolean isdirectory = dir.isDirectory();
|
||||
if (isdirectory==true){
|
||||
//获取目录下的所有的文件以及目录
|
||||
File[] files = dir.listFiles();
|
||||
if (files.length>0){
|
||||
for (File file:files){
|
||||
if (file.isDirectory()){
|
||||
ModifyFileExtensionNameInDir(file,oldExtensionName,newExtensionName);
|
||||
}else{
|
||||
String fileName = file.getName();
|
||||
if (fileName.endsWith(oldExtensionName)){
|
||||
//去掉扩展名
|
||||
int lastIndexOfPoint = fileName.lastIndexOf('.');
|
||||
String newFilename = fileName.substring(0, lastIndexOfPoint);
|
||||
//重命名文件
|
||||
file.renameTo(new File(file.getParent()+"/"+newFilename+newExtensionName));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 修改文件夹下的所有符合条件的文件的扩展名,递归文件夹下所有文件
|
||||
*
|
||||
* @param dir 文件夹下的路径
|
||||
* @param oldExtensionName 旧的文件扩展名 .jpg
|
||||
* @param newExtensionName 新的文件扩展名 .png
|
||||
*/
|
||||
public static void modifyFileExtensionNameInDir(File dir, String oldExtensionName, String newExtensionName) {
|
||||
//1.判断输入的文件是否是文件夹
|
||||
boolean isdirectory = dir.isDirectory();
|
||||
if (isdirectory == true) {
|
||||
//获取目录下的所有的文件以及目录
|
||||
File[] files = dir.listFiles();
|
||||
if (files != null && files.length > 0) {
|
||||
for (File file : files) {
|
||||
if (file.isDirectory()) {
|
||||
modifyFileExtensionNameInDir(file, oldExtensionName, newExtensionName);
|
||||
} else {
|
||||
modifyFileExtensionName(file,oldExtensionName,newExtensionName);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
File file = dir;
|
||||
modifyFileExtensionName(file,oldExtensionName,newExtensionName);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改单个文件的扩展名
|
||||
* @param file
|
||||
* @param oldExtensionName
|
||||
* @param newExtensionName
|
||||
*/
|
||||
public static void modifyFileExtensionName(File file, String oldExtensionName, String newExtensionName) {
|
||||
String fileName = file.getName();
|
||||
if (fileName.endsWith(oldExtensionName)) {
|
||||
//去掉扩展名
|
||||
int lastIndexOfPoint = fileName.lastIndexOf('.');
|
||||
String newFilename = fileName.substring(0, lastIndexOfPoint);
|
||||
//重命名文件
|
||||
file.renameTo(new File(file.getParent() + "/" + newFilename + newExtensionName));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,6 +1,9 @@
|
||||
package com.micro.fast.common.util;
|
||||
|
||||
|
||||
/**
|
||||
* 永远
|
||||
*/
|
||||
public class My {
|
||||
private static final String world="李新";
|
||||
@Override
|
||||
|
||||
@@ -0,0 +1,183 @@
|
||||
package com.micro.fast.common.util;
|
||||
|
||||
import com.sun.image.codec.jpeg.JPEGCodec;
|
||||
import com.sun.image.codec.jpeg.JPEGImageEncoder;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import java.awt.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* 图片添加水印工具类
|
||||
* create by lishouyu 2017/09/05
|
||||
* email 18332763730@163.com
|
||||
*
|
||||
* last edit lishouyu 2017/09/07
|
||||
* email 18332763730@163.com
|
||||
*/
|
||||
public class WatermarkerUtil {
|
||||
|
||||
/**
|
||||
* 默认的字体
|
||||
*/
|
||||
private String fontFamily = "微软雅黑";
|
||||
/**
|
||||
* 默认的字体样式
|
||||
*/
|
||||
private int fontStyle = Font.BOLD;
|
||||
/**
|
||||
* 默认的文字大小
|
||||
*/
|
||||
private int fontSize = 12;
|
||||
/**
|
||||
* 默认的字体颜色
|
||||
*/
|
||||
private Color fontColor = Color.WHITE;
|
||||
/**
|
||||
* 默认的水印右侧距离原图最右边的距离
|
||||
*/
|
||||
private int x = 10;
|
||||
/**
|
||||
* 默认的水印的下侧距离原图下侧的距离
|
||||
*/
|
||||
private int y = 10;
|
||||
/**
|
||||
* 默认的透明度
|
||||
*/
|
||||
private float alpha = 0.9F;
|
||||
|
||||
/**
|
||||
* 为图片添加文字水印
|
||||
* @param text 要添加的水印的文字内容
|
||||
* @param inputImage 输入的图片文件
|
||||
* @param filePath 生成图片的输出路径
|
||||
* @return
|
||||
*/
|
||||
public File markerWord(String text,File inputImage,String filePath){
|
||||
String imageName = inputImage.getName();
|
||||
File file;
|
||||
try {
|
||||
//1.将输入的图片文件解码为图片格式
|
||||
Image image = ImageIO.read(inputImage);
|
||||
//2.获取原图的宽度和高度
|
||||
int width = image.getWidth(null);
|
||||
int height = image.getHeight(null);
|
||||
//3.创建BufferImage对象用于存放原图片.图片的高度和宽度等于原图片的高度和宽度,颜色采用rbg模式
|
||||
BufferedImage bufferedImage = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
|
||||
//4.使用bufferImage对象创建画图工具对象,并将原图绘制到bufferImage中
|
||||
Graphics2D graphics = bufferedImage.createGraphics();
|
||||
graphics.drawImage(image,0,0,width,height,null);
|
||||
//5.设置水印字体的样式
|
||||
graphics.setFont(new Font(fontFamily,fontStyle,fontSize));
|
||||
graphics.setColor(fontColor);
|
||||
//6.设置水印图片的位置
|
||||
//6.1获取原图片和水印的高度以及宽度差,调整图片的位置
|
||||
int textLength = getTextLength(text);
|
||||
int widthDiff = width-textLength;
|
||||
int heightDiff = height-getFontSize();
|
||||
if (widthDiff<x){
|
||||
x=widthDiff;
|
||||
}
|
||||
if (heightDiff<y){
|
||||
y=heightDiff;
|
||||
}
|
||||
//7.设置水印透明的样式
|
||||
graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,alpha));
|
||||
//8.将文字画在图片之上,放在右下角.参数x表示是文字的左侧距离图片左侧的距离,文字的右侧表示是文字的下侧距离图片上册的距离
|
||||
graphics.drawString(text,width-(x+textLength),height-(y));
|
||||
graphics.dispose();
|
||||
//9.将bufferImage输出成文件,使用uuid避免文件名重复
|
||||
file = new File(filePath + "/" + UUID.randomUUID()+ imageName);
|
||||
JPEGImageEncoder img = JPEGCodec.createJPEGEncoder(new FileOutputStream(file));
|
||||
img.encode(bufferedImage);
|
||||
//10.返回加上水印之后的图片文件对象
|
||||
return file;
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
//11.发生异常的时候就返回null
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算文本的长度 返回值是向上取整后的数值
|
||||
* 一个英文的宽度等于0.5*fontSize 英文字符的字节码小于1
|
||||
* 一个中为跨度等于1*fontSize 中文字符的字节码大于1
|
||||
* @param text
|
||||
* @return
|
||||
*/
|
||||
public int getTextLength(String text){
|
||||
float textWidth = 0.0F;
|
||||
//1.计算等同于多少个中文字符
|
||||
for (int i = 0; i <text.length() ; i++) {
|
||||
String s = String.valueOf(text.charAt(i));
|
||||
if (s.getBytes().length>1){
|
||||
textWidth++;
|
||||
}else{
|
||||
textWidth +=0.5F;
|
||||
}
|
||||
}
|
||||
//2.等同的中文字符向上取整然后乘于单个字符的宽度
|
||||
return (int) Math.ceil(textWidth)*fontSize;
|
||||
}
|
||||
|
||||
public String getFontFamily() {
|
||||
return fontFamily;
|
||||
}
|
||||
|
||||
public void setFontFamily(String fontFamily) {
|
||||
this.fontFamily = fontFamily;
|
||||
}
|
||||
|
||||
public int getFontStyle() {
|
||||
return fontStyle;
|
||||
}
|
||||
|
||||
public void setFontStyle(int fontStyle) {
|
||||
this.fontStyle = fontStyle;
|
||||
}
|
||||
|
||||
public int getFontSize() {
|
||||
return fontSize;
|
||||
}
|
||||
|
||||
public void setFontSize(int fontSize) {
|
||||
this.fontSize = fontSize;
|
||||
}
|
||||
|
||||
public Color getFontColor() {
|
||||
return fontColor;
|
||||
}
|
||||
|
||||
public void setFontColor(Color fontColor) {
|
||||
this.fontColor = fontColor;
|
||||
}
|
||||
|
||||
public int getX() {
|
||||
return x;
|
||||
}
|
||||
|
||||
public void setX(int x) {
|
||||
this.x = x;
|
||||
}
|
||||
|
||||
public int getY() {
|
||||
return y;
|
||||
}
|
||||
|
||||
public void setY(int y) {
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
public float getAlpha() {
|
||||
return alpha;
|
||||
}
|
||||
|
||||
public void setAlpha(float alpha) {
|
||||
this.alpha = alpha;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.micro.fast.common.util;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class WatermarkerUtilTest {
|
||||
@Test
|
||||
public void markerWord() throws Exception {
|
||||
new WatermarkerUtil().markerWord("李守余",new File("/root/dev/u=132629542,1898058257&fm=27&gp=0.jpg"),"/home");
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user