java 向图片添加文字或logo

向图片上添加图片或文字

最近做一个根据相关信息在服务器生成一个证书的功能,在网上也没用找到封装好的工具,就自己写了一个,在服务器里使用

下面是简单封装的,水平有限,欢迎指正



/**
 * 图片工具类
 */
public class ImageAddText {

//	public static void main(String[] args) {
//		Font font = new Font("黑体", Font.PLAIN, 25);
//		Color color_l = Color.BLACK;
//		List<ImageAddTextOrLogoBean> beans = new ArrayList<ImageAddTextOrLogoBean>();
//
//		beans.add(new ImageAddTextOrLogoBean(Position_X.CONTER, Position_Y.TOP, 1, 28, "企业保荐单", color_l,
//				new Font("新宋体", Font.BOLD, 75)));
//
//		beans.add(new ImageAddTextOrLogoBean(Position_X.LEFT, Position_Y.TOP, left1, 
//		int a = ImageAddText.ImageAddTextOrImage("e:/bg.png", "e:/1111.jpg", beans);
//		System.out.println(a);

	}

	/**
	 * 
	 * @param source//原图片路径
	 * @param output//新图片路径
	 * @param beans
	 * @return 0 完成 1图片不存在 2异常错误
	 */
	public static int ImageAddTextOrImage(String source, String output, List<ImageAddTextOrLogoBean> beans) {

		try {
			File sourceFile = new File(source);
			if (!sourceFile.exists()) {
				return 1;
			}
			for (int i = 0; i < beans.size(); i++) {
				if (i == 0) {
					if (beans.get(i).getState() == 0) {//添加图片
						imageAddImage(beans.get(i), source, output);
					} else if (beans.get(i).getState() == 1) {//添加文字
						imageAddText(beans.get(i), source, output);
					}
				} else {
					if (beans.get(i).getState() == 0) {//添加图片
						imageAddImage(beans.get(i), output, output);
					} else if (beans.get(i).getState() == 1) {//添加文字
						imageAddText(beans.get(i), output, output);
					}
				}

			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return 2;
		}

		return 0;
	}

	/**
	 * 添加图片
	 * 
	 * @param iBean  要添加的图片的信息
	 * @param source 源图片路径
	 * @param output 新图片路径
	 * @throws Exception
	 */
	private static void imageAddImage(ImageAddTextOrLogoBean iBean, String source, String output) throws Exception {
		// TODO Auto-generated method stub
//		System.out.println("添加图片-" + iBean.getImageUrl());
		File file = new File(source);
		Image img = ImageIO.read(file);
		int srcImgWidth = img.getWidth(null);//水印宽度
		int srcImgHeight = img.getHeight(null);//水印高
		BufferedImage bi = new BufferedImage(srcImgWidth, srcImgHeight, BufferedImage.TYPE_INT_RGB);
		Graphics2D g = bi.createGraphics();
		g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
		g.drawImage(img.getScaledInstance(srcImgWidth, srcImgHeight, Image.SCALE_SMOOTH), 0, 0, null);

		ImageIcon imgIcon = new ImageIcon(iBean.getImageUrl());
		Image con = imgIcon.getImage();

		if (iBean.getToMinSize() != 1) {
			BufferedImage bufImg = ImageIO.read(new File(iBean.getImageUrl())); //读取图片
			con = bufImg.getScaledInstance((int) (con.getWidth(null) * iBean.getToMinSize()),
					(int) (con.getHeight(null) * iBean.getToMinSize()), bufImg.SCALE_SMOOTH);//设置缩放目标图片模板

		}

		//		ImageIcon imgIcon = new ImageIcon(iBean.getImageUrl());
		//		Image con = imgIcon.getImage();
		int x = setPoitionX(iBean, srcImgWidth, g, con);
		int y = setPoitionY(iBean, srcImgHeight, g, con);

		g.drawImage(con, x, y, null);//水印的位置
		g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
		g.dispose();
		File sf = new File(output);
		ImageIO.write(bi, "jpg", sf); // 保存图片
//		System.out.println("完成");
		//		return result;

	}

	/**
	 * 添加文字
	 * 
	 * @param iBean  文字信息
	 * @param source 原图片路径
	 * @param output 新图片路径
	 * @throws Exception
	 */
	private static void imageAddText(ImageAddTextOrLogoBean iBean, String source, String output) throws Exception {
		// TODO Auto-generated method stub
//		System.out.println("添加文字-" + iBean.getWatermark());
		File srcImgFile = new File(source);
		Image srcImg = ImageIO.read(srcImgFile);
		int srcImgWidth = srcImg.getWidth(null);
		int srcImgHeight = srcImg.getHeight(null);
		BufferedImage bufImg = new BufferedImage(srcImgWidth, srcImgHeight, BufferedImage.TYPE_INT_RGB);
		Graphics2D g = bufImg.createGraphics();
		g.drawImage(srcImg, 0, 0, srcImgWidth, srcImgHeight, null);
		if (iBean.getColor() != null) {
			g.setColor(iBean.getColor());
		}

		if (iBean.getFont() != null) {
			g.setFont(iBean.getFont());
		}
		//设置水印的坐标
		int x = setPoitionX(iBean, srcImgWidth, g, null);
		int y = setPoitionY(iBean, srcImgHeight, g, null);

		g.drawString(iBean.getWatermark(), x, y); //加水印
		g.dispose();
		// 输出图片  
		File sf = new File(output);
		ImageIO.write(bufImg, "jpg", sf);
//		System.out.println("完成");

	}

	/**
	 * 计算x位置
	 * 
	 * @param iBean       要添加的内容的信息
	 * @param srcImgWidth 背景图片宽度
	 * @param g
	 * @param image
	 * @return
	 * @throws Exception
	 */
	private static int setPoitionX(ImageAddTextOrLogoBean iBean, int srcImgWidth, Graphics2D g, Image image)
			throws Exception {
		int x = 0;
		if (iBean.getPositon_X() == Position_X.LEFT) {
			if (iBean.getX() < 0) {
				x = 0;
			} else {

			}
			if (iBean.getX() > 100) {
				x = srcImgWidth;
			} else {
				x = (int) Math.round(srcImgWidth / 100.0 * iBean.getX());
			}

		} else if (iBean.getPositon_X() == Position_X.CONTER) {
			if (iBean.getState() == 0) {
				x = srcImgWidth / 2 - image.getWidth(null) / 2;
			} else if (iBean.getState() == 1) {
				x = srcImgWidth / 2 - (g.getFontMetrics(g.getFont()).charsWidth(iBean.getWatermark().toCharArray(), 0,
						iBean.getWatermark().length()) / 2);
			}

		} else if (iBean.getPositon_X() == Position_X.RIGHT) {
			if (iBean.getState() == 0) {
				x = (int) (srcImgWidth - image.getWidth(null) - Math.round(srcImgWidth / 100.0 * iBean.getX()));
			} else if (iBean.getState() == 1) {
				x = (int) (srcImgWidth - (g.getFontMetrics(g.getFont()).charsWidth(iBean.getWatermark().toCharArray(),
						0, iBean.getWatermark().length())) - Math.round(srcImgWidth / 100.0 * iBean.getX()));
			}
		}
		return x;
	}

	/**
	 * 计算y位置
	 * 
	 * @param iBean
	 * @param srcImgHeight //原图片高度
	 * @param g
	 * @param image
	 * @return
	 * @throws Exception
	 */
	private static int setPoitionY(ImageAddTextOrLogoBean iBean, int srcImgHeight, Graphics2D g, Image image)
			throws Exception {
		int y = 0;
		if (iBean.getPositon_Y() == Position_Y.TOP) {
			if (iBean.getY() < 0) {
				y = 0;
			} else if (iBean.getY() > 100) {
				y = srcImgHeight;
			} else {
				y = (int) Math.round(srcImgHeight / 100.0 * iBean.getY());
			}
		} else if (iBean.getPositon_Y() == Position_Y.CONTER) {

			if (iBean.getState() == 0) {
				y = srcImgHeight / 2 - image.getHeight(null) / 2;
			} else if (iBean.getState() == 1) {
				y = srcImgHeight / 2 - (g.getFontMetrics(g.getFont()).getHeight()) / 2;
			}

		} else if (iBean.getPositon_Y() == Position_Y.BOTTOM) {
			if (iBean.getState() == 0) {
				y = (int) (srcImgHeight - image.getHeight(null) - Math.round(srcImgHeight / 100.0 * iBean.getY()));
			} else if (iBean.getState() == 1) {
				y = (int) (srcImgHeight - (g.getFontMetrics(g.getFont()).getHeight())
						- Math.round(srcImgHeight / 100.0 * iBean.getY()));
			}

		}
		return y;
	}
}



