Java 简单实现图片水印

Java 简单实现图片水印 Java 简单实现图片水印


具体实现:

public class Test {

	public static void main(String[] args) {
		Test t= new Test();
		String path="c:\\test.gif";
		String target="c:\\test1.gif";
		String key= "Hello World";
		t.addImageTag(path,target,key);
		
	}

	public void addImageTag(String path,String target,String keyword) {
		File file = new File(path);
		try {
			Image img = ImageIO.read(file);
			int w = img.getWidth(null);
			int h = img.getHeight(null);
			System.out.println("File size: " +w + "px * " + h + "px");
			BufferedImage outimg = new BufferedImage(w,h,BufferedImage.TYPE_3BYTE_BGR);
			Graphics g = outimg.getGraphics();
			g.drawImage(img, 0, 0, w, h, null);
			Font font = new Font(null,Font.ITALIC,20);
			g.setFont(font);
			g.drawString(keyword, w/2, h-10);
			g.dispose();
			FileOutputStream out = new FileOutputStream(target);
			JPEGImageEncoder jpg = JPEGCodec.createJPEGEncoder(out);
			jpg.encode(outimg);
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

你可能感兴趣的:(java,C++,c,C#)