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)); //打上文字 ...
随机推荐
- Codeforces Round #368 (Div. 2) A. Brain's Photos (水题)
Brain's Photos 题目链接: http://codeforces.com/contest/707/problem/A Description Small, but very brave, ...
- C# Common Keyword
[C# Common Keyword] 1.abstract Use the abstract modifier in a class declaration to indicate that a c ...
- POJ 2888 Magic Bracelet(Burnside引理,矩阵优化)
Magic Bracelet Time Limit: 2000MS Memory Limit: 131072K Total Submissions: 3731 Accepted: 1227 D ...
- 关于诺顿身份安全2013独立版(Norton Identity Safe)
现在身份安全这货好像从诺顿的套装当中独立出来了,出了中文版.其实诺顿的Web信誉做得还是不错的,当然天朝不要有太大期望.只是公认的做web信誉做得最好的应该就是趋势科技和诺顿,所以诺顿的身份安全也许还 ...
- cisco tftp 备份/恢复
使用tftp服务器对cisco 3560 配置备份及恢复 Switch#copy running-config tftp:Address or name of remote host []? 192. ...
- [iOS微博项目 - 3.2] - 发送微博
github: https://github.com/hellovoidworld/HVWWeibo A.使用微博API发送微博 1.需求 学习发送微博API 发送文字微博 发送带有图片的微博 ...
- [iOS微博项目 - 3.0] - 手动刷新微博
github: https://github.com/hellovoidworld/HVWWeibo A.下拉刷新微博 1.需求 在“首页”界面,下拉到一定距离的时候刷新微博数据 刷新数据的时候使 ...
- HDU 1712 ACboy needs your help (分组背包模版题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1712 有n门课,和m天时间.每门课上不同的天数有不同的价值,但是上过这门课后不能再上了,求m天里的最大 ...
- How Tomcat Works(十四)补充
在How Tomcat Works(十四)中,本人并没有对javax.servlet.Filter及javax.servlet.FilterChain做详细的描述,本文在这里做一下补充 FilterC ...
- ......那么Win8.1怎么去掉文件夹?
(从已经死了一次又一次终于挂掉的百度空间人工抢救出来的,发表日期2014-05-11) 细心的朋友会发现,在Win8.1这台电脑(计算机)中,除了我们最熟悉的磁盘外,还新增了视频.图片.文档.下载.音 ...