java 生成图片并在页面显示

imageBean:
package com.c.elmer.util.image;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

public class ImageBean {


int width = 100;
int height = 100;

int imagewidth = 100;
int imageheight = 100;

Color defaultBG = new Color(217,233,243);

BufferedImage image = null;
Graphics2D g = null;

public BufferedImage getImage() {
g.dispose();
image.flush();
return image;
}

public ImageBean(int width,int height){
this.width = width ;
this.height = height;
image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
g = image.createGraphics();
g.setBackground(defaultBG);
g.setColor(new Color(140,201,253));
}

public ImageBean(int width,int height,int imgwidth,int imgheight){
this.width = width ;
this.height = height;
this.imagewidth = imgwidth;
this.imageheight = imgheight;

this.height = imgheight*width/imgwidth;

image = new BufferedImage(this.width, this.height, BufferedImage.TYPE_INT_RGB);
g = image.createGraphics();
g.clipRect(0, 0, this.width, this.height);
g.setBackground(defaultBG);
g.setColor(new Color(242,242,242));
g.fillRect( 0,0,this.width,this.height);
g.setColor(new Color(210,224,242));
g.drawRect(1, 1, this.width - 2, this.height - 2);
// System.out.println(g.getBackground().toString());
}

/**
* <p>取比</p>
* @param a
* @return
*/
public int percentXY(int x,int width,int imgwidth){
return (imgwidth*x) / width;
}


public ImageBean setBgColor(Color c){
g.setBackground(c);
return this;
}


public ImageBean set3dRect(boolean is3d){
if(is3d){
g.draw3DRect(0, 0, width - 1, height - 1, true);
}
return this;
}

public ImageBean setText(String text,String style){
g.setColor(Color.red);
if(style.equals("top")){
g.drawString(text, 0, 12);
}
if(style.equals("center")){
g.drawString(text, this.width - this.width/2-30, this.height/2);
}
if(style.equals("bottom")){
g.drawString(text, this.width - this.width/2-30, this.height-20);
}
return this;
}

public ImageBean addRect(int x,int y,int width,int height,Color c,String text){
x = percentXY(x,this.imagewidth,this.width);
y = percentXY(y,this.imageheight,this.height);
int w = percentXY(width,this.imagewidth,this.width);
int h = percentXY(height,this.imageheight,this.height);

g.setColor(new Color(14,159,241));
g.setBackground(defaultBG);
g.drawRect( x , y , w, h);
g.drawString(text, x + 2, y + 10);
return this;
}
public ImageBean fillRect(int x,int y,int width,int height,Color c,String text){
x = percentXY(x,this.imagewidth,this.width);
y = percentXY(y,this.imageheight,this.height);
int w = percentXY(width,this.imagewidth,this.width);
int h = percentXY(height,this.imageheight,this.height);

g.setColor(c);
g.setBackground(defaultBG);
g.fillRect( x , y , w, h);
g.drawString(text, x + 2, y + 20);
return this;
}

public int getImageheight() {
return imageheight;
}

public void setImageheight(int imageheight) {
this.imageheight = imageheight;
}

public int getImagewidth() {
return imagewidth;
}

public void setImagewidth(int imagewidth) {
this.imagewidth = imagewidth;
}



}



java servlet:

package com.ibox.util;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.Random;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.c.elmer.util.image.ImageBean;
import com.provideo.ibox.model.dao.ModelDAO;
import com.provideo.ibox.model.forms.ModelLocationsBean;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class CreateImage extends HttpServlet {

public CreateImage() {
super();
}

public void destroy() {
super.destroy();
}

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}

Color getRandColor(int fc, int bc) {// 给定范围获得随机颜色
Random random = new Random();
if (fc > 255)
fc = 255;
if (bc > 255)
bc = 255;
int r = fc + random.nextInt(bc - fc);
int g = fc + random.nextInt(bc - fc);
int b = fc + random.nextInt(bc - fc);
return new Color(r, g, b);
}

public BufferedImage getImage(){
int width = 100;
int height = 100;
BufferedImage bi = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics2D g = bi.createGraphics();
g.setBackground(Color.BLUE);
g.clearRect(0, 0, width, height);
g.setColor(Color.RED);
g.dispose();
bi.flush();
return bi;
}

