日期:2014-05-20 浏览次数:20919 次
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import javax.swing.*;
import javax.imageio.ImageIO;
public class Demo {
public static void main(String[] args)throws IOException{
ImageIcon imgIcon = new ImageIcon("aa.png");
Image theImg = imgIcon.getImage();
int width = theImg.getWidth(null);
int height = theImg.getHeight(null);
BufferedImage bimage = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
Graphics2D g = bimage.createGraphics();
g.drawImage(theImg, 0, 0, null);
g.setBackground(Color.white);
g.setColor(Color.red);
g.drawString("水印文字", width / 5, height / 5);
g.dispose();
ImageIO.write(bimage, "PNG", new File("bb.jpg"));
}
}
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class Demo {
public static Font NameFont = new Font("宋体", Font.BOLD, 37);
public static void main(String[] args) throws IOException {
BufferedImage image = new BufferedImage(220, 100, BufferedImage.TYPE_4BYTE_ABGR_PRE);
Graphics2D g2d = image.createGraphics();
g2d.setColor(Color.BLACK);
g2d.setFont(NameFont);
g2d.drawString("java学习", 10f, 40f);
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g2d.drawString("加油", 10f, 80f);
ImageIO.write(image, "PNG", new File("aa.jpg"));
}
}