1. 程式人生 > 其它 >JAVVA後臺文字、圖片合併

JAVVA後臺文字、圖片合併


合成圖片

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() + "/");
os = new FileOutputStream(path + bigOrderRep.getDate() + "/" + 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;
}