This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
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 Java Interview Guide this week in the Jobs Discussion 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

Why the jpg image produced by servlet is so poor ?

Li Shangqiang

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:

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

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

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.


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?

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

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:

import javax.servlet.*;
import javax.servlet.http.*;
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
//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);
//OK now!

The sun jpeg package in in jdk1.2 and later, you can find the document in
Li Shangqiang

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:


Wish you success!
I agree. Here's the link:
subject: Why the jpg image produced by servlet is so poor ?
jQuery in Action, 3rd edition