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} /* ...
随机推荐
- iOS基础 - Quartz 2D绘图的基本步骤
一.使用Quartz 2D绘图的基本步骤 1) 获取上下文context(绘制图形的地方) 2) 设置路径(路径是用来描述形状的) 3) 将路径添加到上下文 4) 设置上下文属性(设置颜色,线宽, ...
- 推荐系列:最小与最大[DP+余式定理]
最小与最大 [问题描述] 做过了乘积最大这道题,相信这道题也难不倒你. 已知一个数串,可以在适当的位置加入乘号(设加了k个,当然也可不加,即分成k+1个部分),设这k+1个部分的乘积(如果k=0,则乘 ...
- linux不同环境下c/c++程序移植方法
这边之前的大多数项目都用的java,而自己用的c++,等到快要上线的时候才发现线上机器的gcc和libc的版本都巨低,跟自己测试开发用的环境不兼容,编译出的c++可执行文件没法运行.解决c++程序的移 ...
- JavaWEB开发国际化
1.国际化开发概述 )软件的国际化:软件开发时,要使它能同时应对世界不同地区和国家的访问,并针对不同地区和国家的访问,提供相应的.符合来访者阅读习惯的页面或数据. )国际化又称为 i18n:inter ...
- 记录下自己写的gulp打包脚本
var c = { rootPath: 'src',//项目文件夹 outputPath: 'output',//文件编译后的输出目录 revPath: 'manifest',//rev映射文件目录 ...
- PyQt设计流程
Qt designer 设计流程: 创建一个 PyQt4 的步骤,通常的方法是用 QtDesigner 工具创建 GUI 界面.可以在窗口 上添加部件,并可以对部件进行一些属性配置.一般的过程如下 ...
- web系统数据导出功能设计实现(导出excel2003/2007 word pdf zip等)
web系统数据导出功能设计实现(导出excel2003/2007 word pdf zip等) 前言 我们在做web系统中,导出也是很常用的一个功能,如果每一个数据列表都要对应写一个导出的方法不太现实 ...
- java 逆波兰表达式
最近想把这破机 装成WIN7 想想还是算了 ... 反正用的机会也不多. 不过 发现了一些 想念的东西 从前的作业. 从前的记忆. package org.lmz; import java.util ...
- Android开发(24)---安卓中实现多线程下载(带进度条和百分比)
当我们学完java中多线程的下载后,可以将它移植到我们的安卓中来,下面是具体实现源码: DownActivity.java package com.example.downloads; import ...
- oracle-计算工作日
数据库模拟表如下 operate_id operate_type operate_date process_sn 1 GD 2013-09-15 17:18:37 10001 2 JD 2013-09 ...