java根据图片和文字生成自定义图片
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException; import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder; public class ChartGraphics {
private BufferedImage image;
private int imageWidth = 300; //图片的宽度
private int imageHeight = 500; //图片的高度
//生成图片文件
@SuppressWarnings("restriction")
public void createImage(String fileLocation) {
BufferedOutputStream bos = null;
if(image != null){
try {
FileOutputStream fos = new FileOutputStream(fileLocation);
bos = new BufferedOutputStream(fos); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos);
encoder.encode(image);
bos.close();
} catch (Exception e) {
e.printStackTrace();
}finally{
if(bos!=null){//关闭输出流
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
} public void graphicsGeneration(String name, String id, String classname, String imgurl) {
int H_title = 30; //头部高度
int H_mainPic = 200; //轮播广告高度
int H_tip = 60; //上网提示框高度
int H_btn = 25; //按钮栏的高度
int tip_2_top = (H_title+H_mainPic);
int btns_2_top = tip_2_top+H_tip+20;
int btn1_2_top = btns_2_top+10;
int btn2_2_top = btn1_2_top+H_btn;
int shops_2_top = btn2_2_top+H_btn+20;
int W_btn = 280; //按钮栏的宽度 image = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB);
//设置图片的背景色
Graphics2D main = image.createGraphics();
main.setColor(Color.white);
main.fillRect(0, 0, imageWidth, imageHeight); //***********************页面头部
Graphics title = image.createGraphics();
//设置区域颜色
title.setColor(new Color(143, 0, 0));
//填充区域并确定区域大小位置
title.fillRect(0, 0, imageWidth, H_title);
//设置字体颜色,先设置颜色,再填充内容
title.setColor(Color.white);
//设置字体
Font titleFont = new Font("宋体", Font.BOLD, 14);
title.setFont(titleFont);
title.drawString("my head", 100, (H_title)/2+5); //***********************插入中间广告图
Graphics mainPic = image.getGraphics();
BufferedImage bimg = null;
try {
bimg = javax.imageio.ImageIO.read(new java.io.File(imgurl));
} catch (Exception e) {} if(bimg!=null){
mainPic.drawImage(bimg, 0, H_title, imageWidth, H_mainPic, null);
mainPic.dispose();
}
//***********************设置下面的提示框 Graphics2D tip = image.createGraphics();
//设置区域颜色
tip.setColor(new Color(255, 120, 89));
//填充区域并确定区域大小位置
tip.fillRect(0, tip_2_top, imageWidth, H_tip);
//设置字体颜色,先设置颜色,再填充内容
tip.setColor(Color.white);
//设置字体
Font tipFont = new Font("宋体", Font.PLAIN, 14);
tip.setFont(tipFont);
tip.drawString("登录成功,本次认证时间1小时", 60, tip_2_top+(H_tip)/2-10);
tip.drawString("正在返回商家主页", 100, tip_2_top+(H_tip)/2+10); //***********************设置下面的按钮块
//设置字体颜色,先设置颜色,再填充内容
tip.setColor(Color.black);
tip.drawString("您可以选择的操作:", 20, btns_2_top);
tip.drawString("下面的小图标:", 20, shops_2_top);
//***********************按钮
Font btnFont = new Font("宋体", Font.BOLD, 14);
Graphics2D btn1 = image.createGraphics();
btn1.setColor(new Color(41,192 , 50));//#29C65A
btn1.fillRect(10, btn1_2_top, W_btn, H_btn);
btn1.setColor(Color.BLACK);
btn1.drawRect(10, btn1_2_top, W_btn, H_btn);
//btn1 文本
btn1.setColor(Color.white);
btn1.setFont(btnFont);
btn1.drawString("单击我啊", 120, btn1_2_top+(H_btn/2)+5); Graphics2D btn2 = image.createGraphics();
btn2.setColor(new Color(141,120 , 22));//#29C65A
btn2.fillRect(10, btn2_2_top, W_btn, H_btn);
btn2.setColor(Color.BLACK);
btn2.drawRect(10, btn2_2_top, W_btn, H_btn);
//btn2文本
btn2.setColor(Color.white);
btn2.setFont(btnFont);
btn2.drawString("单击我啊", 120, btn2_2_top+(H_btn/2)+5); createImage("c:\\hehe.jpg"); } public static void main(String[] args) {
ChartGraphics cg = new ChartGraphics();
try {
cg.graphicsGeneration("ewew", "1", "12", "E:\\work_folder\\picture\\big_pic\\1.jpg");
} catch (Exception e) {
e.printStackTrace();
}
}
}
java根据图片和文字生成自定义图片的更多相关文章
- UIButton图片文字控件位置自定义(图片居右文字居左、图片居中文字居中、图片居左文字消失等)
在开发中经常会碰到需要对按钮中的图片文字位置做调整的需求.第一种方式是通过设置按钮中图片文字的偏移量.通过方法setTitleEdgeInsets和setImageEdgeInsets实现 代码如下: ...
- IOS 截取图片 部分 并生成新图片
/** * 从图片中按指定的位置大小截取图片的一部分 * * @param image UIImage image 原始的图片 * @param rect CGRect rect 要截取的区域 * * ...
- php 图片添加文字水印 以及 图片合成(微信快码传播)
1.图片添加文字水印: $bigImgPath = 'backgroud.png'; $img = imagecreatefromstring(file_get_contents($bigImgPat ...
- Swift - 给图片添加文字水印(图片上写文字,并可设置位置和样式)
想要给图片添加文字水印或者注释,我们需要实现在UIImage上写字的功能. 1,效果图如下: (在图片左上角和右下角都添加了文字.) 2,为方便使用,我们通过扩展UIImage类来实现添加水印功能 ( ...
- C#图片裁切,生成新图片
/// 图片裁剪,生成新图,保存在同一目录下,名字加_new,格式1.png 新图1_new.png /// </summary> /// <param name="pic ...
- java后台中处理图片辅助类汇总(上传图片到服务器,从服务器下载图片保存到本地,缩放图片,copy图片,往图片添加水印图片或者文字,生成二维码,删除图片等)
最近工作中处理小程序宝箱活动,需要java画海报,所以把这块都快百度遍了,记录一下处理的方法,百度博客上面也有不少坑! 获取本地图片路径: String bgPath = Thread.current ...
- java 图片与文字生成PDF
1.jar包:iText-2.1.5.jar 2.code: import java.awt.Color; import java.io.File; import java.io.FileNotFou ...
- c# 生成自定义图片
using System.Drawing; using System.IO; using System.Drawing.Imaging; using System; namespace treads ...
- PHP图片加文字水印和图片水印方法(鉴于李老师博客因没加水印被盗,特搜集的办法。希望能有用!)
$dst_path = 'dst.jpg'; //创建图片的实例 $dst = imagecreatefromstring(file_get_contents($dst_path)); //打上文字 ...
随机推荐
- 为何j2ee变成了javaee?
Sun的版本命名有点乱,Java刚面世时还貌不惊人,直到1.2出现后进步很大,Sun就叫它Java 2了,这个称谓持续到1.4,因此Java的三个平台对应的是J2ME(Java 2 Mobile Ed ...
- Labview中局部变量和全局变量
局部变量的作用域是整个VI,它用于在单个VI中传输数据: 全局变量的作用域是整台计算机,它主要用于多个VI之间共享数据
- lght oj 1257 - Farthest Nodes in a Tree (II) (树dp)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1257 跟hdu2196一样,两次dfs //#pragma comment(l ...
- 操作Cookie的一个陷阱服务器端获取不了maxAge或其它属性
搞了几天终于弄明白了这个问题: 在读取Cookie,然后操作时,除了getName(),getValue()外,不要妄图得到其他信息,如下方法不会得到值的:cookie.getMaxAge();=== ...
- C#使用SMTP发送邮件
需要用到的命名空间: using System.Net.Mail; using System.IO; using System.Text.RegularExpressions; using Syste ...
- DLL文件无法删除怎么解决
dll文件你听说过吗?那怎样把那些删不掉的东西删掉呢?请看.... 老听网友说某某文件删不掉啊.之类的.而且有很多都是dll文件.删除的时候总是提示,"正在使用"或者是" ...
- javascript、jquery获取网页的高度和宽度
javascript: 可视区域宽 :document.documentElement.clientWidth (width + padding) 可视区域高 :document.documentE ...
- 微价值:专訪《甜心爱消除》个人开发人员Lee,日入千元!
[导语]我们希望能够对一些个人开发人员进行专訪,这样大家更能显得接地气,看看人家做什么,怎么坚持.<甜心爱消除>作者Lee是三群的兄弟,也关注微价值.微价值的文章还是能够的,得到一些业内大 ...
- Pre-compile (pre-JIT) your assembly on the fly, or trigger JIT compilation ahead-of-time (转)
Introduction All .NET developers know that one of the best features of the CLR is JIT-compilation: J ...
- Codeforces Round #336 (Div. 2) C. Chain Reaction set维护dp
C. Chain Reaction 题目连接: http://www.codeforces.com/contest/608/problem/C Description There are n beac ...