Jerry,
I am running the program using JDK1.2 under Win NT.
1. In terms of "streching" there is a marked spacing difference between
ANTIALIASING_ON and ANTIALIASING_OFF.
2. Antialiasing causes the text to be smoother than when it is turned
off
but at font sizes below 9 the text is unreadable. For the application
that
I am developing, I don't really have any control on the fonts, their
sizes
and placement. We have a certain amout of "fine print" that needs to be
put into the image.
I ran the program that you attached and it results in the same problem,
where the text is not sharp, but fuzzy.
I am also including a copy of my program
thanks
mathew
Jerry Evans wrote:
>
> >I am trying to create an Graphics image at run time and write it out
> >to a file using the JPEG encoding provided with JDK1.2. The image
> >consists
> >of mostly text, lines and barcode images.
> >
> >The lines come out fine, but the text is blurred. I am including some of
> >the
> >code. Any help would be appreciated.
> >
> >Also noticed the following:
> >
> >1. When antialiasing is off, the text font is of very poor quality.
> >2. When antialiasing is on, the text font seems okay (except for the
> >blurr
> > but the characters are stretched out.
>
> Mathew,
>
> I'm not sure what you mean by blur and by stretched out. If you could
> send a screen shot and/or a complete test program, that would be helpful.
> What platform are you running on and what Graphics2D rendering attributes
> are you using?
>
> Antialiasing of course introduces what might be called blur, and as has
> been pointed out, JPEG is really intended for continuous-tone images.
>
> I took the code snippet you sent and created the following program, which
> works fine for me (i.e. no blur in the JPEG image except for the "partially
> on" pixels for the antialiased text, and no stretching that I could detect).
> I ran the test under NT4.
>
> Jerry Evans
>
> import java.awt.*;
> import java.awt.event.WindowEvent;
> import java.awt.event.WindowListener;
> import java.awt.event.WindowAdapter;
> import java.awt.image.BufferedImage;
> import com.sun.image.codec.jpeg.JPEGCodec;
> import com.sun.image.codec.jpeg.JPEGImageEncoder;
> import com.sun.image.codec.jpeg.JPEGEncodeParam;
> import java.awt.geom.GeneralPath;
> import java.io.File;
> import java.io.FileOutputStream;
>
> public class JPEGtext extends Canvas {
>
> public JPEGtext() {
> setBackground(Color.white);
> }
>
> public void paint(Graphics g) {
> Graphics2D g2 = (Graphics2D) g;
> int w = getSize().width;
> int h = getSize().height/2;
> BufferedImage bi = new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB);
> Graphics2D big = bi.createGraphics();
> big.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
> RenderingHints.VALUE_ANTIALIAS_ON);
>
> big.setBackground(Color.white);
> big.clearRect(0, 0, w, h);
> big.setColor(Color.black);
>
> big.drawString("Text", w/4, h/4);
>
> g2.drawImage(bi, 0, 0, this);
>
> try {
> File file = new File("test.jpg");
> FileOutputStream out = new FileOutputStream(file);
> JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
> JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bi);
> param.setQuality(1.0f, false);
> encoder.setJPEGEncodeParam(param);
> encoder.encode(bi);
> } catch (Exception ex) { ex.printStackTrace(); }
>
> Image img = getToolkit().getImage("test.jpg");
> try {
> MediaTracker tracker = new MediaTracker(this);
> tracker.addImage(img, 0);
> tracker.waitForID(0);
> } catch (Exception e) { e.printStackTrace(); }
> g2.drawImage(img, 0, h, this);
>
> g2.setColor(Color.blue);
> g2.drawLine(0,h,w,h);
> g2.drawString("BufferedImage", 5, 15);
> g2.drawString("JPEG", 5, h+15);
> }
>
> public static void main(String s[]) {
> WindowListener l = new WindowAdapter() {
> public void windowClosing(WindowEvent e) {System.exit(0);}
> };
> Frame f = new Frame("Java 2D Demo - JPEGtext");
> f.addWindowListener(l);
> f.add("Center", new JPEGtext());
> f.pack();
> f.setSize(new Dimension(400,300));
> f.show();
> }
>
> }
Print2D.java