合成图片
public static String exportImg(String path, String templateName, BigOrderRep bigOrderRep) {
long s = System.currentTimeMillis();
String imageName = UUID.randomUUID().toString();
try {
String managecom = bigOrderRep.getManagecom();//机构
String agentName = bigOrderRep.getAgentName();//代理人姓名
String InsuranceType = bigOrderRep.getInsuranceType();//险种
String amount = bigOrderRep.getAmount();//金额
String date = bigOrderRep.getDate();//日期
DateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
//1.jpg主图片的路径
InputStream is = new FileInputStream(path + templateName);
//通过JPEG图象流创建JPEG数据流解码器
//JPEGImageDecoder jpegDecoder = JPEGCodec.createJPEGDecoder(is);
//解码当前JPEG数据流,返回BufferedImage对象
//BufferedImage buffImg = jpegDecoder.decodeAsBufferedImage();
BufferedImage buffImg = ImageIO.read(is);
//得到画笔对象
Graphics g = buffImg.getGraphics();
//最后一个参数用来设置字体的大小
//Font f = new Font("宋体", Font.BOLD, 15);
//机构+代理人姓名
Font font = JFreeChartUtil.getDefinedFontByPath(98, "/MFLiHei.TTF", Font.TRUETYPE_FONT);
Color mycolor = new Color(245, 245, 245);
g.setColor(mycolor);
//表示这段文字在图片上的位置(x,y) .第一个是你设置的内容。
g.setFont(font);
g.drawString(managecom + agentName, 750 - g.getFontMetrics(font).stringWidth(managecom + agentName) / 2, 880);
//险种
font = JFreeChartUtil.getDefinedFontByPath(85, "/MFLiHei.TTF", Font.TRUETYPE_FONT);
mycolor = new Color(171, 0, 0);
g.setFont(font);
g.setColor(mycolor);
int length = g.getFontMetrics(font).stringWidth(InsuranceType);
if (length > 1030) {
g.drawString(InsuranceType.substring(0,InsuranceType.length()/2), 750 - g.getFontMetrics(font).stringWidth(InsuranceType.substring(0,InsuranceType.length()/2)) / 2, 1200);
g.drawString(InsuranceType.substring(InsuranceType.length()/2,InsuranceType.length()), 750 - g.getFontMetrics(font).stringWidth(InsuranceType.substring(InsuranceType.length()/2,InsuranceType.length())) / 2, 1320);
} else {
g.drawString(InsuranceType, 750 - g.getFontMetrics(font).stringWidth(InsuranceType) / 2, 1270);
}
//金额
font = JFreeChartUtil.getDefinedFont(150);
mycolor = new Color(215, 183, 106);
g.setFont(font);
g.setColor(mycolor);
g.drawString(amount, 840 - g.getFontMetrics(font).stringWidth(amount) / 2, 1600);
//日期
font = JFreeChartUtil.getDefinedFontByPath(45, "/FZLTCHJW.TTF", Font.PLAIN);
//font = new Font("方正小标宋",Font.PLAIN,45);
mycolor = new Color(255, 255, 231);
g.setFont(font);
g.setColor(mycolor);
g.drawString(date, 1225 - g.getFontMetrics(font).stringWidth(date) / 2, 1900);
g.dispose();
OutputStream os;
JFreeChartUtil.isChartPathExist(path + bigOrderRep.getDate() + "http://www.likecs.com/");
os = new FileOutputStream(path + bigOrderRep.getDate() + "http://www.likecs.com/" + imageName + ".jpg");
//创键编码器,用于编码内存中的图象数据。
ImageIO.write(buffImg, "jpeg", os);
is.close();
os.close();
} catch (Exception e) {
log.error("生成图片错误", e);
return imageName;
}
long e = System.currentTimeMillis();
log.info("生成一张图片共耗时:" + (e - s) / 1000.0 + "s");
return imageName;
}
读取字体包
public static Font getDefinedFontByPath(float fs,String fontPath,int style) {
Font Font = null;
log.info("Path:{}", fontPath);
if (Font == null) {
InputStream is = null;
BufferedInputStream bis = null;
try {
is = new FileInputStream(new File(fontPath));
bis = new BufferedInputStream(is);
Font = Font.createFont(style, is);
// 设置字体大小,float型
Font = Font.deriveFont(fs);
} catch (FontFormatException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (null != bis) {
bis.close();
}
if (null != is) {
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
return Font;
}
JAVVA后台文字、图片合并
内容版权声明:除非注明,否则皆为本站原创文章。