跟在图片中加上自己的logo思路一样,动态生成图片。 BufferedImage bi = new BufferedImage(imagewidth, imageheight,
BufferedImage.TYPE_INT_RGB);
Graphics gra = bi.getGraphics(); gra.drawString("something", 0, imageheight); JPEGCodec.createJPEGEncoder(response.getOutputStream()).encode(bi);
response.getOutputStream().close();
BufferedImage.TYPE_INT_RGB);
Graphics gra = bi.getGraphics(); gra.drawString("something", 0, imageheight); JPEGCodec.createJPEGEncoder(response.getOutputStream()).encode(bi);
response.getOutputStream().close();
import java.awt.image.*;import com.sun.image.codec.jpeg.*;public class WaterMark {
public WaterMark() {
super();
} public static void main(String[] args) {
String imgpath = "c:/test.jpg";
try {
java.awt.Toolkit toolkit = java.awt.Toolkit.getDefaultToolkit();
java.awt.Image appimg = toolkit.getImage(imgpath);
checkImage(appimg);
int biWidth = appimg.getWidth(null);
int biHeight = appimg.getHeight(null);
BufferedImage bi = new BufferedImage(biWidth, biHeight,
BufferedImage.TYPE_INT_RGB);
Graphics gra = bi.getGraphics();
gra.drawImage(appimg, 0, 0, null);
gra.setFont(new Font("Dialog", Font.BOLD, 22));
gra.setColor(Color.PINK);
gra.drawString("power by pigo", 50, 50);
java.io.FileOutputStream fo = new FileOutputStream("c:/testnew.jpg");
JPEGCodec.createJPEGEncoder(fo).encode(
bi);
fo.close();
} catch (Exception ex) {
System.out.println("捕获所有错误" + ex.getMessage());
ex.printStackTrace();
}
} private static void checkImage(Image image) {
waitForImage(image);
if (image.getWidth(null) < 1) {
throw new IllegalArgumentException("image width " + image.getWidth(null) +
" is out of range");
}
if (image.getHeight(null) < 1) {
throw new IllegalArgumentException("image height " +
image.getHeight(null) +
" is out of range");
}
} private static void waitForImage(Image image) {
try {
MediaTracker tracker = new MediaTracker(new Component() {});
tracker.addImage(image, 0);
tracker.waitForID(0);
// loadStatus = tracker.statusID( 0, false );
tracker.removeImage(image, 0);
} catch (InterruptedException e) {
System.out.println("加载图片的时候出错了:错误信息如下:");
e.printStackTrace();
}
} }