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 water 水印图片文件路径
* @return 添加成功返回true,否则返回false
*/
public static boolean createMark(String filePath, String water)
{
//读取原图片
ImageIcon imgIcon = new ImageIcon(filePath);
Image theImg = imgIcon.getImage();
//读取标签图片
ImageIcon waterIcon = new ImageIcon(water);
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)
{}
}
}
return true;
}
}
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 water 水印图片文件路径
* @return 添加成功返回true,否则返回false
*/
public static boolean createMark(String filePath, String water)
{
//读取原图片
ImageIcon imgIcon = new ImageIcon(filePath);
Image theImg = imgIcon.getImage();
//读取标签图片
ImageIcon waterIcon = new ImageIcon(water);
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)
{}
}
}
return true;
}
}
BufferedImage bimage = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
Graphics2D g = bimage.createGraphics();
g.drawImage(theImg, 0, 0, null);
//画水印图
g.drawImage(waterImg, 590, 30, null);
//画字关键都在这里,他的大小不是我的图片的大小+水印的大小,是他重新建立了一个空白的图片,然后把我的图片放上去,再把水印放上去,他不压缩!!!!
怎么办?能不能让他之间把水印放到我的图片上?
param.setQuality(50f, true);
改为
param.setQuality(0.5f, true);
这样就变小了http://www.programmerstudy.com