在服务器端动态生成gif或jpg示例

开发 后端
下面这段Java代码可以实现在服务器端动态生成gif或jpg的功能。

本段代码实现了在服务器端动态生成gif或jpg的功能:

package web.images;

/**
* 此处插入类型描述。
* @author:Administrator
*/
import java.io.*;

import java.util.*;

import com.sun.image.codec.jpeg.*;

import java.awt.image.*;

import java.awt.*;


public class ChartGraphics {

BufferedImage image;

public void createImage(String fileLocation) {

try {

FileOutputStream fos = new FileOutputStream(fileLocation);

BufferedOutputStream bos = new BufferedOutputStream(fos);

JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos);

encoder.encode(image);

bos.close();

} catch(Exception e) {

System.out.println(e);

}

}


public void graphicsGeneration(int h1,int h2,int h3,int h4,int h5) {


final int X=10;

int imageWidth = 800;//图片的宽度

int imageHeight =900;//图片的高度

int columnWidth=70;//柱的宽度

int columnHeight=800;//柱的最大高度


ChartGraphics chartGraphics = new ChartGraphics();

chartGraphics.image = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB);

Graphics graphics = chartGraphics.image.getGraphics();

graphics.setColor(Color.white);

graphics.fillRect(0,0,imageWidth,imageHeight);

graphics.setColor(Color.black);
//graphics.setXORMode(Color.blue);
graphics.drawLine(columnWidth+X,columnHeight-h5-50,columnWidth+X,columnHeight);//Y轴

graphics.drawLine(columnWidth+X,columnHeight-h5-50,columnWidth+X-10,columnHeight-h5-40);//Y轴箭头左

graphics.drawLine(columnWidth+X,columnHeight-h5-50,columnWidth+X+10,columnHeight-h5-40);//Y轴箭头右

graphics.drawLine(750,columnHeight,columnWidth+X,columnHeight);//X轴

graphics.drawLine(750,columnHeight,740,columnHeight-10);//X轴

graphics.drawLine(750,columnHeight,740,columnHeight+10);//X轴

graphics.setColor(Color.yellow);

graphics.fillRect(X+1*columnWidth+10, columnHeight-h1, columnWidth, h1);

graphics.fillRect(X+2*columnWidth+20, columnHeight-h2, columnWidth, h2);

graphics.fillRect(X+3*columnWidth+30, columnHeight-h3, columnWidth, h3);

graphics.fillRect(X+4*columnWidth+40, columnHeight-h4, columnWidth, h4);

graphics.fillRect(X+5*columnWidth+50, columnHeight-h5, columnWidth, h5);

//graphics.drawString()

chartGraphics.createImage("D:\\leaderquery\\leaderqueryWeb\\web800\\images\\chart.jpg");

}

}

 

【编辑推荐】

  1. JavaOne 2009第三天:微软与Sun/Oracle携手并进
  2. 开发高可移植性J2ME的软件
  3. Java虚拟机(JVM)中的内存设置详解
  4. JavaFX重装上阵 加强Web领域开发
  5. 51CTO独家报道:JavaFX即将带来的感官之变
责任编辑:王观 来源: 百家编程
相关推荐

2012-10-15 13:40:15

IBMdw

2013-12-25 11:01:16

JavaScript

2018-07-09 10:01:56

2014-01-15 10:06:30

vFlash

2015-11-04 14:14:56

HTTP网络协议

2011-06-07 16:01:46

Android 服务器 数据交互

2017-12-06 22:29:53

2010-01-08 13:48:51

JSON 形式

2014-11-14 11:03:56

微软.NET

2023-06-30 08:00:00

漏洞网络安全SSTI

2021-07-27 06:14:32

服务器端移动端性能测试

2012-05-21 10:52:43

2010-10-15 08:57:15

PHP多进程

2009-07-27 12:56:27

控件CheckBoxLASP.NET服务器

2009-10-20 17:28:02

VB.NET服务器端

2010-05-27 18:49:38

SVN入门

2010-04-21 13:18:33

RAC负载均衡配置

2010-08-27 10:23:26

DHCP服务器

2009-02-16 16:30:23

OperaTurbo服务器

2024-02-22 13:47:40

点赞
收藏

51CTO技术栈公众号