Java套版生成动态图片

Java套版生成动态图片 , 适用于动态的名片生成 , 证件生成等

制作的图片底版  

 

生成后的图片效果

 

 

package com.ssmf;

/**
 * 套版生成实体类
 * @类 名 : CreateImageBean
 * @功能描述 : TODO
 * @作者信息 : CBC
 * @修改备注 :
 */
public class CreateImageBean {
	
	private Integer fontSize = null;// type为string的时候,为自定义文本内容
	
	private String type = "string";// string||image
	
	private String content = "";// type为image的时候,为本地图片路径,type为string的时候,为文本内容
	
	private Integer height = 0;// 高度 (type为image的时候,为图片的高度)
	
	private Integer width = 0; // 宽度 (type为image的时候,为图片的宽度,type为string的时候,为文本范围的宽度并计算居中)
	
	private Integer x = 0;// 距离左部的像素
	
	private Integer y = 0;// 距离顶部的像素
	
	public CreateImageBean(String type, String content, Integer x, Integer y, Integer height,
			Integer width, Integer fontSize) {
		super();
		this.type = type;
		this.content = content;
		this.height = height;
		this.width = width;
		this.x = x;
		this.y = y;
		this.fontSize = fontSize;
	}
	
	public CreateImageBean(String type, String content, Integer x, Integer y, Integer height,
			Integer width) {
		super();
		this.type = type;
		this.content = content;
		this.height = height;
		this.width = width;
		this.x = x;
		this.y = y;
	}
	
	public Integer getFontSize() {
		return fontSize;
	}

