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根据图片和文字生成自定义图片的更多相关文章

  1. UIButton图片文字控件位置自定义(图片居右文字居左、图片居中文字居中、图片居左文字消失等)

    在开发中经常会碰到需要对按钮中的图片文字位置做调整的需求.第一种方式是通过设置按钮中图片文字的偏移量.通过方法setTitleEdgeInsets和setImageEdgeInsets实现 代码如下: ...

  2. IOS 截取图片 部分 并生成新图片

    /** * 从图片中按指定的位置大小截取图片的一部分 * * @param image UIImage image 原始的图片 * @param rect CGRect rect 要截取的区域 * * ...

  3. php 图片添加文字水印 以及 图片合成(微信快码传播)

    1.图片添加文字水印: $bigImgPath = 'backgroud.png'; $img = imagecreatefromstring(file_get_contents($bigImgPat ...

  4. Swift - 给图片添加文字水印(图片上写文字,并可设置位置和样式)

    想要给图片添加文字水印或者注释,我们需要实现在UIImage上写字的功能. 1,效果图如下: (在图片左上角和右下角都添加了文字.) 2,为方便使用,我们通过扩展UIImage类来实现添加水印功能 ( ...

  5. C#图片裁切,生成新图片

    /// 图片裁剪,生成新图,保存在同一目录下,名字加_new,格式1.png 新图1_new.png /// </summary> /// <param name="pic ...

  6. java后台中处理图片辅助类汇总(上传图片到服务器,从服务器下载图片保存到本地,缩放图片,copy图片,往图片添加水印图片或者文字,生成二维码,删除图片等)

    最近工作中处理小程序宝箱活动,需要java画海报,所以把这块都快百度遍了,记录一下处理的方法,百度博客上面也有不少坑! 获取本地图片路径: String bgPath = Thread.current ...

  7. java 图片与文字生成PDF

    1.jar包:iText-2.1.5.jar 2.code: import java.awt.Color; import java.io.File; import java.io.FileNotFou ...

  8. c# 生成自定义图片

    using System.Drawing; using System.IO; using System.Drawing.Imaging; using System; namespace treads ...

  9. PHP图片加文字水印和图片水印方法(鉴于李老师博客因没加水印被盗,特搜集的办法。希望能有用!)

    $dst_path = 'dst.jpg'; //创建图片的实例 $dst = imagecreatefromstring(file_get_contents($dst_path)); //打上文字 ...

随机推荐

  1. Gradle 1.3之前的Publishing artifacts

    在Gradle1.3之前,Publishing artifacts是使用uploadConfigurationName来publish 声明artifacts是靠使用 build.gradle art ...

  2. HDU 1561The more, The Better(树形DP)

    HDU 1561  The more, The Better 题目大意就不说了 直接DP[i][j]表示i为跟节点的子树上攻克j个城堡的所能获得的最多宝物的数量 DP[fa][j] = MAX{DP[ ...

  3. POJ1837 Balance(DP)

    POJ1837http://poj.org/problem?id=1837 题目大意就是说有一个称上有C个挂钩,告诉你每个挂钩的位置,现在有G个重物,求是之平衡的方法数. 转化一下:DP[i][j]表 ...

  4. POJ 3176 Cow Bowling (水题DP)

    题意:给定一个金字塔,第 i 行有 i 个数,从最上面走下来,只能相邻的层数,问你最大的和. 析:真是水题,学过DP的都会,就不说了. 代码如下: #include <cstdio> #i ...

  5. 隐马尔可夫模型(HMM)

    转自:http://blog.csdn.net/likelet/article/details/7056068 隐马尔可夫模型 (Hidden Markov Model,HMM) 最初由 L. E. ...

  6. uva 558 - Wormholes(Bellman Ford判断负环)

    题目链接:558 - Wormholes 题目大意:给出n和m,表示有n个点,然后给出m条边,然后判断给出的有向图中是否存在负环. 解题思路:利用Bellman Ford算法,若进行第n次松弛时,还能 ...

  7. UserControl和CustomControl基础【PluraSight】

    UserControl UserControl实际上就是ContentControl,xaml里<UserControl></UserControl>tag之间包含的实际就是后 ...

  8. 使用spring.net 1.3.2框架部署在虚拟目录上发生错误

    如果你的网站使用了Spring.net 1.3.2,并部署在IIS的虚拟目录上,那么将会出现如下错误:    The virtual path '/currentcontext.dummy' maps ...

  9. cocos2d-x 用浏览器打开网页

    转自:http://www.xuebuyuan.com/1396292.html,http://www.cocoachina.com/bbs/read.php?tid=88589 First!! 源代 ...

  10. How to get cocoapods work on Yosemite

    查看原文:http://leancodingnow.com/how-to-get-cocoapods-work-on-yosemite/ 今天升级了Mac OS X 10.10-Yosemite以后运 ...