• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why the jpg image produced by servlet is so poor ?

 
Li Shangqiang
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Joel Cochran
Ranch Hand
Posts: 301
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Li Shangqiang
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic