在图上添加水印文字,可以自己添加字体


package com;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontFormatException;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

/**
 * 
 * @author Simba0617
 *	运行时请将 只有一个main方法,其他的原封不动
 */
public class Test {
	/**
	 * 
	 * @param args
	 * @throws IOException
	 * 				main1可以单独运行的
	 */
	public static void main1(String[] args) throws IOException {
		File file=new File("d:/hh/a.jpg");
		//得到原图
		Image img=ImageIO.read(file);
		//得到一张画布,画布的大小和原图一样大
		BufferedImage bufimg=new BufferedImage(img.getWidth(null),img.getHeight(null),BufferedImage.TYPE_INT_RGB);
		//取得在该画布上操作的权利
		Graphics2D g=bufimg.createGraphics();
		//把原图画在该画布上
		g.drawImage(img, 0, 0,img.getWidth(null), img.getHeight(null), null);
		//在上面再画一个字符串,位置是50,50
		g.drawString("aaa",50,40);
		//g的作用完了,收回
		g.dispose();
		//把咱么画的图输出到外面去
		ImageIO.write(bufimg, "jpg", new File("d:hh/b.jpg"));
	}
	/**
	 * 
	 * @param args
	 * @throws IOException
	 * 				main2可以单独运行的
	 */
	public static void main2(String[] args) throws IOException {
		File file=new File("d:/hh/a.jpg");
		//得到原图
		Image img=ImageIO.read(file);
		//取得在原图上操作的权利
		Graphics g= img.getGraphics();
		//获取字体,设置字体
		Font font=getfont(new File("d:/hh/t.ttf"), Font.BOLD, 80);
		System.out.println(font.getFontName());
		g.setFont(font);
		//设置颜色
		g.setColor(Color.red);
		//设置内容
		g.drawString("it's over",500,400);
		//g的作用完了,收回
		g.dispose();
		//把咱么画的图输出到外面去
		ImageIO.write((RenderedImage) img, "jpg", new File("d:hh/c.jpg"));
	}
	/**
	 * 
	 * @param args
	 * @throws FontFormatException
	 * @throws IOException
	 * 			main3是带着封装属性
	 */
	public static void main(String[] args) throws FontFormatException, IOException {
		File file=new File("d:/hh/a.jpg");
		getcanvas(file);
	}
	/**
	 * 
	 * @param file
	 * 			类似注解,请看main2
	 */
	public static void getcanvas(File file) {
		try {
			Image img=ImageIO.read(file);
			int width=img.getWidth(null);
			int height=img.getHeight(null);
			BufferedImage bufimg=new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
			Graphics2D g=bufimg.createGraphics();
			g.drawImage(img, 0, 0, width, height, null);
			Font font=getfont(new File("d:/hh/t.ttf"), Font.BOLD, 80);
			System.out.println(font.getFontName());
			g.setFont(font);			
			g.drawString("it's over", 60, 60);
			g.dispose();
			ImageIO.write(bufimg, "jpg", new File("d:/hh/b.jpg"));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	/**
	 * 
	 * @param file
	 * @param style
	 * @param size
	 * @return
	 * 				设置字体方法
	 */
	public static Font getfont(File file,int style,int size){
		Font font=null;
		Font font2=null;
		try {
			font=Font.createFont(Font.TRUETYPE_FONT, file);
			System.out.println(font.getName());
			//这一步很关键
			font2=font.deriveFont(style, size);
			System.out.println(font2.getFontName());
		} catch (Exception e) {
			e.printStackTrace();
		} 
		return font2;
	}



}

猜你喜欢

转载自blog.csdn.net/weixin_42321963/article/details/81324123