Charles Bell
Posts: 519
Nickname: charles
Registered: Feb, 2002
|
|
Re: displaying text in jpeg image
|
Posted: Mar 20, 2002 2:45 PM
|
|
The following code creates a jpeg image file file and writes text into it. You could adapt it to your problem or program.
/** CreateJPEG.java * @author Charles Bell * @version December 12, 2000 */
import java.awt.*; import java.awt.event.*; import java.awt.image.*; import java.io.*;
import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.image.codec.jpeg.JPEGImageEncoder;
public class CreateJPEG{
public static void main(String[] args){ CreateJPEG createjpeg = new CreateJPEG(); createjpeg.init(); }
public void init(){ try{ String filename = "MyJPEG.jpg"; int imagewidth = 200; int imageheight = 25; int imageType = BufferedImage.TYPE_INT_RGB; // Create an image buffer in which to paint on. BufferedImage jpegimage = new BufferedImage(imagewidth, imageheight,imageType);
// Create graphics object associated with the jpeg image Graphics2D graphics2d = jpegimage.createGraphics();
// Paint image on the graphics object graphics2d.setColor(Color.black); graphics2d.drawRect(1,1,imagewidth,imageheight); graphics2d.setColor(Color.red); graphics2d.drawString("My JPEG",1,10); //graphics2d.dispose();
// JPEG-encode the image //and write to file. OutputStream os = new FileOutputStream(filename); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(os); encoder.encode(jpegimage); os.close(); } catch (IOException ioe) { System.err.println(ioe.getMessage()); }
}
}
|
|