java实现给图片添加水印和文字
2008-05-21 09:13
906 查看
import java.io.*;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import com.sun.image.codec.jpeg.*;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.awt.*;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.awt.image.BufferedImage;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.awt.image.*;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import javax.swing.*;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class Test...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public static void main(String[]args) throws Exception...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//1.jpg是你的 主图片的路径
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
InputStream is = new FileInputStream("1.jpg");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//通过JPEG图象流创建JPEG数据流解码器
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
JPEGImageDecoder jpegDecoder = JPEGCodec.createJPEGDecoder(is);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//解码当前JPEG数据流,返回BufferedImage对象
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
BufferedImage buffImg = jpegDecoder.decodeAsBufferedImage();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//得到画笔对象
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Graphics g = buffImg.getGraphics();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//创建你要附加的图象。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//2.jpg是你的小图片的路径
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ImageIcon imgIcon = new ImageIcon("2.jpg");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//得到Image对象。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Image img = imgIcon.getImage();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//将小图片绘到大图片上。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//5,300 .表示你的小图片在大图片上的位置。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
g.drawImage(img,5,330,null);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//设置颜色。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
g.setColor(Color.BLACK);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//最后一个参数用来设置字体的大小
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Font f = new Font("宋体",Font.BOLD,30);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
g.setFont(f);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//10,20 表示这段文字在图片上的位置(x,y) .第一个是你设置的内容。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
g.drawString("默哀555555。。。。。。。",10,30);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
g.dispose();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
OutputStream os = new FileOutputStream("union.jpg");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//创键编码器,用于编码内存中的图象数据。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
JPEGImageEncoder en = JPEGCodec.createJPEGEncoder(os);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
en.encode(buffImg);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
is.close();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
os.close();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println ("合成结束。。。。。。。。");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//图片的为位置控制: 修改代码中的: g.drawImage(img,5,330,null); 5(x) 和 330(y)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//内容和字显示的位置: g.drawString("默哀555555。。。。。。。",10,30);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import com.sun.image.codec.jpeg.*;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.awt.*;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.awt.image.BufferedImage;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.awt.image.*;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import javax.swing.*;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class Test...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public static void main(String[]args) throws Exception...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//1.jpg是你的 主图片的路径
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
InputStream is = new FileInputStream("1.jpg");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//通过JPEG图象流创建JPEG数据流解码器
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
JPEGImageDecoder jpegDecoder = JPEGCodec.createJPEGDecoder(is);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//解码当前JPEG数据流,返回BufferedImage对象
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
BufferedImage buffImg = jpegDecoder.decodeAsBufferedImage();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//得到画笔对象
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Graphics g = buffImg.getGraphics();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//创建你要附加的图象。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//2.jpg是你的小图片的路径
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ImageIcon imgIcon = new ImageIcon("2.jpg");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//得到Image对象。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Image img = imgIcon.getImage();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//将小图片绘到大图片上。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//5,300 .表示你的小图片在大图片上的位置。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
g.drawImage(img,5,330,null);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//设置颜色。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
g.setColor(Color.BLACK);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//最后一个参数用来设置字体的大小
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Font f = new Font("宋体",Font.BOLD,30);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
g.setFont(f);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//10,20 表示这段文字在图片上的位置(x,y) .第一个是你设置的内容。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
g.drawString("默哀555555。。。。。。。",10,30);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
g.dispose();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
OutputStream os = new FileOutputStream("union.jpg");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//创键编码器,用于编码内存中的图象数据。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
JPEGImageEncoder en = JPEGCodec.createJPEGEncoder(os);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
en.encode(buffImg);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
is.close();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
os.close();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println ("合成结束。。。。。。。。");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//图片的为位置控制: 修改代码中的: g.drawImage(img,5,330,null); 5(x) 和 330(y)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//内容和字显示的位置: g.drawString("默哀555555。。。。。。。",10,30);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
相关文章推荐
- Java实现在图片上添加水印图片和水印文字
- 用java实现给图片增加图片水印或者文字水印(也支持视频图像帧添加水印)
- java实现给图片添加水印和文字
- JAVA实现图片的修改,添加文字水印效果
- javacpp-opencv图像处理之1:实时视频添加文字水印并截取视频图像保存成图片,实现文字水印的字体、位置、大小、粗度、翻转、平滑等操作
- 用java实现给图片增加图片水印或者文字水印(也支持视频图像帧添加水印)
- 用Java实现在图片上添加水印图片和水印文字
- 用java实现给图片增加图片水印或者文字水印(也支持视频图像帧添加水印)
- javacpp-opencv图像处理之1:实时视频添加文字水印并截取视频图像保存成图片,实现文字水印的字体、位置、大小、粗度、翻转、平滑等操作
- java实现给图片添加水印和文字
- 用java实现给图片增加图片水印或者文字水印(也支持视频图像帧添加水印)
- Java实现给图片添加图片水印,文字水印及马赛克的方法示例
- java实现给图片添加水印和文字
- 用Java实现给图片添加文字水印
- javacpp-opencv图像处理之2:实时视频添加图片水印,实现不同大小图片叠加,图像透明度控制,文字和图片双水印
- Java代码实现给图片添加文字水印 详细教程一
- javacpp-opencv图像处理之2:实时视频添加图片水印,实现不同大小图片叠加,图像透明度控制,文字和图片双水印
- Java实现给图片添加水印
- java给图片添加文字水印并消除锯齿
- JAVA编写的图片水印添加(文字水印、图片水印)