210分求帮助--关于用jsp或servlet实现动态生成图片!(210分)

  • 主题发起人 主题发起人 capital.com
  • 开始时间 开始时间
C

capital.com

Unregistered / Unconfirmed
GUEST, unregistred user!
我用response.setcontenttype("image/jepg")后
如何才能实现把graphics对象画的东西通过jsp或servlet输出到浏览器里面
就好像那种动态计数器一样(不是组合图片那种)
想了好久也没有头绪,求各位大侠帮忙!!
 
以下 servlet 在内存中生成图像,然后编码成 jpeg, 并输出到servlet的输出流.
(如果需要 gif 格式,则可以用一个 gif 编码类.)
调用 这个 servlet 的方法:
<img src=http://yourserver/servlet/servletjpeg?str=hi>
// filename: servletjpeg.java
// This is a simple Java servlet which can generate jpeg image.
import java.io.*;
import java.awt.*;
import java.awt.image.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.sun.image.codec.jpeg.*;

public class servletjpeg extends HttpServlet{
public voiddo
Get(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
String str = req.getParameter("str");
Frame frame = new Frame();
frame.addNotify();
frame.setBackground(new Color(255,212,212));
BufferedImage image = (BufferedImage) frame.createImage(200, 100);
Graphics g= image.getGraphics();
g.drawString(str, 50, 50);
res.setContentType("image/jpeg");
ServletOutputStream out = res.getOutputStream();
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(image);
}

public voiddo
Post(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
do
Get(req,res);
}
}
 
为什么还要创建frame呢?
最后输出的到底是frame的图像,还是那个image的图像???
 
没有frame就创建不了image,而且还要在frame.addNotify()之后(看看peer)。
 
如果是gif图片,是不是把那个
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(image);
改成gif的就可以了???
 
得下载个 GIFEncoder: http://164.76.13.241/lgc/dispPkg.jsp?pid=33
 
com.sun.image.codec.jpeg哪里有下载啊1?
 
孙老师:
我在linux+tomcat下面跑那个servlet,服务器安装时选了xwin,但是没有配
错误为:
java.lang.InternalError: Can't connect to X11 window server using ':0.0' as the value of the DISPLAY variable.
at sun.awt.X11GraphicsEnvironment.initDisplay(Native Method)
at sun.awt.X11GraphicsEnvironment.(X11GraphicsEnvironment.java:58)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:120)
at java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment(GraphicsEnvironment.java:59)
at sun.awt.motif.MToolkit.(MToolkit.java:56)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:120)
at java.awt.Toolkit$2.run(Toolkit.java:495)
at java.security.AccessController.doPrivileged(Native Method)
at java.awt.Toolkit.getDefaultToolkit(Toolkit.java:488)
at java.awt.Window.getToolkit(Window.java:416)
at java.awt.Frame.addNotify(Frame.java:270)
at newtest.doGet(newtest.java:12)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:740)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.tomcat.core.ServletWrapper.handleRequest(ServletWrapper.java:503)
at org.apache.tomcat.core.ServletWrapper.handleRequest(ServletWrapper.java:597)
at org.apache.tomcat.servlets.InvokerServlet.service(InvokerServlet.java:257)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.tomcat.core.ServletWrapper.handleRequest(ServletWrapper.java:503)
at org.apache.tomcat.core.ContextManager.service(ContextManager.java:559)
at org.apache.tomcat.service.connector.Ajp12ConnectionHandler.processConnection(Ajp12ConnectionHandler.java:156)
at org.apache.tomcat.service.TcpConnectionThread.run(SimpleTcpEndpoint.java:338)
at java.lang.Thread.run(Thread.java:475)
打头的那个错误消息是什么意思啊,难道我创建frame必须要xwin支持?
这么说来,没有xwin的服务器没法动态输送图了?
 
Actually you can work without Frame. Use BufferedImage Class, it, getGraphics, then
, you can draw something on it.(Details indo
C) and then
do what Dr. Sun said to encode and put bytes to outputStream.
Compared with GIF, I prefer JPEG. You can get JpegEncoder from java official site.
Good luck.
e.g.:
BufferedImage image = new BufferedImage(300,200);
From sun:
Graphics g= image.getGraphics();
g.drawString(str, 50, 50);
res.setContentType("image/jpeg");
ServletOutputStream out = res.getOutputStream();
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(image);
 
多人接受答案了。
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
后退
顶部