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

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. 新认识:SDF数据库

    新认识:SDF数据库 一.SDF数据库初探 SDF是一个标准缩略数据库格式.这个数据库包含扩展名为.sdf的文件并且以结构化文件格式进行数据存储.这些SDF文件通常用于在不同数据库应用之间移动数据.它 ...

  2. CSS选择器从右向左的匹配规则

    CSS选择器从右向左的匹配规则 下面这个栗子,CSS选择器它是如何工作的? .mod-nav h3 span {font-size: 16px;} 如果不知道匹配规则,可能的理解是从左向右匹配:先找到 ...

  3. MongoDB学习(翻译5)

    C#驱动序列化文档对象 介绍 本文档基于C#官方驱动1.8版本. 本节C#驱动教程谈论C#类到BSON对象的序列化和反序列化.序列化是映射一个对象到可保存到MongoDB库中BSON对象的过程,反序列 ...

  4. 基于Hadoop开发网络云盘系统客户端界面设计初稿

    基于Hadoop开发网络云盘系统客户端界面设计初稿 前言: 本文是<基于Hadoop开发网络云盘系统架构设计方案>的第二篇,针对界面原型原本考虑有两个方案:1.类windows模式,文件夹 ...

  5. Exchange 2016 创建日程,远程服务器返回错误: (401) 未经授权

    背景:在测试环境中一切正常,使用的是Exchange2013,上生产环境Exchange2016,部署后产生如下错误: Microsoft.Exchange.WebServices.Data.Serv ...

  6. MySQL中函数、游标、事件、视图

    MySQL中函数.游标.事件.视图基本应用举例(代码) MySQL中function用户自定义函数c,fun,fun是面向过程的实现方式只能传入参数,或不传入参数,不能传出参数,必有返回值函数中是不能 ...

  7. ssd可以用作redo 盘吗?

                                               ssd可以用作redo 盘吗? 1.ssd有写磨损,而且ssd的写性能也不是非常好,ssd只是随机读特别好,因为 ...

  8. sharepoint 2013 文档库eventhandle权限控制

    记录一下如何在sharepoint server 2013文档库中,使用eventhandle控制文档库document library的条目item权限. ///<summary> // ...

  9. about flashback_transaction_query

    详见原文博客链接地址:about flashback_transaction_query

  10. Cocos2d-精灵的几个常识

    性能考虑 该部分是总结的cocos2d的在线文档 1)如果有每个帧有25个以下的精灵需要更新,可以直接使用精灵 class TLayer(cocos.layer.Layer):     is_even ...