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();
		}
	}
}

  

时间: 2024-07-31 10:39:46

java根据图片和文字生成自定义图片的相关文章

压缩图片操作和生成圆形图片

1.对本地相册库或相机拍摄下来的图片进行压缩处理,传进来的参数分别有:要压缩的图片和压缩后的大小. //压缩图片 + (UIImage*)imageWithImageSimple:(UIImage*)image scaledToSize:(CGSize)newSize { // Create a graphics image context UIGraphicsBeginImageContext(newSize); // Tell the old image to draw in this ne

WP8_给图片、按钮设置自定义图片

工程目录下新建文件夹Images,将图片文件复制到Images文件夹里,本文以image1为例 1).在xaml里加背景图片 image图片: <Stretch="Fill" Source="Images/image1.png"  Name="image"/> Button背景图片:<Button Name="button1" >            <Button.Background>

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

在开发中经常会碰到需要对按钮中的图片文字位置做调整的需求.第一种方式是通过设置按钮中图片文字的偏移量.通过方法setTitleEdgeInsets和setImageEdgeInsets实现 代码如下: /*!**方式一***/ - (void)updateBtnStyle_rightImage:(UIButton *)btn { CGFloat btnImageWidth = btn.imageView.bounds.size.width; CGFloat btnLabelWidth = btn

图片操作,生成一个图片

知识点: 1.先创建一个Image,再创建一个Graphic 问题: 生成一个图片 解决方案 1 <%@ WebHandler Language="C#" Class="writeAPic" %> 2 3 using System; 4 using System.Web; 5 using System.Drawing; 6 7 public class writeAPic : IHttpHandler { 8 9 public void ProcessR

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

1.图片添加文字水印: $bigImgPath = 'backgroud.png'; $img = imagecreatefromstring(file_get_contents($bigImgPath)); $font = 'msyhl.ttc';//字体 $black = imagecolorallocate($img, 0, 0, 0);//字体颜色 RGB $fontSize = 20; //字体大小 $circleSize = 60; //旋转角度 $left = 50; //左边距

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

/** * 从图片中按指定的位置大小截取图片的一部分 * * @param image UIImage image 原始的图片 * @param rect CGRect rect 要截取的区域 * * @return UIImage */ + (UIImage *)ct_imageFromImage:(UIImage *)image inRect:(CGRect)rect{ //把像 素rect 转化为 点rect(如无转化则按原图像素取部分图片) CGFloat scale = [UIScre

如何给gif图片添加文字?GIF图片添加文字教程

我们经常会在网上下载一下GIF动态图片,有时候我们想给我们下载的GIF动态图片添加文字,这时候该怎么操作,下面小编就来分享一下给GIF图片添加文字的教程给大家,供大家参考和学习,希望大家都是能够满意的. GIF格式可以存多幅彩色图像,如果把存于一个文件中的多幅图像数据逐幅读出并显示到屏幕上,就可构成一种最简单的动画. 迅捷GIF制作工具添加文字就是将我们添加进来GIF图片逐帧分解成若干张图片,然后我们批量的在这些图片上添加文字. 迅捷GIF制作工具http://www.xunjieshipin.

一个不错的基于文字生成favicon图片的网站

http://faviconist.com/ 例子:

PHP图片加文字水印和图片水印方法

文字水印 $dst_path = 'dst.jpg'; //创建图片的实例$dst = imagecreatefromstring(file_get_contents($dst_path)); //打上文字$font = './simsun.ttc';//字体$black = imagecolorallocate($dst, 0x00, 0x00, 0x00);//字体颜色imagefttext($dst, 13, 0, 20, 20, $black, $font, '快乐编程'); //输出图