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} /* ...
随机推荐
- C#有意思的算法题
年底了,特贡献一些C#有意思的算法题 2013年,即将要过去了.屌丝C#程序员们拿到了年终奖不?是不是又想蠢蠢欲动了?是不是想通过跳槽来为自己实现加薪的梦想?好吧,跳槽之前还是做点准备吧,准备好C ...
- 设计适用于SOA的类型(二)
接着上次的,以扩展String类型为例,继续根据自己的使用习惯闭门造车! 这一次我重载了运算符 = 和 + ,我觉得这样更方便我的使用.首先先贴代码: /// <summary> // ...
- [转]Disabling ASLR on individual iOS applications when using iOS 6.0.1
ASLR: Address Space Layout Randomization 查看应用是否进行了 ASLR 保护的方法:otool -hv ${File-Path} I recently enco ...
- ubuntu下使用golang、qml与ubuntu sdk开发桌面应用
ubuntu下使用golang.qml与ubuntu sdk开发桌面应用 (简单示例) 找了很长时间go的gui库,试了gtk,准备试qt的时候发现了这个qml库,试了下很好用. 准备工作 1.Go ...
- MyEclipse取消Show in Breadcrumb的方法
eclipse中的Show in Breadcrumb是快速导航条,可以清晰的看到我们当前的类,属性或方法的导航 定位. 如果不喜欢的话,取消掉的方法如下: Window -> Customiz ...
- 权限系统设计实现MVC4 + WebAPI + EasyUI + Knouckout
权限系统设计实现MVC4 + WebAPI + EasyUI + Knouckout (一) 一.前言 之前的博客一直都还没写到框架的实现及权限系统,今天开始写我的权限系统,我以前做过的项目基本上都有 ...
- 初识ionic
1.Ionic环境安装 Ionic开发是依赖于Nodejs环境的,所以在开发之前我们需要安装好Nodejs.下载安装:http://nodejs.org/ 安装完成之后打开PowerShell输入命令 ...
- Python爬虫小白入门(六)爬取披头士乐队历年专辑封面-网易云音乐
一.前言 前文说过我的设计师小伙伴的设计需求,他想做一个披头士乐队历年专辑的瀑布图. 通过搜索,发现网易云音乐上有比较全的历年专辑信息加配图,图片质量还可以,虽然有大有小. 我的例子怎么都是爬取图片? ...
- Android自定义控件系列(二)—icon+文字的多种效果实现
转载请注明出处:http://www.cnblogs.com/landptf/p/6290810.html 今天给大家带来一个很简单但是很常用的控件ButtonExtendM,在开发中我们经常会用到图 ...
- HTML5的文档结构
HTML5的文档结构 HTML5简化了许多,它的设计遵循了3个原则:1.兼容性.2.实用性.3.通用访问性 1. header 元素 <header> 标签定义文档或者文档 ...