aspose file tools*
The moose likes Servlets and the fly likes Produce image dynamicly by servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Produce image dynamicly by servlet" Watch "Produce image dynamicly by servlet" New topic
Author

Produce image dynamicly by servlet

Niu Xiuyuan
Ranch Hand

Joined: Jun 08, 2001
Posts: 68
I wanna write a counter by servlet,but I don't know how to do.Who can help me?


Sun Certified Java Programmer<br />Sun Certified Web Component Developer
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Here are the basics:

Basically, anything you do to the Graphics context gets output to the client...
You'll want to write text to the graphics context, obviously!
Dave.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12835
    
    5
You may find it necessary to set the contentLength in addition to the content type. If so you have to write to a ByteArrayOutputStream first so you can get the size - then output the resulting byte array.
Bill
Tracey Currier
Ranch Hand

Joined: Feb 06, 2001
Posts: 50
This is a really good example for what I have been trying to do the last couple of days. But I am still not getting the image drawn in the applet. I checked the byte array, it is loaded in. My utility class says I don't have a jpeg but I will disregard this because I know I have a byte[]. I convert to an int[] and use:
Image temp_image = createImage(new MemoryImageSource(200, 200,int_arr, 0, 200));
g.drawImage(temp_image,0,0,this);
but still doesn't draw, what am I missing?
Thanks


MS,MS,SCJP,SCBCD Seize the day!
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12835
    
    5
If I understand what you are saying, I think the problem is on the Applet end. How are you treating the data stream from the servlet?
If you address the servlet directly from a browser do you get an image?
Bill
Tracey Currier
Ranch Hand

Joined: Feb 06, 2001
Posts: 50
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException{
ServletOutputStream out = res.getOutputStream();
ByteArrayOutputStream bout = new ByteArrayOutputStream();
res.setContentType("image/jpg");
try{

BufferedImage buffImg = new BufferedImage(200,200, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = buffImg.createGraphics();
//anything you do to the Graphics Context gets written to the BufferedImage
g2.setColor(Color.red);
Rectangle s = new Rectangle(100,100);
g2.fill(s);
g2.dispose();
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(buffImg);
param.setQuality(1.0f,true);
encoder.encode(buffImg,param);


int sizebuf = bout.size();
res.setContentLength(sizebuf);
bout.writeTo(out);
bout.flush();
bout.close();

out.flush();
out.close();
Tracey Currier
Ranch Hand

Joined: Feb 06, 2001
Posts: 50
I just realized that the fill method is fillRect - I will change and try again.
Tracey Currier
Ranch Hand

Joined: Feb 06, 2001
Posts: 50
no, fill is ok, it's fill(shape)
I am thinking that I need to add a ColorModel
to the my MemoryImageSource constructor in the
AppletCode???
Tracey Currier
Ranch Hand

Joined: Feb 06, 2001
Posts: 50
This is how I receive the stream in the applet:
InputStream in = con.getInputStream();
ByteArrayOutputStream bstream = new ByteArrayOutputStream();
int c=0;int i =0;
while((c = in.read()) != -1){
bstream.write(c);
}

byt_arr = bstream.toByteArray();
Tracey Currier
Ranch Hand

Joined: Feb 06, 2001
Posts: 50
No, I can't call it up in my browser, I didn't
even realize you could do this with an image, I
thought I had to send it to an applet. Neat (especially if I can get it to work).
Tracey Currier
Ranch Hand

Joined: Feb 06, 2001
Posts: 50
I'm using linux op on server and IPlanet web server, I am trying to resolve the awt to X11 graphics environment problem
???
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12835
    
    5
I think you are making things overly complex. Instead of reading a byte array and trying to interpret it, read the stream as if it was coming from a file using the built in image grabber in the Applet class -
getImage( URL url )
where the URL addresses your servlet and includes the parameter to identify the image.
Bill
Tracey Currier
Ranch Hand

Joined: Feb 06, 2001
Posts: 50
what would the parameter be?
I tried "image/gif" or "gif"
(changed from jpeg)
and "http"
Thanks!
Tracey Currier
Ranch Hand

Joined: Feb 06, 2001
Posts: 50
I tried the class name of the servlet if this
parameter is "name" like "name of the servlet" when you are mapping the servlet, no go yet
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12835
    
    5
I mean like this where the parameter tells the ImgServ which image to serve up.
urlstring = "http:/yourhost/servlet/ImgServ?Image=NameOfImage" ;
In other words what you would have in an <IMG tag in a plain HTML page.
ImgServ does a request.getParameter("Image"); and has to set the content type and content size.
Bill
Prashanth menon
Ranch Hand

Joined: Feb 20, 2001
Posts: 65
Originally posted by Tracey Currier:
No, I can't call it up in my browser, I didn't
even realize you could do this with an image, I
thought I had to send it to an applet. Neat (especially if I can get it to work).

Hi I had checked that code. and I could call it up through the browser.
http://localhost/servlet/SampleImgServlet
may be you should check the content type.
you have set it as "image/jpg". I think it should be "image/jpeg". In the former case you may get the file save as dialog.
Prashanth
Prashanth menon
Ranch Hand

Joined: Feb 20, 2001
Posts: 65
Hi,
Since the Contenttype for the response has already been set as image, how is it possible to send an HTML. I think we must possibly write image to a filestream and the give the path as attribute for SRC of IMG tag.
Prashanth
Tracey Currier
Ranch Hand

Joined: Feb 06, 2001
Posts: 50
it turned out to be a problem with the graphics environment, I was working in a "headless"
environment so could not use the awt classes.
java 1.4 has classes that enable some of the
awt classes in a headless environment. we got
the example to work by installing a graphics environment on the server !!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Produce image dynamicly by servlet