java 图文生成图片_java生成图片

在一张图片上绘制别的图片以及文字。

public String GenerateImage(WebCast_baseinfo base,String code,String customName,String customImg,String customId){

Log.debug("------GenerateImage----start");

String ret = null;

try {

InputStream is = new FileInputStream("/usr/share/tomcat7/webapps/worklite/promotionImage/card.jpg");

JPEGImageDecoder jpegDecoder = JPEGCodec.createJPEGDecoder(is);

BufferedImage buffImg=null;

try{

buffImg = jpegDecoder.decodeAsBufferedImage();

}catch(Exception e){

Log.error(this.getClass().getSimpleName()+"----"+e.getMessage());

}

Log.debug("------GenerateImage----Graphics");

Graphics g = buffImg.getGraphics();

g.setColor(new Color(255,140,0));

double rate=1.0;

Font f = new Font("楷体",Font.ROMAN_BASELINE,30);

FontMetrics fm = sun.font.FontDesignMetrics.getMetrics(f);

g.setFont(f);

String str="";

if(base.getIntroduction()!=null){

str=base.getIntroduction();

}

if(str.length()>16){

String s=str.substring(0,16);

String ss=str.substring(16);

g.drawString(s,(640-fm.stringWidth(s))/2,326);

//MyDrawString(s,(640-fm.stringWidth(s))/2,326,rate,g);

g.drawString(ss,(640-fm.stringWidth(ss))/2,366);

//MyDrawString(ss,(640-fm.stringWidth(ss))/2,366,rate,g);

}else{

g.drawString(str,(640-fm.stringWidth(str))/2,366);

//MyDrawString(str,(640-fm.stringWidth(str))/2,326,rate,g);

}

Font f3 = new Font("楷体",Font.ROMAN_BASELINE,30);

FontMetrics fm3 = sun.font.FontDesignMetrics.getMetrics(f3);

g.setFont(f3);

g.setColor(Color.white);

String str2=base.getwName();

if(str2.length()>16){

String s2=str2.substring(0,16);

String ss2=str2.substring(16);

g.drawString(s2,(640-fm3.stringWidth(s2))/2,426);

g.drawString(ss2,(640-fm3.stringWidth(ss2))/2,466);

//MyDrawString(s2,(640-fm.stringWidth(s2))/2,426,rate,g);

//g.drawString(ss,(640-fm.stringWidth(ss))/2,366);

//MyDrawString(ss2,(640-fm.stringWidth(ss2))/2,466,rate,g);

}else{

g.drawString(str2,(640-fm3.stringWidth(str2))/2,446);

//MyDrawString(str2,(640-fm.stringWidth(str2))/2,446,rate,g);

}

g.setColor(Color.white);

String str3="主讲人:"+base.getSpeaker();

if(str2.length()>16){

g.drawString(str3,(640-fm3.stringWidth(str3))/2,526);

}else{

g.drawString(str3,(640-fm3.stringWidth(str3))/2,506);

}

g.setColor(Color.white);

String str4=DateUtil.toString(base.getStartTime(), "yyyy年MM月dd日 HH:mm")+" 开讲";

if(str2.length()>16){

g.drawString(str4,(640-fm3.stringWidth(str4))/2,586);

}else{

g.drawString(str4,(640-fm3.stringWidth(str4))/2,566);

}

ImageIcon imgIcon3 = new ImageIcon(new URL(customImg),"customImg");

Image img3 = imgIcon3.getImage();

g.drawImage(img3,(640-80)/2,700,80,80,null);

Font f1 = new Font("楷体",Font.BOLD,20);

FontMetrics fm1 = sun.font.FontDesignMetrics.getMetrics(f1);

Font f2 = new Font("楷体",Font.BOLD,24);

FontMetrics fm2 = sun.font.FontDesignMetrics.getMetrics(f2);

g.setFont(f1);

g.setColor(Color.white);

String str6=customName;

String str5="来自 ";

String str7=" 的邀请";

g.drawString(str5,(640-fm1.stringWidth(str5)-fm2.stringWidth(str6)-fm1.stringWidth(str7))/2,816);

//g.setColor(new Color(30,144,255));

g.setFont(f2);

g.drawString(str6,(640-fm1.stringWidth(str5)-fm2.stringWidth(str6)-fm1.stringWidth(str7))/2+fm1.stringWidth(str5),816);

//g.setColor(Color.gray);

g.setFont(f1);

g.drawString(str7,(640-fm1.stringWidth(str5)-fm2.stringWidth(str6)-fm1.stringWidth(str7))/2+fm1.stringWidth(str5)+fm2.stringWidth(str6),816);

ImageIcon imgIcon2 = new ImageIcon(new URL(code),"qrcode");

Image img2 = imgIcon2.getImage();

g.drawImage(img2,(640-185)/2,848,180,180,null);

g.dispose();

String fileName = base.getCompanyId()+"_"+customId+"_"+base.getwId()+".jpg";

LocalFileManager tmpfileMgr = new LocalFileManager();

File tmpFile = tmpfileMgr.getFile(FileConstants.TYPE_DATA, FileConstants.SUBTYPE_HB, fileName, true);

OutputStream os = new FileOutputStream(tmpFile);

JPEGImageEncoder en = JPEGCodec.createJPEGEncoder(os);

en.encode(buffImg);

is.close();

os.close();

IFileManager fileMgr = FileFactory.getFileManager();

fileMgr.moveTempFile(FileConstants.TYPE_DATA, FileConstants.SUBTYPE_HB, fileName, tmpFile);

ret = fileMgr.getHref(FileConstants.TYPE_DATA, FileConstants.SUBTYPE_HB, fileName);

Log.debug("------GenerateImage----end");

} catch (Exception e) {

Log.error(this.getClass().getSimpleName()+"----"+e.getMessage());

}

Log.debug("------GenerateImage---ret--"+ret);

return ret;

}

原文:http://www.cnblogs.com/momo1210/p/6283854.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值