工作中遇到要用java在后台给移动端上传上来的图片添加一个时间的水印。上传上来的本来是图片流。我在网上搜索的都是给图片添加的,但是我又不想通过流生成临时图片,添加水印之后再上传。所以修改了一下网上的那种给文件添加水印的方法,现在传进去从前台或者移动端上传的文件即可添加上水印。
代码如下:
/**
* 添加文字水印
* @param pressText 水印文字,
* @param x 水印文字距离目标图片左侧的偏移量,如果x<0, 则在正中间
* @param y 水印文字距离目标图片上侧的偏移量,如果y<0, 则在正中间
* 注:若x,y都等于1,则在图片的右下方
* @param alpha 透明度(0.0 -- 1.0, 0.0为完全透明,1.0为完全不透明)
*/
public void pressText(MultipartFile attach,String pressText,int x,int y,float alpha){
try {
CommonsMultipartFile cf= (CommonsMultipartFile)attach; //这个myfile是MultipartFile的
DiskFileItem fi = (DiskFileItem)cf.getFileItem();
File file = fi.getStoreLocation();
Image image = ImageIO.read(file);
int width = image.getWidth(null);
int height = image.getHeight(null);
BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = bufferedImage.createGraphics();
g.drawImage(image,0,0, width, height, null);
g.setFont(new Font("微软雅黑",Font.BOLD,24));
g.setBackground(Color.white);
g.setColor(Color.red);
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
int width_wi = 24*getTextLength(pressText);
int height_wi = 24;
int widthDiff = width-width_wi;
int heightDiff = height-height_wi;
if(x<0){
x = widthDiff/2;
}else if(x>widthDiff){
x=widthDiff;
}
if(y<0){
y = heightDiff/2;
}else if(y>heightDiff){
y = heightDiff;
}
if(x==1&&y==1){
x = width-width_wi-70;
y = height-height_wi;
}
g.drawString(pressText, x, y);//水印文件 y+height_wi
g.dispose();
ImageIO.write(bufferedImage, "JPEG", file);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 计算文字像素长度
* @param text
* @return
*/
private int getTextLength(String text){
int textLength = text.length();
int length = textLength;
for (int i = 0; i < textLength; i++) {
int wordLength = String.valueOf(text.charAt(i)).getBytes().length;
if(wordLength > 1){
length+=(wordLength-1);
}
}
return length%2==0 ? length/2:length/2+1;
}
上传的接口代码如下
@RequestMapping(value = "/upload",method=RequestMethod.POST)
@ResponseBody
public String uploadFile(@RequestParam("files") MultipartFile[] files,HttpServletRequest request, HttpServletResponse response){
List<String> pathArray = new LinkedList<String>();
String retStr = "";
try {
//FileManagerConfig.TRACKER_NGNIX_ADDR = fastDFS;
// 获取文件后缀名
for(MultipartFile attach:files){
//1.0代表水印不透明,0.1-1.0为透明度设置
pressText(attach, "这里是你要添加的水印文字", 1, 1, 1.0f);
System.out.println("attach.getOriginalFilename()======"+attach.getOriginalFilename());
String ext = attach.getOriginalFilename().substring(attach.getOriginalFilename().lastIndexOf(".")+1);
//下面的attach.getBytes()获取的字节流中就带有水印了。下面的方法是上传到图片服务器的。
FastDFSFile file = new FastDFSFile(attach.getBytes(),ext);
NameValuePair[] meta_list = new NameValuePair[4];
meta_list[0] = new NameValuePair("fileName", attach.getOriginalFilename());
meta_list[1] = new NameValuePair("fileLength", String.valueOf(attach.getSize()));
meta_list[2] = new NameValuePair("fileExt", ext);
meta_list[3] = new NameValuePair("fileAuthor", "hzxh");
String filePath = FileManager.upload(file,meta_list);
pathArray.add(filePath);
}
for(int i=0;i<pathArray.size();i++){
retStr += pathArray.get(i);
if(i<pathArray.size()-1)retStr += "@";
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("------添加图片-----------" + retStr);
return retStr;
}
因为移动端一次上传多个文件,所以我用的一个数组接收的上传的文件。
上传到文件服务器的源码见:http://download.csdn.net/download/it_java_shuai/9960309
希望可以帮到大家!