private void createImage(OutputStream out) {
int width = 100;
int height = 100;
int allwidth = 1024;
int allheight = 888;
BufferedImage bi =
new ImageBean(width,height,allwidth,allheight)
.setBgColor(new Color(217,233,243))
.set3dRect(true)
.addRect(22, 222,288,234 , Color.red, "")
.addRect(400, 200,300,500, Color.green, "")
.addRect(2, 500,400,444 , Color.BLUE, "")
.getImage();
/*
BufferedImage bi = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics2D g = bi.createGraphics();
// set background:
g.setBackground(new Color(217,233,243));
g.setColor(new Color(140,201,253));
g.clearRect(0, 0, width, height);
g.drawRect(0, 0, 44, 33);
g.drawArc(32, 45, 78, 54, 45, 222);

// g.drawOval(12, 12, 45, 45);
// g.drawRoundRect(21, 5, 48, 55, 55, 55);

g.setColor(new Color(242,242,242));
g.drawString("Power Ibox", 12, 93);

g.draw3DRect(0, 0, width - 1, height - 1, true);
// set fore color:
g.setColor(Color.RED);
// start draw:
g.drawLine(0, 66, 100, 66);
g.drawLine(66, 0, 66, 66);
// end draw:
g.dispose();
bi.flush();
// encode:
*/

JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bi);
param.setQuality(1.0f, false);
encoder.setJPEGEncodeParam(param);
try {
encoder.encode(bi);
} catch (IOException ioe) {
ioe.printStackTrace();
}
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ModelDAO dao = new ModelDAO();
int tid = 246 ;
if(request.getParameter("tid") != null){
tid = Integer.parseInt(request.getParameter("tid"));
}
int width = 100;
if(request.getParameter("width") != null){
width = Integer.parseInt(request.getParameter("width"));
}
String model = "default";
if(request.getParameter("model") != null){
model = request.getParameter("model");
}
String tname = "";
List list = dao.getModelLocationById(tid);

int height = 100;
int allwidth = 1024;
int allheight = 888;
BufferedImage bi = null;
ImageBean imgbean = null;
for(int i=0;i<list.size();i++){
ModelLocationsBean bean = (ModelLocationsBean) list.get(i);
String wh = bean.getWh();
int w = Integer.parseInt( wh.split("x")[0]);
int h = Integer.parseInt( wh.split("x")[1]);
imgbean = new ImageBean(width,height,w,h);
tname = bean.getTname();
break;
}

for(int i=0;i<list.size();i++){
ModelLocationsBean bean = (ModelLocationsBean)list.get(i);
String location = bean.getLocation();
String[] ls = location.split("x");

int w = Integer.parseInt(ls[0]);

String l = ls[1];

l = l.replace("-", "+-").replace("+", " ");
String[] htl = l.split(" ");

int h = Integer.parseInt( htl[0]);
int top = Integer.parseInt( htl[1]);
int left = Integer.parseInt( htl[2]);

String type = bean.getType();
String text = "";
Color c = new Color(217,233,243);
if(type.equals("1")){
c = new Color(147,165,180);
text = "video";
}
else if(type.equals("2")){
c = new Color(248,165,180);
text = "pic";
}
else if(type.equals("3")){
c = new Color(176,165,122);
text = "flash";
}
else if(type.equals("4")){
c = new Color(219,222,223);
text = "text";
}
else if(type.equals("5")){
c = new Color(219,237,225);
text = "url";
}
else if(type.equals("6")){
c = new Color(147,165,180);
text = "live";
}
else if(type.equals("7")){
c = new Color(248,165,122);
text = "ppt";
}

if(model.equals("default")){
c = Color.red;
imgbean.setText(tname, "top");
imgbean.addRect(top, left, w, h, c, "");
}
if(model.equals("text")){
c = Color.red;
imgbean.addRect(top, left, w, h, c, text);
}
if(model.equals("name")){
c = Color.red;
imgbean.setText(tname, "top");
imgbean.addRect(top, left, w, h, c, text);
}
if(model.equals("empty")){
imgbean.addRect(top, left, w, h, c, "");
}
if(model.equals("fill")){
imgbean.setText(tname, "top");
imgbean.fillRect(top, left, w, h, c, "");
}
}
bi = imgbean.getImage();

JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(response.getOutputStream());
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bi);
param.setQuality(1.0f, false);
encoder.setJPEGEncodeParam(param);
try {
encoder.encode(bi);
} catch (IOException ioe) {
ioe.printStackTrace();
}


// response.setContentType("image/jpeg");
// createImage(response.getOutputStream());

/*
//阻止生成的页面内容被缓存,保证每次重新生成验证码
response.setContentType("image/jpeg");

BufferedImage image = getImage();

ImageIO.write(image,"JPEG",response.getOutputStream()); */
}

public void init() throws ServletException {
// Put your code here
}

}



web.xml:

  <servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>CreateImage</servlet-name>
<servlet-class>com.ibox.util.CreateImage</servlet-class>
</servlet>



页面:
<img id="iboxitem<%=i %>" width="70" src="${pageContext.request.contextPath%20}/servlet/CreateImage?tid=<%=bean.getOid()%20%>&model=text" name="tempimgs" style="-moz-opacity:0.5; filter:alpha(opacity=50);cursor:hand;" onmouseover="imgMouseOver(this)" onmouseout="imgMoused(this);" onclick="tempImgClicked(<%=bean.getOid() %>,this,'<%=bean.getTname() %>')" />



最后效果:
这是从个人项目中剪下来的,(代码也是,不同需求可以更简单)
[img]http://dl.iteye.com/upload/attachment/133549/0dec6623-fe92-3617-ba5e-e3cf568e625e.jpg[/img]

版权声明:本文为wuzijingaip原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。