It's not a secret anymore!*
The moose likes Swing / AWT / SWT and the fly likes Why the jpg image produced by servlet is so poor ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Why the jpg image produced by servlet is so poor ?" Watch "Why the jpg image produced by servlet is so poor ?" New topic
Author

Why the jpg image produced by servlet is so poor ?

Li Shangqiang
Greenhorn

Joined: Mar 25, 2001
Posts: 22

I have use BufferedImage to produce a immediate image, and use Sun jpeg package class to output it in servlet, but when i draw a line, there are many colorful points around the line, just like a low-quality jpeg image.



I worked like this:


<pre>
BufferedImage image = new BufferedImage(intImgWidth, intImgHeight, BufferedImage.INT_RGB);
Graphics g = image.getGraphics();
...
g.drawLine();
g.drawString();
...
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder((OutputStream)out);
encoder.encode(image); //��������
</pre>

What's the problem: BufferedImage, JPEGEncoder, or my wrong use?
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

It's not just "low-quality JPEG images"... all JPEG images are compressed, and thus have artifacts in them... especially if you are drawing a line!

If anything it is the fault of the JPEGencoder... if you look in the API docs for JPEGImageEncoder, you will find the encode(BufferedImage bi, JPEGEncodeParam jep) method... you are using a the default JPEGEncodeParam object... you may be able to get better image quality by messing around with settings in the JPEGEncodeParam object...

Also, look into third party libraries or the JMF (Java Media Framework). It may be better for you to use some other image format (such as GIF or PNG) that doesn't compress the image.

-Nate

-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Joel Cochran
Ranch Hand

Joined: Mar 23, 2001
Posts: 301
I am very interested in following a similar path. I currently have an Applet that draws a simple line-based sketch but have run into plenty of problem because of the Applet approach. I would really like to convert this to a server side solution where a Java Program draws the skethc and saves it as a .jpg and then sends the .jpg address out as a imple HTML .
Does anyone have experience with this sort of approach? If so, how would I go about getting started? Does anyone have a link to the API documentation for the JPEG APIs?
Thanks,
Joel


Wait a minute, I'm trying to think of something clever to say...<p>Joel
Li Shangqiang
Greenhorn

Joined: Mar 25, 2001
Posts: 22


There are 3 approaches, in an article on JGuru( I don't know where now), one is the approach i use: Do your drawing code on an BufferedImage, then encode it with a JPEGEncoder to the servlet outputstream. The encoder is very easy to use, So you should just know smt about servlet.

The code is like this:

<pre>
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.awt.*;
import java.awt.image.*;
import com.sun.
//There may be syntax errors here, because i'm not on my own computer and can't get src code
//But everything is in it
public class MyImageServlet extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
response.setContentType("image/jpeg");
//get the outputstream
OutputStream out = response.getOutputStream();
//do your drawing here
BufferedImage image = new BufferedImage(intWidth, intHeight);
Graphics g = image.getGraphics();
g.drawString("Dynamic Image", 5, 5);
//output the image
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder((OutputStream)out);encoder.encode(image);
out.flush();
out.close();
g.dispose();
//OK now!
}
}
</pre>




The sun jpeg package in in jdk1.2 and later, you can find the document in http://java.sun.com/j2se/1.3/docs/guide/2d/api-jpeg/overview-summary.html
Li Shangqiang
Greenhorn

Joined: Mar 25, 2001
Posts: 22

I've forget somthing:


If it works fine, you can type the url: http://localhost/servet/MyImageServlet, and the image not html will show.


Now you can write a html like this to use the image:
<pre>

</pre>

Wish you success!
 
wood burning stoves
 
subject: Why the jpg image produced by servlet is so poor ?