在服务器端动态生成gif或jpg示例
创始人
2024-03-31 11:22:18
0

本段代码实现了在服务器端动态生成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即将带来的感官之变

相关内容

热门资讯

如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...
《非诚勿扰》红人闫凤娇被曝厕所... 【51CTO.com 综合消息360安全专家提醒说,“闫凤娇”、“非诚勿扰”已经被黑客盯上成为了“木...
2012年第四季度互联网状况报... [[71653]]  北京时间4月25日消息,据国外媒体报道,全球知名的云平台公司Akamai Te...