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)); //打上文字 ...
随机推荐
- csu oj 1804: 有向无环图 (dfs回溯)
题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1804 中文题意就不说了. dfs从底到根回溯即可,看代码应该能清楚. //#pragma ...
- Objective-C 学习记录--toches、Motion/Size/Rect/Point/CGFloat/protocol
- (void)touchesBegan touchesEnd touchesCancelled touchesMoved //代表的是手指在屏幕上的动作,开始 结束 取消 移动 //还有就是代表摇动 ...
- 利用HTML5开发Android(6)---构建HTML5离线应用
需要提供一个cache manifest文件,理出所有需要在离线状态下使用的资源例如 Manifest代码 CACHE MANIFEST #这是注释 images/sound-icon.png ima ...
- 6 种CSS设置居中的方法
原文 Demos of each of the methods below by clicking here. Horizontal centering with css is rather easy ...
- CountDownLatch和CyclicBarrier的区别
[CountDownLatch.CyclicBarrier和Semaphore]http://www.cnblogs.com/dolphin0520/p/3920397.html [CountDo ...
- javaScript-原型、继承-02
原型链 首先回顾下实列.构造函数.原型对象之间的关系: 实列都包含指向原型对象的一个指针(_proto_): 构造函数都有prototype(原型属性)指向原型对象的指针: 原型是一个对象也存在一个内 ...
- mvvm架构使用解析
配置 android studio目前已经集成了dataBinding,只需在build.gradle中配置,如下: android { dataBinding { enabled = true; } ...
- 可拖拽GridView代码解析
本片学习笔记是对eoe网上一个项目代码的解读.详细项目作者的博客例如以下:http://blog.csdn.net/vipzjyno1/article/details/26514543.项目源代码下载 ...
- Python 存储模型
1.Python彻底分离了对象和引用,可以认为内存中的对象都是不可修改的,每次修改引用,相当于在堆上重新创建一个对象,引用指向新对象. 2.对于数值和字符串,修改意味着引用指向一个新对象. 3.集合中 ...
- Visual Studio 2015 和 Apache Cordova
英文原版:http://www.codeproject.com/Articles/860150/Visual-Studio-and-Apache-Cordova 在开始前,问一下自己下面这些问题: 熟 ...