>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();
    }

}


=====================================================================
To subscribe/unsubscribe, send mail to [EMAIL PROTECTED]
Java 2D Home Page: http://java.sun.com/products/java-media/2D/

Reply via email to