import java.awt.Color;
import java.awt.Font;
/**
 *      在图片上添加文字或图片
 * @author Administrator
 *
 */
public class ImageAddTextOrLogoBean {

	public enum Position_X{
		LEFT,
		CONTER,
		RIGHT,
	}
	public enum Position_Y{
		TOP,
		CONTER,
		BOTTOM,
	}
	
	
	private Position_X positon_X = Position_X.LEFT;
	private Position_Y positon_Y = Position_Y.TOP;
	
	private Integer state;//0图片 1文字

	private int x;//x坐标
	private int y;//y坐标

	private String watermark;//文字内容
	private Color color;//颜色
	private Font font;//字体

	private String imageUrl;//图片路径

	private double toMinSize = 1;//缩小程度
	
	public ImageAddTextOrLogoBean() {

	}

	/**
	 * 
	 * @param x  x轴坐标距左侧的距离(百分比)
	 * @param y  y轴坐标距顶部的距离(百分比)
	 * @param watermark 内容
	 * @param color 颜色
	 * @param font 字体
	 */
	public ImageAddTextOrLogoBean(int x, int y, String watermark, Color color, Font font) {
		super();
		this.x = x;
		this.y = y;
		this.watermark = watermark;
		this.color = color;
		this.font = font;
		this.state = 1;
	}
	
	
/**
 * 可以设置xy方位的方法
 * @param positon_X
 * @param positon_Y
 * @param x
 * @param y
 * @param watermark
 * @param color
 * @param font
 */
	public ImageAddTextOrLogoBean(Position_X positon_X, Position_Y positon_Y, int x, int y,
			String watermark, Color color, Font font) {
		super();
		this.positon_X = positon_X;
		this.positon_Y = positon_Y;
		this.state = 1;
		this.x = x;
		this.y = y;
		this.watermark = watermark;
		this.color = color;
		this.font = font;
	}

	
	
