有问题,我给一个50K的图片加了个水印后她就变成100多K了,有人对BufferedImage熟悉点吗?
package j2src;
import java.*;
import java.io.*;
import javax.*;
import com.sun.image.codec.jpeg.*;
public class ShuiYin {
public static void main(String[] args)
{
createMark( "c:/photo.JPG ", "c:/水印.gif ");
}
/**
* 在图片上添加水印图片和文字
* @param filePath 原图片文件路径
* @param watermark 水印图片文件路径
* @return 添加成功返回true,否则返回false
*/
public static boolean createMark(String filePath, String watermark)
{
//读取原图片
ImageIcon imgIcon = new ImageIcon(filePath);
Image theImg = imgIcon.getImage();
//读取标签图片
ImageIcon waterIcon = new ImageIcon(watermark);
Image waterImg = waterIcon.getImage();
// int width = theImg.getWidth(null);
// int height = theImg.getHeight(null);
//创建一个和原图片同大小的新空白图片
//BufferedImage bimage = new BufferedImage(600, 450,BufferedImage.TYPE_INT_RGB);
BufferedImage bimage = new BufferedImage(600, 450,BufferedImage.TYPE_INT_BGR);
Graphics2D g = bimage.createGraphics();
//设置字体
//Font font = new Font( "SansSerif ",Font.BOLD,30);
//g.setFont(font);
//设置前景色
g.setColor(Color.red);
//设置背景色
g.setBackground(Color.white);
//画原图
g.drawImage(theImg, 0, 0, null);
//画水印图
g.drawImage(waterImg, 470, 30, null);
//画字
// g.drawString( "中国人民共和国 ", 10, 10);
g.dispose();
FileOutputStream out = null;
try {
out = new FileOutputStream(filePath);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bimage);
param.setQuality(50f, true);
encoder.encode(bimage, param);
}
catch (Exception e)
{
return false;
}
finally
{
if(out!=null)
{
try
{
out.close();
out = null;
}
catch(Exception e)
{}
}