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

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;

}

java生成图片的更多相关文章

  1. JAVA生成图片缩略图、JAVA截取图片局部内容

    package com.ares.image.test; import java.awt.Color; import java.awt.Graphics; import java.awt.Image; ...

  2. 转:Java生成图片验证码(有点仿QQ验证码的意思)

    http://blog.csdn.net/ruixue0117/article/details/22829557 java: VerifyCodeUtils.java package com.fro. ...

  3. 【转】Java生成图片验证码

    原文转自:http://blog.csdn.net/ruixue0117/article/details/22829557 看了挺多图片验证码的代码,感觉没什么长的好看点的,就自己动手写了个,写完发现 ...

  4. java生成图片验证码(转)--封装生成图片验证码的工具类

    博客部分内容转载自 LonlySnow的博客:后台java 实现验证码生成 1.controller方法 @RequestMapping(value = "/verifycode/img&q ...

  5. Java生成图片验证码

    在日常我们在登录或者注册的时候,网页上会出现验证码让我们填写,其实利用jdk提供给我们的工具类完全可以模拟出来一个生成验证码图片的功能. package util; import javax.imag ...

  6. java.lang.NoClassDefFoundError: com/sun/image/codec/jpeg/JPEGCodec 解决方案

    使用java生成图片的时候,报了java.lang.NoClassDefFoundError: com/sun/image/codec/jpeg/JPEGCodec 错误. 根据这个类的api说明,在 ...

  7. Linux下JDK中文字体乱码

    java生成图片的时候用到字体,但是liunx系统没有这些字体需要把C:\Windows\Fonts 上传到/usr/local/jdk1.8.0_171/jre/lib/fonts 重启tomcat ...

  8. Centos7 使用 Ansible 批量安装中文字体

    需求背景 Centos7 下 Java 生成图片水印时中文乱码,原因是没有安装中文字体. 安装中文字体 以下是基于 Centos7 手动安装中文字体的详细步骤.当测试或者生产环境服务器比较多的时候,建 ...

  9. Spark案例分析

    一.需求:计算网页访问量前三名 import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext} /* ...

随机推荐

  1. iOS基础 - CALayer

    一.CALayer简介 Core Animation是跨平台的,支持iOS环境和Mac OS X环境 凡是支持跨平台的框架,都不能直接使用UIKit框架,因为UIKit框架只能应用在iOS而不能用于M ...

  2. 在一般处理文件中访问Session需要添加IRequiresSessionState

    在IHttpHandler 使用Session 通常我们经常,通过session判定用户是否登录.还有一些临时的.重要的数据也尝尝存放在Session中. 在页面我们很容易的得到Session的值,但 ...

  3. Coursera台大机器学习基础课程1

    Coursera台大机器学习基础课程学习笔记 -- 1 最近在跟台大的这个课程,觉得不错,想把学习笔记发出来跟大家分享下,有错误希望大家指正. 一 机器学习是什么? 感觉和 Tom M. Mitche ...

  4. 什么是Angular JS?

    Angular JS学习笔记——什么是Angular JS? Angular JS是一个由Google维护的开源的Javascript框架,主要作者为: Misko Hevery(angular JS ...

  5. FineUI开源版之TreeGrid(修改)

    上篇文章中做了简单实现,但是还是有bug的,还需要在外面写事件的处理,今天又进行修改了. 下面放出代码,同样的  hzh modify标记的就是我进行修改的地方 grid.cs 添加代码 #regio ...

  6. Sublime和Codeblocks支持C++11

    Sublime和Codeblocks支持C++11 闲来没事看了一下C++11,比起C++0x多了很多新功能,像auto变量,智能指针等,g++4.7以上版本也提供了对C++11的支持,但是,如何在你 ...

  7. 【转】title与alt的区别

    html中的title属性和alt属性让人有些混淆. 以前不知道有title这个属性,第一次用到它时,就和alt产生了混淆.一位朋友告诉我说,alt是图片img标签里用的,title是超链接里用的,当 ...

  8. 基于easyui的webform扩展(续)

    基于easyui的webform扩展(续) 回顾 <前端基于easyui的mvc扩展>.<前端基于easyui的mvc扩展(续)>.<基于easyui的webform扩展 ...

  9. SystemTray文字颜色问题

    今天想给SystemTray的ForegroundColor设置为白色,但是模拟器正确,真机仍为黑色.经过一番折腾,发现是微软做了限制,背景是什么颜色,ForegroundColor就不能为什么颜色. ...

  10. iOS核心应用对象

    IOS应用之设计模式:模型-视图-控制器 iOS应用与其它应用的区别就在于它所管理的数据(和相应的业务逻辑)以及将数据展现给用户的方式.大多数UIKit对象并不定义应用而是帮助完善其行为.例如,你的应 ...