java动态生成图片 并把当前时间写在图片上

 

servlet:

 

package timeImage;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

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

import com.sun.image.codec.jpeg.ImageFormatException;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

@SuppressWarnings("serial")
public class CreateImageServlet extends HttpServlet {

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

  response.setContentType("image/jpeg");
  createImage(response.getOutputStream());
 }
 @SuppressWarnings("unused")
 private void createImage(OutputStream out){
  SimpleDateFormat   formatter   =   new   SimpleDateFormat   ("yyyy年MM月dd日   HH:mm:ss     ");     
     Date   curDate   =   new   Date(System.currentTimeMillis());//获取当前时间  
     String s = "岱嘉:"+formatter.format(curDate); //转化为字符串
     Font font = new Font("Serif", Font.BOLD, 10); //设置字体 及大小
  int width = 250;  //图片的大小
  int height = 200;
  BufferedImage bi=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
  Graphics2D g=bi.createGraphics();
  g.setBackground(Color.green);
  g.clearRect(0, 0, width, height);
  g.setColor(Color.red);
  FontRenderContext context = g.getFontRenderContext(); 
//  g.drawLine(0, 0, 99, 199);
  Rectangle2D bounds = font.getStringBounds(s, context);   
        double x = (width - bounds.getWidth()) / 2;   
        double y = (height - bounds.getHeight()) / 2;
        double ascent = -bounds.getY();   
        double baseY = y + ascent;   
  g.drawString(s, (int)x, (int)y);
  g.dispose();
  bi.flush();
  JPEGImageEncoder encoder=JPEGCodec.createJPEGEncoder(out);
  JPEGEncodeParam param=encoder.getDefaultJPEGEncodeParam(bi);
  param.setQuality(1, false);
  encoder.setJPEGEncodeParam(param);
  
  try {
   encoder.encode(bi);
  } catch (ImageFormatException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }

}

 

jsp:

<img alt="hello" src="CreateImage" align="middle">

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值