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;
}
java生成图片的更多相关文章
- JAVA生成图片缩略图、JAVA截取图片局部内容
package com.ares.image.test; import java.awt.Color; import java.awt.Graphics; import java.awt.Image; ...
- 转:Java生成图片验证码(有点仿QQ验证码的意思)
http://blog.csdn.net/ruixue0117/article/details/22829557 java: VerifyCodeUtils.java package com.fro. ...
- 【转】Java生成图片验证码
原文转自:http://blog.csdn.net/ruixue0117/article/details/22829557 看了挺多图片验证码的代码,感觉没什么长的好看点的,就自己动手写了个,写完发现 ...
- java生成图片验证码(转)--封装生成图片验证码的工具类
博客部分内容转载自 LonlySnow的博客:后台java 实现验证码生成 1.controller方法 @RequestMapping(value = "/verifycode/img&q ...
- Java生成图片验证码
在日常我们在登录或者注册的时候,网页上会出现验证码让我们填写,其实利用jdk提供给我们的工具类完全可以模拟出来一个生成验证码图片的功能. package util; import javax.imag ...
- java.lang.NoClassDefFoundError: com/sun/image/codec/jpeg/JPEGCodec 解决方案
使用java生成图片的时候,报了java.lang.NoClassDefFoundError: com/sun/image/codec/jpeg/JPEGCodec 错误. 根据这个类的api说明,在 ...
- Linux下JDK中文字体乱码
java生成图片的时候用到字体,但是liunx系统没有这些字体需要把C:\Windows\Fonts 上传到/usr/local/jdk1.8.0_171/jre/lib/fonts 重启tomcat ...
- Centos7 使用 Ansible 批量安装中文字体
需求背景 Centos7 下 Java 生成图片水印时中文乱码,原因是没有安装中文字体. 安装中文字体 以下是基于 Centos7 手动安装中文字体的详细步骤.当测试或者生产环境服务器比较多的时候,建 ...
- Spark案例分析
一.需求:计算网页访问量前三名 import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext} /* ...
随机推荐
- web代理进行跨域访问
通过web代理进行跨域访问,http请求返回超时的问题定位 [现象] 在ajax通过web代理跨域访问时,http第一次登陆时正常,但是第二次再下发其他命令的时候总是返回 java.net.Soc ...
- 把《C语言接口与实现》读薄之第一章:引言
1.1文学程序 文学程序(literate program):接口及其实现的代码与对其进行解释的正文交织在一起.文学程序由英文正文和带标签的程序代码块组成.例如, 〈compute x * y〉≡ s ...
- twisted学习笔记 No.1
原创博文,转载请注明出处 . 1.安装twisted ,然后安装PyOpenSSL(一个Python开源OpenSSL库),这个软件包用于给Twisted提供加密传输支持(SSL).最后,安装PyCr ...
- Bootstrap3.0学习第八轮
Bootstrap3.0学习第八轮(工具Class) 前言 阅读之前您也可以到Bootstrap3.0入门学习系列导航中进行查看http://www.cnblogs.com/aehyok/p/34 ...
- CF 161D Distance in Tree【树DP】
题目大意:给一棵树,求树上两点之间距离为K的点对数目. 方程含义: dp(i,j)表示从已经遍历过的点到当前点i,路径长度为 j 的路径条数.因此,对于当前点,每当遍历了其中一个儿子节点的时候,首先统 ...
- 消除Switch...Case的过程
http://www.cnblogs.com/happyframework/p/3300170.html 目录 备注需求第一遍代码(重复的代码)第二遍代码(消除重复)备注 备注返回目录 不要重复自己, ...
- Pig性能优化
Pig性能优化 1. 尽早去除无用的数据 MapReduce Job的很大一部分开销在于磁盘IO和数据的网络传输,如果能尽早的去除无用的数据,减少数据量,会提升Pig的性能. 1). 尽早的使用Fil ...
- 分享Mvc3+NInject+EF+LigerUI权限系统Demo
前段时间时不时看到有园友的分享权限系统,于是本人突发奇想,也想写一个玩玩,就利用晚上时间,陆陆续续花了一周多样子,写了如今这个权限系统,这个权限系统具有 组织结构.用户.角色.菜单,组织结构下挂用户, ...
- 对dump脱壳的一点思考
对dump脱壳的一点思考 偶然翻了一下手机日历,原来今天是夏至啊,时间过的真快.ISCC的比赛已经持续了2个多月了,我也跟着比赛的那些题目学了2个月.......虽然过程很辛苦,但感觉还是很幸运的,能 ...
- js 获取页面内链接
今天有同学问如何用 JS 正则表达式获取一段文本中的超链接,并对超链接进行处理,想了几分钟,写了下面的代码: var re = /https?:\/\/[\w\.:~\-\d\/]+(?:\?[\w\ ...