java实现水印

package com.wawagame;
import java.awt.AlphaComposite;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.imageio.ImageIO;


import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
public class WaterMarkerImageUtil {
/**
* 保存JPEG图片
* @param image 二进制图片对象
* @param imageFile 待保存的文件名称
* @param quality 待保存的图片质量
* @throws FileNotFoundException
* @throws IOException
*/
public static void saveJPEGImage(BufferedImage image,File imageFile,
int quality) throws FileNotFoundException,IOException{
BufferedOutputStream output = null;
output = new BufferedOutputStream(
new FileOutputStream(imageFile));
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(output);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(image);
param.setQuality(quality/100F, false);
encoder.setJPEGEncodeParam(param);
encoder.encode(image);
if(output != null){
output.close();
}
}

/**
* 给图片加上水印文字
* @param imageFile 待处理的图片
* @param mark 待添加的水印文字
* @throws FileNotFoundException
* @throws IOException
*/
public static void waterMarkImage(File imageFile,String mark)
throws FileNotFoundException,IOException{
//读取图片
BufferedInputStream input = new BufferedInputStream(
new FileInputStream(imageFile));
BufferedImage image = ImageIO.read(input);

//给图片加水印效果 z
Graphics2D g2d = (Graphics2D) image.getGraphics();
int width = image.getWidth();
int height = image.getHeight();
Font font = new Font("black",Font.BOLD,10);
Rectangle2D rectangle = font.getStringBounds(mark, g2d.getFontRenderContext());
// if(width < rectangle.getWidth() + 6 || height < rectangle.getHeight() + 20)
// return;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,(float)0.8));
int x = width - (int)rectangle.getWidth() - 5;
int y = height - 10;
g2d.setFont(font);
g2d.drawString(mark, x, y);

//保存图片
String fileName = imageFile.getName();
int dotPosition = fileName.lastIndexOf(".");
String newFile = fileName.substring(0, dotPosition) + "_waterMark" + fileName.substring(dotPosition);
System.out.println("x="+x+"y:"+y);
saveJPEGImage(image, new File(newFile), 100);
}

public static void main(String[] args) {
try {
File file = new File("c:\\a.jpg");
waterMarkImage(file,"Ujiang.com");
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值