	/**
	 * 可以设置xy方位   添加图片
	 * @param positon_X
	 * @param positon_Y
	 * @param x
	 * @param y
	 * @param imageUrl
	 */
	
	public ImageAddTextOrLogoBean(Position_X positon_X, Position_Y positon_Y, int x, int y,
			String imageUrl) {
		super();
		this.positon_X = positon_X;
		this.positon_Y = positon_Y;
		this.state = 0;
		this.x = x;
		this.y = y;
		this.imageUrl = imageUrl;
	}

	public ImageAddTextOrLogoBean(int x, int y, String imageUrl) {
		super();
		this.x = x;
		this.y = y;
		this.imageUrl = imageUrl;
		this.state = 0;
	}
	
	
	/**
	 *       可以缩放图片
	 * @param positon_X
	 * @param positon_Y
	 * @param x
	 * @param y
	 * @param imageUrl
	 * @param toMinSize  缩放程度,(1:大小不变)
	 */
	public ImageAddTextOrLogoBean(Position_X positon_X,Position_Y positon_Y,int x, int y, String imageUrl,Double toMinSize) {
		super();
		this.positon_X = positon_X;
		this.positon_Y = positon_Y;
		this.x = x;
		this.y = y;
		this.imageUrl = imageUrl;
		this.state = 0;
		this.toMinSize = toMinSize;
	}
	
	public Integer getState() {
		return state;
	}

	public void setState(Integer state) {
		this.state = state;
	}

	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}

	public String getWatermark() {
		return watermark;
	}

	public void setWatermark(String watermark) {
		this.watermark = watermark;
	}

	public Color getColor() {
		return color;
	}

	public void setColor(Color color) {
		this.color = color;
	}

	public Font getFont() {
		return font;
	}

	public void setFont(Font font) {
		this.font = font;
	}

	public String getImageUrl() {
		return imageUrl;
	}

	public void setImageUrl(String imageUrl) {
		this.imageUrl = imageUrl;
	}

	public Position_X getPositon_X() {
		return positon_X;
	}

	public void setPositon_X(Position_X positon_X) {
		this.positon_X = positon_X;
	}

	public Position_Y getPositon_Y() {
		return positon_Y;
	}

	public void setPositon_Y(Position_Y positon_Y) {
		this.positon_Y = positon_Y;
	}

	public double getToMinSize() {
		return toMinSize;
	}

	public void setToMinSize(double toMinSize) {
		this.toMinSize = toMinSize;
	}

	
}

文档为笔记使用,欢迎大佬指点,如有侵权,请联系删除,转载请标明出处

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值