	public void setFontSize(Integer fontSize) {
		this.fontSize = fontSize;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public Integer getHeight() {
		return height;
	}

	public void setHeight(Integer height) {
		this.height = height;
	}

	public Integer getWidth() {
		return width;
	}

	public void setWidth(Integer width) {
		this.width = width;
	}

	public Integer getX() {
		return x;
	}

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

	public Integer getY() {
		return y;
	}

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








package com.ssmf;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import javax.imageio.ImageIO;

/**
 * 生成动态图片
 * @类 名 : CreateImage
 * @功能描述 : TODO
 * @作者信息 : CBC
 * @创建时间 : 2017-9-10上午12:21:09
 * @修改备注 :
 */
public class CreateImage {
	
	/** 模板图片路径 */
	private static String TEMPLATEPAHT = "";
	
	/** 模板印章路径 */
	private static String TEMPLATEPATHYZ = "";
	
	static {
		TEMPLATEPAHT = "D:\\准考证.png";
		TEMPLATEPATHYZ = "D:\\准考证签发单位章.png";
	}
	
    private static int getCount(String content, int fontSize) {
    	char[] c = content.toCharArray();
    	double count = 0;
    	for(int i = 0; i < c.length; i ++) {
			String len = Integer.toBinaryString(c[i]);
			if(len.length() > 8) {
				count++;
			}else{
				count = (count + 0.5);
			}
    	}
    	return (int) (fontSize * count);
    }
	
	public static void main(String[] args) {
		Map<String, CreateImageBean> map = new HashMap<String, CreateImageBean>();
		map.put("标题", new CreateImageBean("string", "XXX培训准考证", 32, 95, null, 1126, 40));
		map.put("报名地市", new CreateImageBean("string", "XXX5224", 210, 175, null, null));
		map.put("考生姓名", new CreateImageBean("string", "张三丰", 210, 250, null, null));
		map.put("性别", new CreateImageBean("string", "男", 210, 329, null, null));
		map.put("报考级别", new CreateImageBean("string", "考全级", 210, 402, null, null));
		map.put("工作单位", new CreateImageBean("string", "XXXXX市XXX县", 210, 480, null, null));
		map.put("考点地址", new CreateImageBean("string", "XXXXXXX", 210, 558, null, null));
		
		map.put("报名序号", new CreateImageBean("string", "XXXX0220", 693, 175, null, null));
		map.put("证件号码", new CreateImageBean("string", "XXXXXXXXXXXXXXXXXX", 693, 250, null, null));
		map.put("档案号", new CreateImageBean("string", "NHX0025666325", 693, 329, null, null));
		map.put("报考专业", new CreateImageBean("string", "建筑施工安全", 693, 402, null, null));

		map.put("人员头像", new CreateImageBean("image", "D:\\头像照片.jpg", 971, 133, 184, 220));
		
		map.put("考试科目", new CreateImageBean("string", "全科的考试", 34, 710, null, 347));
		map.put("准考证号", new CreateImageBean("string", "NHXAJ022365", 381, 710, null, 292));
		map.put("考试时间", new CreateImageBean("string", "2021年4月25日13点", 678, 710, null, 273));
		map.put("考场", new CreateImageBean("string", "1", 954, 710, null, 106));
		map.put("座位号", new CreateImageBean("string", "96", 1063, 710, null, 94));
		
		map.put("盖章", new CreateImageBean("image", TEMPLATEPATHYZ, 855, 280, 350, 350));
		createImage(map, "D:\\输出.png", Color.black, 25, true, "宋体");
	}
	
	/**
	 * @Title : createImage
	 * @功能描述 : TODO
	 * @设定文件 : @param map 需要填充的数据集合
	 * @设定文件 : @param path 文件输出的路径
	 * @设定文件 : @return
	 * @返回类型 : String 文件路径
	 * @throws :
	 */
	public static String createImage(Map<String, CreateImageBean> map, String path, Color color, Integer fontSize, Boolean fontBold, String typeface) {
		String filepath = null;
		try{
			// 加载模板图片
			BufferedImage image = ImageIO.read(new File(TEMPLATEPAHT));
		    // 得到图片操作对象
		    Graphics graphics = image.getGraphics();  
		    // 设置文字的颜色为黑色
		    graphics.setColor(color); 
		    // 设置文字的字体,大小
		    graphics.setFont(new Font(typeface, (fontBold ? Font.BOLD : null), fontSize));
		    for(Entry<String, CreateImageBean> bean : map.entrySet()) {
		    	CreateImageBean b = bean.getValue();
		    	if("string".equals(b.getType())) {
		    		// 自定义字体大小
		    		if(b.getFontSize() != null) {
		    			graphics.setFont(new Font(typeface, (fontBold ? Font.BOLD : null), b.getFontSize()));
		    		}
		    		if(b.getWidth() != null) {
		    			int count = getCount(b.getContent(), (b.getFontSize() == null ? fontSize : b.getFontSize()));
		    			int startX = ((b.getWidth()) - count) / 2;
		    			// 将文字放入模板中
			    		graphics.drawString(b.getContent(), startX +  b.getX(), b.getY()); 
		    		}else{
		    			// 将文字放入模板中
			    		graphics.drawString(b.getContent(), b.getX(), b.getY()); 
		    		}
		    		if(b.getFontSize() != null) {
		    			// 将字体大小切换回默认
					    graphics.setFont(new Font(typeface, (fontBold ? Font.BOLD : null), fontSize));
		    		}
		    	}
		    	if("image".equals(b.getType())) {
	    			File file = new File(b.getContent());
	    			// 对头像进行裁剪
	    		 	Image img = thumbnail(file, b.getHeight(), b.getWidth());
		 		    // 将头像放入模板中
		 		    graphics.drawImage(img, b.getX(), b.getY(), null);
		    	}
		    }
		    // 将图片存储到本地
		    createImage(path, image);  
	   }catch (Exception e) {
		   e.printStackTrace();
	   }
	   return filepath;
	}
	
	/**
	 * 将image对象存储到本地
	 * @Title : createImage
	 * @功能描述 : TODO
	 * @设定文件 : @param fileLocation 本地路径
	 * @设定文件 : @param image 图片对象
	 * @返回类型 : void
	 * @throws :
	 */
	private static void createImage(String fileLocation, BufferedImage image) {  
	    try {  
	      String formatName = fileLocation.substring(fileLocation.lastIndexOf(".") + 1);
	      ImageIO.write(image, formatName , new File(fileLocation));
	    } catch (Exception e) {  
	      e.printStackTrace();  
	    }  
	}  

	/**
	 * 图片压缩-按照固定宽高原图压缩
	 * @Title : thumbnail
	 * @功能描述 : TODO
	 * @设定文件 : @param img 本地图片地址
	 * @设定文件 : @param width 图片宽度
	 * @设定文件 : @param height 图片高度
	 * @设定文件 : @return
	 * @设定文件 : @throws IOException
	 * @返回类型 : Image
	 * @throws :
	 */
	public static Image thumbnail(File img, int width, int height) throws IOException {
	    BufferedImage BI = ImageIO.read(img);
	    Image image = BI.getScaledInstance(width, height, Image.SCALE_SMOOTH);
	    BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
	    Graphics g = tag.getGraphics();
	    g.setColor(Color.RED);
	    g.drawImage(image, 0, 0, null);
	    g.dispose();
	    return image;
	}

}








 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值